亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

ThinkPHP5 implements app Alipay payment function, have you learned it?
專注PHP中高級(jí)進(jìn)階學(xué)習(xí)
專注PHP中高級(jí)進(jìn)階學(xué)習(xí) 2018-11-29 11:33:12
0
1
1591

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();
        //獲取訂單號(hào)
        $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);
                //實(shí)例化alipay類
                $ali = new Alipay(); 
                //異步回調(diào)地址
                $url = 'XXXXXXXXXXXXXXXXXX/Callback/aliPayBack';
                $array = $ali->alipay('商品名稱', $money,$reoderSn,  $url);
                if ($array) {
                    return $array;
                } else {
                    echo json_encode(array('status' => 0, 'msg' => '對(duì)不起請檢查相關(guān)參數(shù)!@'));
                }
                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)
    {
        /**
         * 調(diào)用支付寶接口。
         */
        /*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 . "&";
        }
        //去掉最后一個(gè)&字符
        $arg = substr($arg, 0, count($arg) - 2);
        //如果存在轉(zhuǎn)義字符,那么去掉轉(zhuǎn)義
        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
{
    /*
     * 支付寶支付回調(diào)修改訂單狀態(tài)
     */
    public function aliPayBack()
    {
        if ($_POST['trade_status'] == 'TRADE_SUCCESS') {//如果支付成功
            //===============修改訂單狀態(tài)===========================//
            $order = new OrderGoods();//實(shí)例化
            $orderSn = $_POST['out_trade_no'];//獲取訂單號(hào)
                $where['order_sn'] = $orderSn;
                $data1['type'] = 2;
            $order->where($where)->update($data1);//修改訂單狀態(tài)
            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中高級(jí)進(jìn)階學(xué)習(xí)
專注PHP中高級(jí)進(jìn)階學(xué)習(xí)

專注PHP中高級(jí)進(jìn)階學(xué)習(xí),swoole,tp5,laravel等教程分享+VX:PHPopen888

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