php 支付宝支付

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sfmcatl/article/details/83415710

在给app做支付宝支付接口的时候收集内容整理如下:

接口:

       import('alipay.AopClient', EXTEND_PATH);
        import('alipay.request.AlipayTradeAppPayRequest', EXTEND_PATH);

        $aop = new \AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = Config::get('alipay.app_id');
        $aop->rsaPrivateKey = Config::get('alipay.rsa_private_key');
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = Config::get('alipay.alipay_rsa_public_key'); //Config::get('alipay.rsa_public_key'); //

        $request = new \AlipayTradeAppPayRequest();

        $bizcontent = json_encode([
            'body' => '红包支付',
            'subject' => '用户红包支付',
            'out_trade_no' => $orderSn,
            'timeout_express' => '30m',
            'total_amount' => $amount,
            'product_code' => 'QUICK_MSECURITY_PAY'
        ]);

        $request->setNotifyUrl("https://" . $_SERVER['HTTP_HOST'] . "/api/callback/alipay");
        $request->setBizContent($bizcontent);//这里和普通的接口调用不同,使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        $return['aliresponse'] = $response;//htmlspecialchars($response);//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
        $this->returnSuccess($return);

回调处理:

    public function alipay()
    {
        import('alipay.AopClient', EXTEND_PATH);
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = Config::get('alipay.alipay_rsa_public_key');// file_get_contents($_SERVER['DOCUMENT_ROOT']."/key/alipay/alipay_public_key.txt");
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        if ($flag){
            //验签通过后核实如下参数trade_status、out_trade_no、total_amount、seller_id
            if (isset($_POST['trade_status']) && ($_POST['trade_status'] == 'TRADE_SUCCESS' || $_POST['trade_status'] == 'TRADE_FINISHED') ) {
                // 业务处理

                
            } else {
                $msg = var_export($_POST, true);
                $fileName = mt_rand(10,1000) .'00alisuccessAnother.log' ;
                file_put_contents($fileName, $msg);
            }
            echo "success";

        } else {
            $msg = var_export($_POST, true);
            $fileName = mt_rand(10,1000) .'00alicheckSignFail.log';
            file_put_contents($fileName, $msg);
        }
    }

参考链接:

手机网站支付结果异步通知 https://docs.open.alipay.com/203/105286/

App支付服务端DEMO&SDK https://docs.open.alipay.com/54/106370/

个人文章:

 https://blog.csdn.net/swimming_in_it_/article/details/78540643
https://blog.csdn.net/xiojing825/article/details/65448745?utm_source=itdadao&utm_medium=referral

猜你喜欢

转载自blog.csdn.net/sfmcatl/article/details/83415710