一,官方文档说明入口
https://opendocs.alipay.com/open/20190111144811460526/quickstart
环境配置 php + thinkphp3.2
二,红包步骤
前期准备,应用申请及开放平台设置(关键是 公钥证书 设置)
App<==>后端(php)授权串(支付宝登录授权完成版)
App->授权后拿到支付宝唯一标识uid->后端php( alipay.fund.trans.uni.transfer )
三,步骤说解
1,下载 支付宝开放平台开发助手windows版 非java放言选择非JAVA适用
获取csr文件:和开放平台申请填写信息完全一致。
按说明:配置好公钥证书相关。
二,APP向PHP请求完整授权串
$sign_type = 'RSA2'; $data = [ 'apiname' => 'com.alipay.account.auth', 'method' => 'alipay.open.auth.sdk.code.get', 'app_id' => $app_id, 'app_name' => 'mc', 'biz_type' => 'openservice', 'pid' => $pid, 'product_id' => 'APP_FAST_LOGIN', 'scope' => 'kuaijie', 'target_id' => $target_id, 'auth_type' => 'AUTHACCOUNT', 'sign_type' => $sign_type, ];
vendor('alipay2.AopCertClient');
$aop = new AopCertClient;
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = $app_id;
$aop->rsaPrivateKey = $aliConf['rsaPrivateKey']; //私钥
$aop->alipayrsaPublicKey = $aliConf['rsaPublicKey']; //公钥
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset = 'UTF-8';
$aop->format = 'json';
$aop->appCertSN = $aop->getCertSN(支付宝应用证书路径); //支付宝应用证书
$aop->alipayRootCertSN = $aop->getRootCertSN(支付宝应用根证书路径); //根证书sn
$data["sign"] = urlencode($aop->generateSign($data, $sign_type))
$strArr = [];
foreach ($data as $k => $v){
$strArr[] = "$k=$v";
}
$authInfo = implode('&',$strArr);
返回授权串$authInfo给客户端调用,客户正常授权后拿支付宝uid
三,APP拿授权后uid向PHP请求发送支付宝红包
vendor('alipay2.request.AlipayFundTransUniTransferRequest'); $request = new AlipayFundTransUniTransferRequest(); $payee_info = [ 'identity' => 支付宝uid, 'identity_type' => "ALIPAY_USER_ID", ]; $payParams = [ "out_biz_no" => 订单号, "trans_amount" => 红包金额, "product_code" => "STD_RED_PACKET", "biz_scene" => "DIRECT_TRANSFER", "order_title" => "红包标题", "payee_info" => $payee_info, "remark" => "红包描述", ]; $request->setBizContent(json_encode($payParams)); vendor('alipay2.AopCertClient'); $aop = new AopCertClient; $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $app_id; $aop->rsaPrivateKey = $aliConf['rsaPrivateKey']; $aop->alipayrsaPublicKey = $aliConf['rsaPublicKey']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'UTF-8'; $aop->format = 'json'; $aop->appCertSN = $aop->getCertSN($aliConf['alipayrsaAppPath']); //支付宝应用证书 $aop->alipayRootCertSN = $aop->getRootCertSN($aliConf['alipayrsaRootPath']); //根证书sn $aliResult = call_user_func_array([$aop,'excute'],[$request]); array( 'alipay_fund_trans_uni_transfer_response' => stdClass::__set_state(array( 'code' => '10000', 'msg' => 'Success', 'out_biz_no' => 'xxxxx', 'order_id' => 'xxxxx', 'pay_fund_order_id' => 'xxxxxxxx', 'status' => 'SUCCESS', 'trans_date' => '2019-08-21 00:00:00', )), 'sign' => 'ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE', )
四,注意事项
1,支付宝红包要用公钥证书,类似微信红包有证书文件需要上传到服务器。
2,PHP SDK要用 AopCertClient 申明
完结,更多关注支付宝官方文档,不过文档里的示例可以好久没有更新了,证书那块有点问题。