wqy
2018-02-25 12:00:37
13895

更新时间:20210131

 

1 直接下载sdk

        $appCertPath = config('alipay.appCertPath');//"应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
        $alipayCertPath = config('alipay.alipayCertPath');//"支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
        $rootCertPath =  config('alipay.rootCertPath');//"支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";
        $passback_params = urlencode($order_type);

        $aop = new \AopCertClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config('alipay.appId');
        $aop->format = 'json';
        $aop->charset = 'UTF-8';
        $aop->signType = 'RSA2';
        $aop->rsaPrivateKey = config('alipay.rsaPrivateKey');
        $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
        $aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
        $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号


        $request = new \AlipayTradeAppPayRequest();
        $bizcontent = "{\"body\":\"$subject\","
            . "\"subject\": \"$subject\","
            . "\"out_trade_no\": \"$out_trade_no\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"$total_amount\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\","
            . "\"passback_params\":\"$passback_params\""
            . "}";
        $request->setNotifyUrl(config('alipay.notifyUrl'));
        $request->setBizContent($bizcontent);

        //这里和普通的接口调用不同,使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        return $response;

 

2 使用composer 

 

 public function __construct()
    {
        Factory::setOptions($this->getOptions());
    }

    /**
     * 获取支付参数
     * @param $subject
     * @param $outTradeNo
     * @param $totalAmount
     * @return string
     * @throws \Exception
     */
    public function getPayParams($subject, $outTradeNo, $totalAmount){

        try {
            //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
            $result = Factory::payment()->app()->pay($subject, $outTradeNo, $totalAmount);
            $responseChecker = new ResponseChecker();

//            dump(json_decode(json_encode($result),true));
            //3. 处理响应或异常
            if ($responseChecker->success($result)) {
                return $result->body;
            } else {
               ServerResponse::error("调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL);
            }
        } catch (Exception $e) {
            ServerResponse::error("调用失败,". $e->getMessage(). PHP_EOL);
        }
    }