支付宝支付/付款接口接入

准备

沙箱

使用沙箱调试:https://openhome.alipay.com/platform/appDaily.htm

生成秘钥

参照:https://docs.open.alipay.com/58/103242

我1024改成了2048.。。。后面提交的时候竟然或要2048位。。。真坑而且还要去掉开头和结尾的标志,怀疑是不是没有同步更新文档

huanl@huanl-CN15S:~/soft$ openssl
OpenSSL> genrsa -out rsa_private_key.pem 2048
OpenSSL> rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
writing RSA key
OpenSSL> exit
huanl@huanl-CN15S:~/soft$ ls
sa_public_key.pem  rsa_private_key.pem  rsa_private_key_pkcs8.pem

提交

如图所示。。。。
《支付宝支付/付款接口接入》

网站接入

参考文档:
https://docs.open.alipay.com/270/105899/
https://docs.open.alipay.com/203/107090/
https://docs.open.alipay.com/54/103419

先下载sdk导入到我们的项目中(感觉我还是喜欢自己调用接口的那种,sdk虽然很全面,但是也感觉显得臃肿)

调起支付

代码如下:还有一些参数可以加入
具体可以看这里的请求参数:https://docs.open.alipay.com/203/107090/#s1

require_once 'lib/AopSdk.php';
$config = require 'config.php';

$AliPay = new AopClient();
$AliPay->appId = $config['appid'];
$AliPay->gatewayUrl = $config['gateway'];

$AliPay->rsaPrivateKey = $config['rsa_private_key'];
$AliPay->format = "json";
$AliPay->charset = "utf-8";
$AliPay->signType = "RSA2";
$AliPay->alipayrsaPublicKey = $config['ali_public_key'];
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
$request = new AlipayTradePagePayRequest();
$request->setReturnUrl("http://{$config['domain']}/return.php");
$request->setNotifyUrl("http://{$config['domain']}/notify.php");
//SDK已经封装掉了公共参数,这里只需要传入业务参数
//此次只是参数展示,未进行字符串转义,实际情况下请转义
$request->setBizContent("{" .
    "    \"body\":\"订单参数\"," .
    "    \"subject\":\"商品名\"," .
    "    \"out_trade_no\":\"" . time() . rand(10000, 99999) . "\"," .//商家订单号
    "    \"timeout_express\":\"90m\"," .//有效时间
    "    \"total_amount\":0.01," .//订单金额
    "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"" .//这个参数根据手机还是电脑改变
    "  }");
try {
    $response = $AliPay->pageExecute($request);
} catch (Exception $e) {
    var_dump($e);
}
//header('content-type: text/plain');
echo $response;

异步回调

https://docs.open.alipay.com/203/105286/

可以通过一些参数来判断支付是否成功,通过out_trade_no可以判断清单发起者之类的,不多写了,可以参照我的微信支付对接

企业付款

这个主要就是可能用户提现时候用到的,直接通过接口打款过去
https://docs.open.alipay.com/309
https://docs.open.alipay.com/309/106236/

<?php
/**
 *============================
 * author:Farmer
 * time:18-10-8 下午7:44
 * blog:blog.icodef.com
 * function:
 *============================
 */

require_once 'lib/AopSdk.php';
$config = require 'config.php';


$AliPay = new AopClient();
$AliPay->appId = $config['appid'];
$AliPay->gatewayUrl = $config['gateway'];

$AliPay->rsaPrivateKey = $config['rsa_private_key'];
$AliPay->format = "json";
$AliPay->charset = "utf-8";
$AliPay->signType = "RSA2";
$AliPay->alipayrsaPublicKey = $config['ali_public_key'];


$request = new AlipayFundTransToaccountTransferRequest();
$request->setReturnUrl("http://{$config['domain']}/return.php");
$request->setNotifyUrl("http://{$config['domain']}/notify.php");
//SDK已经封装掉了公共参数,这里只需要传入业务参数
//此次只是参数展示,未进行字符串转义,实际情况下请转义
$request->setBizContent("{" .
    "    \"out_biz_no\":\"" . (time() . rand(1000, 9999)) . "\"," .
    "    \"payee_type\":\"ALIPAY_LOGONID\"," .
    "    \"payee_account\":\"" . $config['payee_account'] . "\"," .
    "    \"amount\":\"10.00\"," .
    "    \"payer_show_name\":\"订单名称\"," .
    "    \"payee_real_name\":\"沙箱环境\"," .//对方用户名
    "    \"remark\":\"转账备注\"," .
    "  }");
try {
    $response = $AliPay->execute($request);
} catch (Exception $e) {
    var_dump($e);
}
//header('content-type: text/plain');
var_dump($response);//可以判断是否成功
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.