支付宝单笔转账demo (改配置直接用)
支付宝接口说明文档 https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer
需要注意的是:rsa的 公钥和私钥 是不要空格,换行 和 开始和前后的那个 -----BEGIN PUBLIC KEY----- -----END PUBLIC KEY----- 的,就是一行字符串 ,还有就是一定搞对了 对应的值。我在做的时候公钥错了, 把报错提示全部打开,然后根据它抛出的错误一点点打印,很快能帮助你找出原因。还有就是做好加log日志。养成好习惯方便以后出问题查找问题原因。
<?php /***调试的时候打开这个,可以显示出来抛出的错误,帮助快速解决问题***/ // ini_set("display_errors","on"); // error_reporting(E_ALL); require_once dirname(dirname(dirname(__FILE__)))."/plugin/alipay_transfer/AopSdk.php";//只要引用AopSdk.php这个文件即可,sdk不用动 $c = new AopClient; $c->gatewayUrl = "https://openapi.alipay.com/gateway.do"; $c->appId = "2017000000";//app_id 非 parter_id $c->rsaPrivateKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';//注意这是一行去头去尾的支付宝的rsa私钥 $c->format = "json"; $c->charset= "utf-8"; $c->signType= "RSA"; $c->alipayrsaPublicKey ='xxxxxxxxxxxxxxxxxxxxxxxxxx';//注意这是一行去头去尾的支付宝公钥 $request = new AlipayFundTransToaccountTransferRequest(); $arr['out_biz_no'] = "xxxxx"//商户转账唯一订单号 $arr['payee_type'] = "ALIPAY_LOGONID";//支付宝登录号,支持邮箱和手机号格式。 $arr['payee_account'] = "xxxxx@qq.com";//收款方账户。 $arr['amount'] = number_format("10",2);// 转账金额,单位:元。 只支持2位小数, $arr['payer_show_name'] = "xxx公司";// 转账记录里显示的名字 $arr['payee_real_name'] = "张三";//支付宝用户姓名, $arr['remark'] = "备注";// 备注, $request->setBizContent(json_encode($arr)); // echo "<pre>"; $response= $c->execute($request); // print_r($response->response);//调试过程有问题可以打印这个看看,根据抛出的错误位置一点点往上打印,基本能查出问题所在 $resultCode = $response->alipay_fund_trans_toaccount_transfer_response->code; $log['resultCode'] = $response->alipay_fund_trans_toaccount_transfer_response->code; $log['msg'] = $response->alipay_fund_trans_toaccount_transfer_response->msg; $log['sub_msg'] = $response->alipay_fund_trans_toaccount_transfer_response->sub_msg; $log['out_biz_no'] = $response->alipay_fund_trans_toaccount_transfer_response->out_biz_no; $log['order_id'] = $response->alipay_fund_trans_toaccount_transfer_response->order_id; $log['pay_date'] = $response->alipay_fund_trans_toaccount_transfer_response->pay_date; $log['sign'] = $response->sign; file_put_contents("/data/log/ali_transfer.log",json_encode($log)."\r\n",FILE_APPEND);//建议做日志,方便后期查找问题 if(!empty($resultCode)&&$resultCode == 10000){ echo "成功"; } else { $sub_msg = $response->alipay_fund_trans_toaccount_transfer_response->sub_msg; echo $sub_msg; //失败说明 }