下載支付寶sdk後解壓放到extend目錄下
在public/index.php中定義常量
參考文檔
define('ALI_PATH',__DIR__.'/../extend/alipay/aop/');
創建控制器並寫入代碼
require ALI_PATH.'AopClient.php';
require ALI_PATH.'request/AlipayFundAuthOrderAppFreezeRequest.php';
class Ali extends Controller{
public static $appid = ''; //appid
public static $pub_key = ''; //應用公鑰
public static $prikey = ''; //應用私鑰
public static $alipubkey = '';//支付寶公鑰
public function shouquan()
{
$aop = new \AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = self::$appid;
$aop->rsaPrivateKey = self::$prikey;
$aop->alipayrsaPublicKey=self::$alipubkey;
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new \AlipayFundAuthOrderAppFreezeRequest ();
$order_no = time().rand(1111,9999).'orderid';
$l = time().rand(1111111,9999999).'liushui';
DB::name('ali_demo')->insert(['order_no'=>$order_no,'l'=>$l]); //將生成的訂單號和流水號存儲在數據庫,查訂單,撤銷訂單時需要用到
$request->setBizContent("{" .
"\"out_order_no\":\"".$order_no."\"," . //商戶訂單號
"\"out_request_no\":\"".$l."\"," . //商戶流水號
"\"order_title\":\"測試預授權凍結\"," . //描述
"\"amount\":0.01," . //金額
"\"product_code\":\"PRE_AUTH_ONLINE\"," .
"\"payee_logon_id\":\"[email protected]\"," . //收款方支付寶賬號
"\"pay_timeout\":\"90m\"," .
"\"scene_code\":\"ONLINE_AUTH_COMMON_SCENE\"," .
"\"trans_currency\":\"CNY\",".
"\"settle_currency\":\"CNY\",".
"\"enable_pay_channels\":\"[{\\\"payChannelType\\\":\\\"PCREDIT_PAY\\\"},{\\\"payChannelType\\\":\\\"MONEY_FUND\\\"}]\"" .
" }");
$request->setNotifyUrl(''); //請求成功後的回調地址
$result = $aop->sdkexecute ( $request);
return json_encode($result);
}
}
上面的方法返回一個字符串,將字符串傳到前端,前端用這個字符串來喚起支付頁面,在支付寶app執行資金凍結
返回的字符串中有資金授權查詢接口需要的請求參數數據