前言
一、下载SDK
二、配置
三、实际操作
1.引入SDK
2.代码参考
四、回调
总结
官网文档:
一、SDK下载
下载语言对应的SDK - 这里我下载PHP
创建支付对象
二、SDK配置
1、将SDK完整文件放入项目 extend 目录下(Demo中有参考文件,生产使用AdapaySdk目录)

{
"*************DO NOT CHANGE CONTENT*************":"",
// 支付后台有
"api_key_live":"api_live_5a32a9c",
"api_key_test":"api_test_ed99fa4",
// 生成的证书商户的私钥
"rsa_private_key":"MIICdqf9b8hJ3iTsbbTw=="
}
2.封装后的使用方法
$AdaPayService = app()->make(AdaPayService::class);
$notify_url = sys_config('site_url') . '/api/pay/notify/adapay';
$jsConfig = $AdaPayService->mpAdaPayments($rechangeData['order_id'], $rechangeData['rechange_amount'], '用户充值', $openid, $notify_url, 'chess_recharge');
return $jsConfig;
需要在 class PayController 类文件中 添加回调处理方法

需要在 class PayNotifyServices 类文件中 添加支付成功回调
public function wechatChessRecharge(string $order_id = null, string $trade_no = null)
{
try {
$rechangeLogServices = app()->make(RechangeLogServices::class);
if ($rechangeLogServices->getOne(['order_id' => $order_id, 'status' => 1])) return true;
return $rechangeLogServices->rechangeSuccess($order_id, ['trade_no' => $trade_no]);
} catch (\Exception $e) {
return false;
}
}
封装的支付类
<?php
namespace crmeb\services;
use app\services\pay\PayNotifyServices;
use crmeb\utils\Hook;
use think\facade\Log;
/**
* Class AdaPayService
* @package crmeb\services
*/
class AdaPayService
{
/**
* 配置
* @var array
*/
private $config = [
'app_id' => 'app_a12312',
];
/**
* AliPayService constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
require_once "./extend/adapay_sdk_php/AdapaySdk/init.php";
require_once "./extend/adapay_sdk_php/AdapaySdk/config.php";
}
//小程序支付
public function mpAdaPayments($order_no, $money, $title, $open_id, $notify_url, $description)
{
$payment = new \AdaPaySdk\Payment();
# 支付设置
$payment_params = array(
'app_id' => $this->config['app_id'],
'order_no' => $order_no,
'pay_channel' => 'wx_lite',
'time_expire' => date("YmdHis", time() + 86400),
'pay_amt' => $money,
'goods_title' => $title,
'goods_desc' => $title,
'description' => $description,
'notify_url' => $notify_url,
'expend' => [
'open_id' => $open_id,
]
);
# 发起支付
$payment->create($payment_params);
# 对支付结果进行处理
if ($payment->isError()) {
//失败处理
throw new \Exception($payment->result);
} else {
$res = json_decode($payment->result['expend']['pay_info'], true);
$res['timestamp'] = $res['timeStamp'];//有大小写问题
return $res;
}
}
/**
* 支付异步回调
* @return string
*/
public static function handleNotify()
{
$adapay_tools = new \AdaPaySdk\AdapayTools();
$post_data = json_decode($_POST['data'], 1);
$post_data_str = json_encode($post_data, JSON_UNESCAPED_UNICODE);
$post_sign_str = isset($_POST['sign']) ? $_POST['sign'] : '';
// 此处只是个示例 需要测试请去掉注释
//$post_data_str = "{\"app_id\":\"app_fe1ec54d-e7cd-432a-a994-c12c3d8295f8\",\"created_time\":\"20201115182858\",\"end_time\":\"20201115182904\",\"expend\":{\"bank_type\":\"OTHERS\",\"open_id\":\"o8jhotwaUEffs1fyWE5O3N4HWvbk\",\"sub_open_id\":\"o4WGIxA59TYBzEKdwz_s6actNIYY\"},\"id\":\"002112020111518285710173995928213929984\",\"order_no\":\"SH20201115182857625624\",\"out_trans_id\":\"4200000839202011155051561044\",\"party_order_id\":\"02212011156653808201465\",\"pay_amt\":\"0.01\",\"pay_channel\":\"wx_pub\",\"status\":\"succeeded\"}";
//
//$sign_flag = $adapay_tools->verifySign($post_data_str,"YXOWP5pyL38cZvXbVTyr4Lp9tpr2IzYmc5+EXuNofMTPPlCMfgXX4aBHT8QhxmKMYe95TBklWrM6IAdSLqIBXyc7CYnEYh0o54QHH4H\/yKy5yiOqFCbcHAHPhtJPU28rj+dHbG7YG\/4Qk5psFoBuOTP99ACizLy\/uiILYY3UhJk=");
$post_data_str = "{\"app_id\":\"app_55dcbd98-988e-45d4-aafd-93e0833f2e64\",\"created_time\":\"20210827154320\",\"description\":\"description\",\"end_time\":\"20210827154414\",\"expend\":{\"sub_open_id\":\"2088002620020732\"},\"fee_amt\":\"0.01\",\"id\":\"002112021082715432010277234787138306048\",\"order_no\":\"C2021112799100519\",\"out_trans_id\":\"2021082722001420731428147993\",\"party_order_id\":\"02212108275660018000697\",\"pay_amt\":\"1.00\",\"pay_channel\":\"alipay\",\"status\":\"succeeded\"}";
$post_sign_str = "JDRZDnB+oKorcWjEsvvzDX3HI7dCfnxQ+/l780oFTG9MO6gGmCR6MiovfXLgpmlYyoCuHVjD/5cAVfJWppr+tgg3rzQFR+3O42ZXZxctTVJXp2fVszuYeSzipKFE60Jo3aavhgkt/5Hres77u353UXMkRxUHG/hMTIwPoxg789A=";
try {
if (isset($post_data['type']) && $post_data['type'] == 'payment.succeeded') {
# 先校验签名和返回的数据的签名的数据是否一致
$sign_flag = $adapay_tools->verifySign($post_data_str, $post_sign_str);
if ($sign_flag) {
// var_dump("签名验证通过");
# 业务方自己的逻辑
return (new Hook(PayNotifyServices::class, 'adapay'))
->listen($post_data['description'], $post_data['expend']['order_no'], $post_data['expend']['out_trans_id']);
} else {
// var_dump("签名验证失败");
# 业务方自己的逻辑
}
}
} catch (\Exception $e) {
Log::error('adapay回调异常,失败原因:' . $e->getMessage());
}
return false;
}
}