PHP对接支付宝支付APP端

33 篇文章 0 订阅
14 篇文章 0 订阅

1、登录支付宝开放平台--控制台--新建自研应用--网页&移动应用

1.1、添加能力--APP支付,等待审核

1.2、设置接口加签方式,使用公私钥方式,不是证书

 

2、下载SDK,看文档

 

laravel框架


AopClient.php 

添加命名空间

namespace App\Libs\AlipaySDK\aop;




use App\Libs\AlipaySDK\aop\AopClient;


$orderNo = $data['payNo'];
$amount = $data['amount'];
$body = $data['describe'];

//2、sdkExecute 测试
        $aop = new AopClient();

        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  
        $aop->appId = '******';

        $aop->rsaPrivateKey = 'yyyy';
        $aop->alipayrsaPublicKey = 'xxxx';

        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';

        $request = new AlipayTradeAppPayRequest();



        $notify_url =  'http://api.***.com/payment/alinotify';


        $request->setNotifyUrl($notify_url);

        $request->setBizContent("{" .
            "\"body\":\"$body\"," . //对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。
            "\"subject\":\"起床了么\"," . //商品的标题/交易标题/订单标题/订单关键字等。
            "\"out_trade_no\":\"$orderNo\"," . //商户网站唯一订单号
            "\"total_amount\":\"$amount\"," . //订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
            "\"product_code\":\"QUICK_MSECURITY_PAY\"," .

            "  }");

        $result = $aop->sdkExecute($request);


        return $result;




/**
     * 支付宝回调
     * @param Request $request
     * @return string
     * @throws \App\Exceptions\NotAcceptableException
     *
     */
    public function alinotify(Request $request)
    {
        $data = $request->all();

        Log::info('支付信息回调alinotify--接收参数--' . json_encode($data));

        $aop = new AopClient;

        $aop->alipayrsaPublicKey = 'xxxxx';


        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if ($flag) {
            if ($data['trade_status'] == 'TRADE_SUCCESS') {

                $out_trade_no = $request->input('out_trade_no');
                $buyer_logon_id = $request->input('buyer_logon_id');
                $total_amount = $request->input('total_amount');
                $trade_status = $request->input('trade_status');
                $trade_no = $request->input('trade_no');
                $sign_type = $request->input('sign_type');

                file_put_contents(storage_path() . '/logs/paylog.log', json_encode($request->all()) . PHP_EOL, FILE_APPEND);
                $orderModel = new Order();
                $info = $orderModel->getOrderBySerial($out_trade_no);

                if ($info && $trade_status == 'TRADE_SUCCESS' && (bccomp($total_amount, $info['amount'], 2) == 0)) {

                    // $alipay = new AliPay();
                    if ($info['payStatus'] == 1) {
                        Log::info('支付信息回调错误--订单已支付--');
                        return 'success';
                        // return $alipay->getReturnData('SUCCESS');
                    }
                    if ($info['payStatus'] !== 0) {
                        Log::info('支付信息回调错误--无支付信息--');
                        return 'success';
                        // $this->fail('无支付信息');
                    }


                    $isCall = Redis::hsetnx('ORDER_CALL_BACK', $info['payNo'], 1);
                    if ($isCall == 1) {
                        $ret = $orderModel->successPay($out_trade_no, $buyer_logon_id, $trade_no);
                        if ($ret) {
                            (new User())->successPay($info);

                            return 'success';
                        }
                        
                    } 
                       
                }
            }
        } else {
            Log::info('支付信息回调错误--验签错误--$flag--' . json_encode($flag));
        }

        return "fail";
    }

tp6

/**
     * 获得jsSdk支付参数
     */
    public function jsPay($out_trade_no, $total_fee, $describe, $subject)
    {
        Log::info('AlipayService---jsPay---begin------');

        require_once('./extend/Alipay/aop/AopClient.php');
        require_once('./extend/Alipay/aop/request/AlipayTradeAppPayRequest.php');

        //2、sdkExecute 测试
        $aop = new \AopClient();

        $aop->appId = $this->config['payment']['appId'];

        //'请填写开发者私钥去头去尾去回车,一行字符串'
        $aop->rsaPrivateKey = $this->config['payment']['rsaPrivateKey'];

        //'请填写支付宝公钥,一行字符串'
        $aop->alipayrsaPublicKey = $this->config['payment']['alipayrsaPublicKey'];

        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';

        $request = new \AlipayTradeAppPayRequest();

        $notify_url = $this->config['payment']['notify_url'];

        $request->setNotifyUrl($notify_url);

        $request->setBizContent("{" .
            "\"body\":\"$describe\"," . //对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。
            "\"subject\":\"$subject\"," . //商品的标题/交易标题/订单标题/订单关键字等。必选
            "\"out_trade_no\":\"$out_trade_no\"," . //商户网站唯一订单号。必选
            "\"total_amount\":\"$total_fee\"," . //订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]。必选
            "\"product_code\":\"QUICK_MSECURITY_PAY\"," .
            "  }");

        $result = $aop->sdkExecute($request);

        Log::info('AlipayService---jsPay---$result------' . json_encode($result));
        Log::info('AlipayService---jsPay---end------');

        return $result;


        // return $this->paymentService()->configForJSSDKPayment($this->paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options));
    }



    public function handleNotify()
    {

        require_once('./extend/Alipay/aop/AopClient.php');

        $data = input('param.'); 

        Log::info('支付宝回调handleNotify--接收参数--' . json_encode($data));

        $aop = new \AopClient;

        $aop->alipayrsaPublicKey = $this->config['payment']['alipayrsaPublicKey'];


        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if ($flag) {
            if ($data['trade_status'] == 'TRADE_SUCCESS') {

                try {
                    //TODO

                    return 'success';
                } catch (\Exception $e) {
                    Log::info('支付宝支付回调失败:' . $e->getMessage());
                    return 'fail';
                }
            }
        } else {
            Log::info('支付信息回调错误--验签错误--$flag--' . json_encode($flag));
        }
        return "fail";
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值