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

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

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

2.需要的配置信息

1).应用appid

e59a05587cdf41c6407860b495cc9914.png

2).生成密钥

根据文档步骤生成

2f61894f9a27c6cd4e6e233def97d496.png

上传这里的 应用公钥

a5686369a7ecef91a39a35535539eef5.png

bf7589559740ca723f188ca66805c380.png

3.下载官方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()){

//.....

}

效果如下

fb7c5cd1a027099903a54f617e56a628.png

我的代码

/**

* create by 适可而止

* create time 2018/4/8

*/

namespace OrgUtil;

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;

}

}

?>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是支付宝实现单笔转账接口的代码示例(使用的是PHP语言): ```php <?php // 引入支付宝 SDK require_once 'aliyun-php-sdk-core/Config.php'; require_once 'aop/AopClient.php'; // 初始化 AopClient $aop = new AopClient(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = '<YOUR_APP_ID>'; $aop->rsaPrivateKey = '<YOUR_RSA_PRIVATE_KEY>'; $aop->alipayrsaPublicKey = '<ALIPAY_RSA_PUBLIC_KEY>'; $aop->format = 'json'; $aop->charset = 'UTF-8'; $aop->signType = 'RSA2'; // 创建请求对象 $request = new AlipayFundTransToaccountTransferRequest(); $request->setBizContent('{ "out_biz_no":"<YOUR_OUT_BIZ_NO>", // 商户转账唯一订单号 "payee_type":"ALIPAY_LOGONID", // 收款方账户类型 "payee_account":"<PAYEE_ACCOUNT>", // 收款方账户 "amount":"<TRANSFER_AMOUNT>", // 转账金额,单位:元 "payer_show_name":"<PAYER_SHOW_NAME>", // 付款方姓名 "payee_real_name":"<PAYEE_REAL_NAME>", // 收款方真实姓名 "remark":"<TRANSFER_REMARK>" // 转账备注 }'); // 发起 API 请求 $result = $aop->execute($request); // 处理 API 响应 if ($result && $result->alipay_fund_trans_toaccount_transfer_response->code == '10000') { // 转账成功 echo '转账成功,支付宝转账流水号:' . $result->alipay_fund_trans_toaccount_transfer_response->order_id; } else { // 转账失败 echo '转账失败,错误信息:' . $result->alipay_fund_trans_toaccount_transfer_response->sub_msg; } ?> ``` 需要注意的是,上述代码的 `<YOUR_APP_ID>`、`<YOUR_RSA_PRIVATE_KEY>`、`<ALIPAY_RSA_PUBLIC_KEY>`等变量需要替换成你自己的支付宝应用ID、RSA私钥、支付宝公钥等信息。同时,`<YOUR_OUT_BIZ_NO>`、`<PAYEE_ACCOUNT>`、`<TRANSFER_AMOUNT>`等参数也需要替换成你自己的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值