因為公司業務需求,需接入支付寶ISV業務,成為支付寶的服務商,商戶對開發者進行應用授權后,開發者可以幫助商戶完成相應的業務邏輯,例如代替商戶發起當面付的收單請求。
此業務只適用於支付寶當面付功能。
對應用授權,可以采用URL拼接,PC訪問授權,或者將url生成二維碼,商戶用手機端支付寶掃碼,授權。
URL拼接規則:
https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id=應用ID&redirect_uri=回調地址
除了app_id 和 redirect_uri必要參數外,還可以附加自身業務參數,在回調地址中處理,以綁定此授權碼和授權商戶的關系
回調處理demo:
<?php /** * 支付寶isv授權回調地址,通過code和refresh_token換取token * User: Administrator * Date: 2017/2/26 * Time: 16:17 */ date_default_timezone_set('PRC'); $config = array(); require_once 'protected/extensions/AliF2F/f2fpay/service/AlipayTradeService.php'; require_once 'protected/extensions/AliF2F/f2fpay/config/lkhealth_config.php'; isset($_REQUEST['app_auth_code']) && $code = addslashes($_REQUEST['app_auth_code']); //授權code isset($_REQUEST['hq_id']) && $hq_id = (int)$_REQUEST['hq_id']; //在授權鏈接上帶的店鋪標識 $currentDate = date('Y-m-d H:i:s'); (!$code || !$hq_id) && exit('Access Denied!'); $aop = new AopClient (); $aop->gatewayUrl = $config['gatewayUrl']; $aop->appId = $config['app_id']; $aop->rsaPrivateKey = $config['merchant_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = $config['sign_type']; $aop->postCharset = $config['charset']; $aop->format = 'json'; $request = new AlipayOpenAuthTokenAppRequest (); $params = array( 'grant_type' => 'authorization_code', //refresh_token 'code' => $code, 'hq_id' => $hq_id, 'refresh_token' => '' ); $request->setBizContent(json_encode($params)); $result = $aop->execute ( $request ); /** { "alipay_open_auth_token_app_response":{ "app_auth_token":"201509BBeff9351ad1874306903e96b91d248A36", "app_refresh_token":"201509BBdcba1e3347de4e75ba3fed2c9abebE36", "auth_app_id":"2013121100055554", "code":"10000", "expires_in":"123456", "msg":"Success", "re_expires_in":"123456", "user_id":"2088102150527498" }, "sign":"ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE" } */ $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultObj = $result->$responseNode; $resultCode = $resultObj->code; if(!empty($resultCode) && $resultCode == 10000){ //授權成功,保存店鋪對應的app_auth_token echo "success"; } else { //授權失敗 echo $result->$responseNode->msg; }