ThinkPHP5 implements app Alipay payment function, have you learned it?
专注PHP中高级进阶学习
专注PHP中高级进阶学习 2018-11-29 11:33:12
0
1
1499

I used a total of three controllers to make this Alipay payment:

1: Alipay payment controller.

2: Alipay payment configuration parameter controller.

3: Alipay callback address controller.

First create an Alipay payment controller, which is the controller that needs to be passed after selecting the payment method on the previous page and confirming the payment (Alipay payment is selected here), the code is as follows:

<?php
namespace app\mobile\controller;
use app\mobile\model\Goods;
use app\mobile\model\OrderGoods;
use think\Controller;
class Pay extends Controller
{
    public function pay_order()
    {
        $res = new OrderGoods();
        //获取订单号
        $where['id'] = input('post.order_sn');
        $reoderSn = input('post.order_sn');
        //查询订单信息
        $order_info = $res->where($where)->find();
        //获取支付方式
        $pay_type = input('post.pay_type');//微信支付 或者支付宝支付
        //获取支付金额
        $money = input('post.totle_sum');
        //判断支付方式
        switch ($pay_type) {
            case 'ali';//如果支付方式为支付宝支付
                //更新支付方式为支付宝
                $type['pay_type'] = 'ali';
                $res->where($where)->update($type);
                //实例化alipay类
                $ali = new Alipay(); 
                //异步回调地址
                $url = 'XXXXXXXXXXXXXXXXXX/Callback/aliPayBack';
                $array = $ali->alipay('商品名称', $money,$reoderSn,  $url);
                if ($array) {
                    return $array;
                } else {
                    echo json_encode(array('status' => 0, 'msg' => '对不起请检查相关参数!@'));
                }
                break;
            case 'wx';
                break;
        }
    }
}

Then I created an Alipay controller and wrote the Alipay configuration parameters. The code is as follows:

class AliPay extends Controller
{
    protected $appId = '';//支付宝AppId
    protected $rsaPrivateKey = '';//支付宝私钥
    protected $aliPayRsaPublicKey = '';//支付宝公钥
    private $seller = '';
    /*
     * 支付宝支付
     */
    public function aliPay($body, $total_amount, $product_code, $notify_url)
    {
        /**
         * 调用支付宝接口。
         */
        /*import('.Alipay.aop.AopClient', '', '.php');
        import('.Alipay.aop.request.AlipayTradeAppPayRequest', '', '.php');*/
        Loader::import('Alipay\aop\AopClient', EXTEND_PATH);
        Loader::import('Alipay\aop\request\AlipayTradeAppPayRequest', EXTEND_PATH);
        $aop = new \AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = $this->aliPayRsaPublicKey;
        $request = new \AlipayTradeAppPayRequest();
        $arr['body'] = $body;
        $arr['subject'] = $body;
        $arr['out_trade_no'] = $product_code;
        $arr['timeout_express'] = '30m';
        $arr['total_amount'] = floatval($total_amount);
        $arr['product_code'] = 'QUICK_MSECURITY_PAY';
        $json = json_encode($arr);
        $request->setNotifyUrl($notify_url);
        $request->setBizContent($json);
        $response = $aop->sdkExecute($request);
        return $response;
    }
    function createLinkstring($para)
    {
        $arg = "";
        while (list ($key, $val) = each($para)) {
            $arg .= $key . "=" . $val . "&";
        }
        //去掉最后一个&字符
        $arg = substr($arg, 0, count($arg) - 2);
        //如果存在转义字符,那么去掉转义
        if (get_magic_quotes_gpc()) {
            $arg = stripslashes($arg);
        }
        return $arg;
    }
    function argSort($para)
    {
        ksort($para);
        reset($para);
        return $para;
    }
}

I placed the Alipay payment demo in the extend directory and used this method to implement Alipay. For payment, just copy the contents of the above Alipay configuration parameter controller except Alipay Appid, Alipay public key, private key, and payee account number. Now two controllers have been written. Next, there are An asynchronous callback address.

<?php
namespace app\mobile\controller;
use app\mobile\model\OrderGoods;
use app\mobile\model\IntegralRecord;
use app\admin\model\SystemSettings;
use app\mobile\model\Members;
use think\Controller;
use think\Request;
use think\Db;
class Callback extends Controller
{
    /*
     * 支付宝支付回调修改订单状态
     */
    public function aliPayBack()
    {
        if ($_POST['trade_status'] == 'TRADE_SUCCESS') {//如果支付成功
            //===============修改订单状态===========================//
            $order = new OrderGoods();//实例化
            $orderSn = $_POST['out_trade_no'];//获取订单号
                $where['order_sn'] = $orderSn;
                $data1['type'] = 2;
            $order->where($where)->update($data1);//修改订单状态
            echo 'success';
            exit;
        }
    }
}

OK now the code is finished. When executing an Alipay payment order, first enter the Alipay Pay controller to receive the three parameters passed by POST, order number, amount, and payment method. After receiving it, write the product name, product description, callback address, etc. and then instantiate the Alipay class and call the aliPay method in this class to make the payment. The call result returns a signature, which is fed back to the app for payment. After the payment is successful, an asynchronous callback is executed. , modify order status


专注PHP中高级进阶学习
专注PHP中高级进阶学习

专注PHP中高级进阶学习,swoole,tp5,laravel等教程分享+VX:PHPopen888

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template