重要的事情說三遍:PHP支付不能用csr檔案上傳的方式設定公鑰!PHP支付不能用csr檔案上傳的方式設定公鑰!PHP支付不能用csr檔案上傳的方式設定公鑰!
架構:TP5 + mysql
設定順序:
1.在支付寶開發者中心中申請應用,例:應用為:測試支付,appID:abc123
2.在tp5根目錄vendor檔案夾下加入Alipay的PHPSDK(官方文檔中下載下傳)
3.業務代碼
function aliPay($order_id, $total_amount){
$config = [
'appid' => 'abc123',
'notify_url' => '本地可外部通路的url連結',
'rsaPrivateKey' => '申請應用時用的私鑰',
'alipayrsaPublicKey' => '支付寶公鑰!支付寶公鑰!支付寶公鑰!'
];
if ($order_id!= '' && $total_amount > 0) {
vendor('Alipay.AopSdk');
$aop = new \AopClient();
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";//寫死不用動
$aop->format = "json";//寫死不用動
$aop->charset = "utf-8";//寫死不用動
$aop->signType = "RSA2";//寫死不用動
$aop->appId = $config['appid'];
$aop->rsaPrivateKey = $config['rsaPrivateKey'];
$aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];
//執行個體化具體API對應的request類,類名稱和接口名稱對應,目前調用接口名稱:alipay.trade.app.pay
$request = new \AlipayTradeAppPayRequest();
//SDK已經封裝掉了公共參數,這裡隻需要傳入業務參數
$bizcontent = json_encode([
'subject' => '訂單的簡要概述',
'out_trade_no' => $order_id,//此訂單号為商戶唯一訂單号
'total_amount' => $total_amount,//訂單價格,保留兩位小數
'product_code' => 'QUICK_MSECURITY_PAY'//寫死不用動
],JSON_UNESCAPED_UNICODE);
$request->setNotifyUrl($config['notify_url']);
$request->setBizContent($bizcontent);
//這裡和普通的接口調用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
//htmlspecialchars是為了輸出到頁面時防止被浏覽器将關鍵參數html轉義,實際列印到日志以及http傳輸不會有這個問題
//此處str_replace為我個人加入的,在傳回安卓端的時候有時會帶上“amp;”,雖然不影響,去掉以防萬一
$response = str_replace('amp;', '', htmlspecialchars($response));//就是orderString 可以直接給用戶端請求,無需再做處理。
return $response;
} else {
return false;
}
}
其他業務代碼略
//打完收工w(゚Д゚)w