1,首先拿到应该获取的权限,账户信息里面能拿到pid
私钥与公钥,以及支付宝公钥
3,我使用的是thinkphp导入支付宝sdk
tp里面verdor可以直接调用支付宝
vendor('Alipay.AopSdk'); $aop = new \AopClient ();
4,封装alipayapi
个人二次次封装alipay的sdk调用
附上代码(加上自己的配置文件可以直接使用,具体根据自己业务需求做拓展):
<?php namespace Common\Lib\AlipayApi; /** * 支付宝sdk封装 * * Class ArrayFormat * @package Common\Lib\AlipayApi; */ class AlipayApi { public function alipayUserInfo($auth_code) { vendor('Alipay.AopSdk'); $aop = new \AopClient (); if(empty($auth_code)){ E('auth_code不能为空!'); } //APPID $appid = C('ALIPAY_CONFIG.appid'); //私钥 文件名(rsa_private_key.pem) $rsaPrivateKey = C('ALIPAY_CONFIG.rsaPrivateKey'); //公钥 文件名 (rsa_public_key.pem) $alipayrsaPublicKey = C('ALIPAY_CONFIG.alipayrsaPublicKey'); //初始化 $aop->gatewayUrl = C('ALIPAY_CONFIG.gatewayUrl'); $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($auth_code);//这里传入 code $result = $aop->execute($request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $access_token = $result->$responseNode->access_token; //获取用户信息 $request_a = new \AlipayUserUserinfoShareRequest(); $result_a = $aop->execute ($request_a,$access_token); //这里传入获取的access_token $responseNode_a = str_replace(".", "_", $request_a->getApiMethodName()) . "_response"; $alipayUser =array(); $alipayUser['alipay_user_id'] = $result_a->$responseNode_a->user_id; //用户唯一id $alipayUser['alipay_province'] = $result_a->$responseNode_a->province; //省份 $alipayUser['alipay_city'] = $result_a->$responseNode_a->city; //城市 $alipayUser['alipay_user_type'] = $result_a->$responseNode_a->user_type; // 用户类型(1/2)1代表公司账户2代表个人账户 $alipayUser['alipay_user_status'] = $result_a->$responseNode_a->user_status; //用户状态(Q/T/B/W)。Q代表快速注册用户T代表已认证用户B代表被冻结账户W代表已注册,未激活的账户 $alipayUser['alipay_is_certified'] = $result_a->$responseNode_a->is_certified; //是否通过实名认证。T是通过 F是没有实名认证。 return $alipayUser; } }