欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现单笔转账到支付宝功能

程序员文章站 2023-12-06 09:20:04
本文实例为大家分享了php实现单笔转账到支付宝的具体代码,供大家参考,具体内容如下 1.首先 去蚂蚁金服签约 单笔转账到支付宝 官方  2.需要的配置信息...

本文实例为大家分享了php实现单笔转账到支付宝的具体代码,供大家参考,具体内容如下

1.首先 去蚂蚁金服签约 单笔转账到支付宝

官方 

2.需要的配置信息

1).应用appid

php实现单笔转账到支付宝功能

2).生成密钥

根据文档步骤生成

php实现单笔转账到支付宝功能

上传这里的 应用公钥

php实现单笔转账到支付宝功能

php实现单笔转账到支付宝功能

3.下载官方sdk 然后集成到自己项目

服务端sdk

官方实例

//实例化客户端
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实现单笔转账到支付宝功能

我的代码

<?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;
 }
}
?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。