PHP是如何实现单笔转账到支付宝,参考下步骤
2019-08-09 05:49:10来源:编程学习网 阅读 ()
本文实例为大家分享了php实现单笔转账到支付宝的具体代码,供大家参考,具体内容如下:
1.首先 去蚂蚁金服签约 单笔转账到支付宝
官方api文档
https://docs.open.alipay.com/309
2.需要的配置信息
1).应用appid
2).生成密钥
文档地址
https://docs.open.alipay.com/291/106130
根据文档步骤生成
上传这里的 应用公钥
3.下载官方sdk 然后集成到自己项目
服务端SDK
https://docs.open.alipay.com/54/103419/
官方实例
//实例化客户端 AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数 //此次只是参数展示,未进行字符串转义,实际情况下请转义 request.setBizContent(" {" + " \"primary_industry_name\":\"IT科技/IT软件与服务\"," + " \"primary_industry_code\":\"10001/20102\"," + " \"secondary_industry_code\":\"10001/20102\"," + " \"secondary_industry_name\":\"IT科技/IT软件与服务\"" + " }"); AlipayOpenPublicTemplateMessageIndustryModifyResponse response = alipayClient.execute(request); //调用成功,则处理业务逻辑 if(response.isSuccess()){ //..... }
整体代码,需要的可以试试的
<?php /** * create by 适可而止 * create time 2018/4/8 */ namespace Org\Util; class AlipayTransfer{ private $appId = 'appid'; private $rsaPrivateKey = '私钥'; private $alipayrsaPublicKey = "支付宝公钥"; private $payer_name = "xx科技"; private $aop; public function __construct() { $g_alipay = C('ALIPAY_CONFIG'); $this->appId = $g_alipay['APPID'];//appid $this->rsaPrivateKey = $g_alipay['rsaPrivateKey']; //私钥 $this->alipayrsaPublicKey=$g_alipay['rsaPublicKey'];//支付宝公钥 //引入单笔转账sdk Vendor('Alipayaop.AopSdk'); } public function init_aop_config() { $this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $this->aop->appId = $this->appId; $this->aop->rsaPrivateKey = $this->rsaPrivateKey; $this->aop->alipayrsaPublicKey=$this->alipayrsaPublicKey; $this->aop->apiVersion = '1.0'; $this->aop->signType = 'RSA2'; $this->aop->postCharset='UTF-8'; $this->aop->format='json'; } /** * 单笔转账接口 * @param $order_number 订单号 * @param $pay_no 转账账号 * @param $pay_name 转账用户名 * @param $amount 转账金额 * @param $memo 备注 */ public function transfer($order_number,$pay_no,$pay_name,$amount,$memo) { //存入转账日志 $this->transferLog($order_number,$pay_no,$pay_name,$amount); $this->aop = new \AopClient (); //配置参数 $this->init_aop_config(); //导入请求 $request = new \AlipayFundTransToaccountTransferRequest (); $request->setBizContent("{" . "\"out_biz_no\":\"".$order_number."\"," .//商户生成订单号 "\"payee_type\":\"ALIPAY_LOGONID\"," .//收款方支付宝账号类型 "\"payee_account\":\"".$pay_no."\"," .//收款方账号 "\"amount\":\"".$amount."\"," .//总金额 "\"payer_show_name\":\"".$this->payer_name."\"," .//付款方账户 "\"payee_real_name\":\"".$pay_name."\"," .//收款方姓名 "\"remark\":\"".$memo."\"" .//转账备注 "}"); $result = $this->aop->execute ( $request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $resultSubMsg = $result->$responseNode->sub_msg; //修改转账日志 $this->edit_transferLog($order_number,$resultCode,$resultSubMsg); if(!empty($resultCode)&&$resultCode == 10000){ return true; } else { return false; } } /** * 存取日志 */ private function transferLog($order_number,$pay_no,$pay_name,$amount) { $data['order_number'] = $order_number; $data['pay_no'] = $pay_no; $data['pay_name'] = $pay_name; $data['amount'] = $amount; $data['create_time'] = time(); M('AlipayTransferLog')->add($data); } /** * 修改日志 */ private function edit_transferLog($order_number,$result_code,$sub_msg) { $model = D("AlipayTransferLog"); $where['order_number'] = $order_number; $result = $model->where($where)->order('create_time desc')->find(); if ($result_code == 10000) { $result['status'] = 1; $sub_msg = 'success'; } else { $result['status'] = 2; } $result['memo'] = $sub_msg; $result['update_time'] = time(); M('AlipayTransferLog')->save($result); } /** * 查单接口 */ public function query($order_number) { $this->aop = new \AopClient (); //配置参数 $this->init_aop_config(); $request = new \AlipayFundTransOrderQueryRequest (); $request->setBizContent("{" . "\"out_biz_no\":\"".$order_number."\"" . " }"); $result = $this->aop->execute ( $request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if(!empty($resultCode)&&$resultCode == 10000){ $res_arr['code'] = '00'; $res_arr['data'] = $result; } else { $res_arr['code'] = '-1'; } return $res_arr; } } ?>
原文链接:http://www.phpxs.com/post/6439/
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PHP访问MySQL查询超时怎么办 2020-03-09
- mysql7.x如何单独安装mysql 2020-02-27
- linux下如何用源码安装mysql5.6.20 2020-02-27
- SQL如何查询语句优化 2020-02-26
- MySQL如何存储IP地址 2019-10-25
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash