簽名和驗簽在APP端也是可以完成的,考慮到安全問題,簽名和驗簽最好在服務端完成,支付寶官方建議也是這樣的,所以php端需要把簽名好的參數傳給APP端。
PHP服務端SDK產生APP支付訂單資訊樣本
$aop = new AopClient;$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";$aop->appId = "app_id";$aop->rsaPrivateKey = '請填寫開發人員私密金鑰去頭去尾去斷行符號,一行字串';$aop->format = "json";$aop->charset = "UTF-8";$aop->signType = "RSA2";$aop->alipayrsaPublicKey = '請填寫支付寶公開金鑰,一行字串';//執行個體化具體API對應的request類,類名稱和介面名稱對應,當前調用介面名稱:alipay.trade.app.pay$request = new AlipayTradeAppPayRequest();//SDK已經封裝掉了公用參數,這裡只需要傳入業務參數$bizcontent = "{\"body\":\"我是測試資料\"," . "\"subject\": \"App支付測試\"," . "\"out_trade_no\": \"20170125test01\"," . "\"timeout_express\": \"30m\"," . "\"total_amount\": \"0.01\"," . "\"product_code\":\"QUICK_MSECURITY_PAY\"" . "}";$request->setNotifyUrl("商戶外網可以訪問的非同步地址");$request->setBizContent($bizcontent);//這裡和普通的介面調用不同,使用的是sdkExecute$response = $aop->sdkExecute($request);//htmlspecialchars是為了輸出到頁面時防止被瀏覽器將關鍵參數html轉義,實際列印到日誌以及http傳輸不會有這個問題echo htmlspecialchars($response);//就是orderString 可以直接給用戶端請求,無需再做處理。
以下是我們項目所使用的demo
<?php /*creatOrder.php *1.在資料庫插入訂單資訊 *2.產生訂單號,返回商品資訊 */ include __DIR__.'/config/apiConfig.php'; $m = include __DIR__.'/config/mysqlConfig.php'; include __DIR__.'/function/time.php'; include __DIR__.'/function/checkUid.php'; include __DIR__.'/alipay/aop/AopClient.php'; include __DIR__.'/alipay/aop/request/AlipayTradeAppPayRequest.php'; /**/ // error_reporting(E_ALL); // //設定錯誤資訊的類別。 // ini_set('display_error','1'); foreach ($_POST as $key => $value) { $$key = $value; } errorLog(function_exists('openssl_sign')); // if(!checkUid($uid)){ OPjson(['code'=>'0011','msg'=>'使用者不存在']); }; //建立訂單號 $time = $goodsId.time(); $str = bin2hex($uid); eval('$time ='."dechex($time);"); $orderId = $time.$str; $order = $m->order; $insert = [ 'uid'=>$uid, 'orderId'=>$orderId, 'status'=>0, 'time'=>time(), ]; $order->insert($insert,M::INSERT); $goods = $m->goods; $where['='] = [ 'goodsId'=>$goodsId, ]; $result = $goods->select(['price'])->where($where); $price = $result->fetch()['price'];?>
<?php /* *這裡使用的orderId和price請看creatOrder.php */ include __DIR__.'/creatOrder.php'; //連結商品表 $aop = new AopClient(); $aop->signType = "RSA2"; $request = new AlipayTradeAppPayRequest(); $bizcontent = '{"body":"1",' .'"subject": "1",' .'"out_trade_no": "'.$orderId.'",' .'"timeout_express": "30m",' .'"total_amount": "'.$price.'",' .'"product_code":"QUICK_MSECURITY_PAY"}'; //商戶外網可以訪問的非同步地址 $request->setNotifyUrl("http://www.***.com/api/alipayUrl.php"); $request->setBizContent($bizcontent); //在這我理解的是AopClient內部產生了一個簽名,而在回調的時候驗證了這個簽名 $response = $aop->sdkExecute($request); errorLog($response); // echo $response; OPjson(['code'=>'0000','info'=>$response]);?>
PHP服務端驗證非同步通知資訊參數樣本
$aop = new AopClient;$aop->alipayrsaPublicKey = '請填寫支付寶公開金鑰,一行字串';$flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
以下是我們項目所使用的demo
<?php/**這裡有個坑,要使用的是支付寶的公開金鑰,不要搞錯哦。它可不是自己產生的,而是支付寶給的*/include __DIR__.'/alipay/aop/AopClient.php';$aop = new AopClient;$aop->alipayrsaPublicKey = '請填寫支付寶公開金鑰,一行字串';$flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");errorLog($_POST);echo "success";