wqy
2018-02-25 12:01:28
14371

 

 

1 直接使用下载的sdk

 /**
     * 退款 
     * @param $out_trade_no 交易单号
     * @param $refund_amount
     * @return bool|array
     * @throws \Exception
     */
    public static function refund($out_trade_no,$refund_amount){


        $out_request_no = StringUtil::makeOrderNo();//退款单号

        //1、execute 使用
        $aop = new \AopCertClient();
        $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";

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

        $request = new \AlipayTradeRefundRequest ();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"$out_trade_no\"," .
            "\"refund_amount\":$refund_amount," .
            "\"refund_reason\":\"取消订单-退款\"," .
            "\"out_request_no\":\"$out_request_no\"" .
            "  }");
        $result = $aop->execute ( $request );

        Log::record($result);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;

        if(!empty($resultCode)&&$resultCode == 10000){

            $data = [
                'refund_no'=>$out_request_no,
                'pay_order_no'=>$out_trade_no,
                'refund_trade_no'=>$result->$responseNode->trade_no
            ];
            return $data;
        } else {

            throw new Exception($result->$responseNode->sub_msg);
        }
    }

2 使用composer

  /**
     * 退款
     * @param $outTradeNo
     * @param $refundAmount
     * @return mixed
     * @throws \Exception
     */
    public function refund($outTradeNo, $refundAmount){
        try {
            //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
            $result = Factory::payment()->common()->refund($outTradeNo, $refundAmount);

            $responseChecker = new ResponseChecker();


            //3. 处理响应或异常
            if ($responseChecker->success($result)) {

                $data = [
                    'trade_no'=>$result->trade_no,
                    'out_trade_no'=>$result->out_trade_no,
                    'refund_fee'=>$result->refund_fee
                ];
                return $data;
            } else {
                ServerResponse::error("调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL);
            }
        } catch (Exception $e) {
            ServerResponse::error("调用失败,". $e->getMessage(). PHP_EOL);
        }
    }