我们项目通过扫码支付,HTML5与后台交互,先获取到用户的信息,在通过信息进行一些支付操作
首先:获取用户信息
$appid=config('alipay_id');
$redirect_uri = urlencode ('你的回调地址');
$url ="https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id={$appid}&scope=auth_base&redirect_uri={$redirect_uri}";
header("Location:".$url);//此处进行地址跳转
//app_id 支付宝分给商家的标示
//scope 授权方式 auth_base 静默授权(不需要用户点击直接获取权限)auth_userinfo(需要用户手动授权)
//redirect_uri 授权回调地址
支付宝获取商家信息文档地址:https://docs.open.alipay.com/53/104114
支付宝回调地址在开发者配置里边设置 有一个授权回调地址 改为自己可以访问的服务器可以访问路径
/**
* 授权回调地址 支付宝
* @return [type] [description]
*/
public function backAlipayUrl()
{
//支付宝授权 获取用户信息
$code = $_REQUEST['auth_code'];
//APPID
$appid = config('alipay_id');
//私钥 文件名(rsa_private_key.pem)
$rsaPrivateKey = config('private_key');
//公钥 文件名 (rsa_public_key.pem)
$alipayrsaPublicKey = config('public_key');
//config[public_key]
//初始化
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = $appid;
$aop->rsaPrivateKey = $rsaPrivateKey;
$aop->alipayrsaPublicKey = $alipayrsaPublicKey;
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
//获取access_token
$request = new AlipaySystemOauthTokenRequest ();
$request->setGrantType("authorization_code");
$request->setCode($code);//这里传入 code
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
// $access_token = $result->$responseNode->access_token;
$result = json_decode(json_encode($result),true);
$user_id = $result[$responseNode]['user_id'];//用户信息 userID
Cache::set('alipayUserid',$user_id);
return $this->redirect('Payback/pay');
}