アリペイ支払公開鍵証明書方式

8071 ワード

支付宝「単笔振り込みインタフェース」をドッキングしたところ、资金支出系インタフェースは证明书方式しか使用できないことがわかり、最新のsdkを导入し、元の支払いインタフェースを修正し、振り込みインタフェースを増やした.
AlipayBase.php
appId = 'test1111111111';//TODO      id

        $this->rsaPrivateKeyPath = root_path() . "library/alipay/rsaPrivateKey.txt";//TODO                     
        $this->appCertPath = root_path() . "library/alipay/appCertPublicKey_2021001154615307.crt";//TODO         crt     
        $this->rootCertPath = root_path() . "library/alipay/alipayRootCert.crt";//TODO         crt         
        $this->alipayrsaPublicKeyPath = root_path() . "library/alipay/alipayCertPublicKey_RSA2.txt";//TODO         txt          (                      ,              )

        $this->alipayrsaPublicKey = file_get_contents($this->alipayrsaPublicKeyPath);//TODO            ($aop->getPublicKey($alipayCertPath);//  getPublicKey             ),    ,     file_get_contents
        $this->rsaPrivateKey = file_get_contents($this->rsaPrivateKeyPath);

        require_once root_path() . 'library/alipay/aop/AopCertClient.php';
        //1、execute   
        $aop = new \AopCertClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';
        $aop->isCheckAlipayPublicCert = true;//                ,                    

        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->appCertSN = $aop->getCertSN($this->appCertPath);//  getCertSN       
        $aop->alipayRootCertSN = $aop->getRootCertSN($this->rootCertPath);//  getRootCertSN           

        $this->aop = $aop;
    }

    /**
     *       
     * @return mixed
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     *       
     * @param mixed $error
     */
    public function setError($error = '       ,   '): bool
    {
        $this->error = $error;
        return false;
    }
}

Trans.php
 'DIRECT_TRANSFER',
                'product_code' => 'TRANS_ACCOUNT_NO_PWD',
                'order_title' => $order_title,
                'out_biz_no' => $data['out_biz_no'],//TODO    
                'trans_amount' => $data['trans_amount'],//TODO   
                'payee_info' => [
                    'identity_type' => 'ALIPAY_LOGON_ID',
                    'identity' => $data['identity'],//TODO    
                    'name' => $data['name'],//TODO   
                ]
            ];
            $bizcontent = json_encode($param);

            $request->setBizContent($bizcontent);
            $result = $this->aop->execute($request);

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

            if(!empty($resultCode)&&$resultCode == 10000){
                return $result->$responseNode;
            } else {
                return $this->setError($result->$responseNode->sub_msg);
            }
        } catch (\Exception $e) {
            return $this->setError($e->getMessage());
        }
    }

    /**
     *       
     * @param $out_biz_no
     * @return bool|\SimpleXMLElement
     */
    public function find($out_biz_no){
        try {
            require_once root_path() . 'library/alipay/aop/request/AlipayFundTransOrderQueryRequest.php';
            $request = new \AlipayFundTransOrderQueryRequest ();

            $param = [
                'out_biz_no' => $out_biz_no,//TODO    
            ];
            $bizcontent = json_encode($param);

            $request->setBizContent($bizcontent);
            $result = $this->aop->execute($request);

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

            if(!empty($resultCode)&&$resultCode == 10000){
                return $result->$responseNode;
            } else {
                return $this->setError($result->$responseNode->sub_msg);
            }
        } catch (\Exception $e) {
            return $this->setError($e->getMessage());
        }
    }
}

AppPay.php
 $subject,
                'subject' => $subject,//    
                "timeout_express" => $timeout_express,//        
                'out_trade_no' => $data['out_trade_no'],//   
                'total_amount' => $data['total_amount'],//  
            ];
            $bizcontent = json_encode($param);

            $alipay_notify_url = $notify_url;
            $request->setNotifyUrl($alipay_notify_url);//TODO              

            $request->setBizContent($bizcontent);
            $result = $this->aop->sdkExecute($request);//TODO           sdkExecute
            return $result;
        } catch (\Exception $e) {
            return $this->setError($e->getMessage());
        }
    }

    /**
     *     
     * @param $data    post  
     * @return bool
     */
    public function notify($data){
        return $this->aop->rsaCheckV1($data, NULL, $data['sign_type']);
    }
}

テストの呼び出し
//  app      
$data = [
  'out_trade_no' => $out_trade_no,
  'total_amount' => $amount
];
$appPay = new AppPay();
$response = $appPay->pay($data);

//      
$appPay = new AppPay();
$flag = $appPay->notify($data);

//      
$out_biz_no = 'test'.time();
$data = [
  'out_biz_no' => $out_biz_no,
  'trans_amount' => 1.00,
  'identity' => '17539592164',
  'name' => '  '
];
$trans = new Trans();
$res = $trans->transfer($data);
if($res) var_dump('  ');
else var_dump($trans->getError());

//          
$trans = new Trans();
$res = $trans->find($out_biz_no);
if($res) var_dump($res);
else var_dump($trans->getError());