322 lines
13 KiB
PHP
322 lines
13 KiB
PHP
<?php
|
||
namespace app\common\server\platform;
|
||
|
||
use Alipay\EasySDK\Kernel\Factory;
|
||
use Alipay\EasySDK\Kernel\Config;
|
||
use app\common\model\User;
|
||
use think\App;
|
||
use think\facade\Db;
|
||
use think\facade\Log;
|
||
use app\common\model\Order;
|
||
use app\common\model\OrderNotify;
|
||
use app\common\helper\EnvHelper;
|
||
/**
|
||
* 多端平台支付抽象基类
|
||
*/
|
||
class H5Platform extends BasePlatform
|
||
{
|
||
public function __construct()
|
||
{
|
||
parent::__construct();
|
||
$this->code = 'WEB_H5';
|
||
$this->env = 'h5';
|
||
Factory::setOptions($this->getOptions());
|
||
}
|
||
/**
|
||
* 获取系统配置
|
||
* @return void
|
||
*/
|
||
public function get_config(): array
|
||
{
|
||
return ['isWebPay' => false];
|
||
}
|
||
|
||
/**
|
||
* Summary of pay
|
||
* @param array $data
|
||
* @throws \Exception
|
||
* @return array|array{data: array, status: int}
|
||
*/
|
||
public function pay(array $data): array
|
||
{
|
||
// echo "H5支付:{$amount}分,订单号:{$orderId}\n";
|
||
// 实际项目中这里调用支付SDK
|
||
|
||
try {
|
||
$data += ['user' => null, 'price' => 0, 'title' => '', 'attach' => 'order_wxs', 'pre' => 'MH_'];
|
||
[
|
||
'user' => $user,
|
||
'price' => $price,
|
||
'title' => $title,
|
||
'attach' => $attach,
|
||
'pre' => $pre,
|
||
'quitUrl' => $quitUrl,
|
||
'returnUrl' => $returnUrl
|
||
] = $data;
|
||
if ($user == null) {
|
||
//抛出异常
|
||
throw new \Exception('用户信息为空');
|
||
}
|
||
$project_prefix = "H5";
|
||
if ($price <= 0) {
|
||
//生成订单号,订单号为:ML_DRAYDMP02025018.... ML_ 前缀(固定3位),DRA 商户号(固定3位),YD 项目,固定2位,MP0 表示微信小程序支付
|
||
$order_no = create_order_no_new($pre, "MON", $project_prefix, "MP0");
|
||
return [
|
||
'status' => 1,
|
||
'data' => [
|
||
'order_no' => $order_no,
|
||
'res' => []
|
||
]
|
||
];
|
||
}
|
||
$title = mb_substr($title, 0, 30);
|
||
|
||
$prefix = "ZFA";//$this->GetPrefix();
|
||
//生成订单号,订单号为:ML_DRAYDMP02025018.... ML_ 前缀(固定3位),DRA 商户号(固定3位),YD 项目,固定2位,MP0 表示微信小程序支付
|
||
$order_no = create_order_no_new($pre, $prefix, $project_prefix, "ZFB");
|
||
$openid = $user['openid'];
|
||
$payment_type = 'zfbpay';
|
||
$order_type = $attach;
|
||
$user_id = $user ? $user['id'] : 0;
|
||
// 回调使用的随机数(与支付随机数分离)
|
||
$callback_nonce_str = $this->genRandomString();
|
||
// 生成新的支付通知URL
|
||
$notifyUrl = generatePayNotifyUrl($payment_type, $order_type, $user_id, $order_no, $callback_nonce_str);
|
||
$is_test = $user['istest'];
|
||
// if ($is_test == 2) {
|
||
// $price = 0.01;
|
||
// }
|
||
$returnUrl = urldecode($returnUrl);
|
||
//2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
|
||
$result = Factory::payment()
|
||
->wap()
|
||
->asyncNotify($notifyUrl)
|
||
->pay(
|
||
$title, // 支付标题
|
||
$order_no, // 商户订单号(唯一)
|
||
$price, // 金额
|
||
$quitUrl,// 取消地址
|
||
$returnUrl// 支付后返回地址
|
||
);
|
||
//3. 处理响应或异常
|
||
if (!empty($result->body)) {
|
||
// echo "调用成功" . PHP_EOL;
|
||
// 将通知URL和随机字符串保存到order_notify表中
|
||
Db::name('order_notify')->insert([
|
||
'order_no' => $order_no,
|
||
'notify_url' => $notifyUrl,
|
||
'nonce_str' => $callback_nonce_str,
|
||
'pay_time' => date('Y-m-d H:i:s'),
|
||
'pay_amount' => $price,
|
||
'status' => 0,
|
||
'retry_count' => 0,
|
||
'create_time' => date('Y-m-d H:i:s'),
|
||
'update_time' => date('Y-m-d H:i:s'),
|
||
'title' => $title,
|
||
]);
|
||
return [
|
||
'status' => 1,
|
||
'data' => [
|
||
'order_no' => $order_no,
|
||
'res' => $result->body
|
||
]
|
||
];
|
||
} else {
|
||
return ['status' => 0, 'msg' => '支付失败:' . $result];
|
||
}
|
||
} catch (\Exception $e) {
|
||
// echo "调用失败," . $e->getMessage() . PHP_EOL;
|
||
// ;
|
||
}
|
||
return ['status' => 0, 'msg' => '支付失败:'];
|
||
}
|
||
/**
|
||
* 发货
|
||
* @param mixed $openid
|
||
* @param mixed $access_token
|
||
* @param mixed $order_num
|
||
* @param mixed $title
|
||
* @return int
|
||
*/
|
||
public function post_order($user, $order_num): int
|
||
{
|
||
|
||
return 1;
|
||
}
|
||
|
||
public function verify($order_no, $data): bool
|
||
{
|
||
$result = Factory::payment()->common()->verifyNotify($data);
|
||
if ($result) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 生成URL链接
|
||
* @param int $userId 用户ID
|
||
* @return array
|
||
*/
|
||
public function generateUrlLink($userId): array
|
||
{
|
||
|
||
return ['status' => 1, 'data' => ""];
|
||
}
|
||
|
||
/**
|
||
* 获取手机号
|
||
* @param string $code 手机号获取凭证
|
||
* @return array
|
||
*/
|
||
public function getMobile($code = ''): array
|
||
{
|
||
|
||
|
||
return ['status' => 1, 'data' => ""];
|
||
}
|
||
|
||
/**
|
||
* 获取openid
|
||
* @param string $code 登录凭证
|
||
* @return array
|
||
*/
|
||
public function getOpenid($code): array
|
||
{
|
||
|
||
return ['status' => 1, 'data' => ""];
|
||
}
|
||
/**
|
||
* 客服发送消息
|
||
* @param mixed $user_id
|
||
* @param mixed $order_num
|
||
* @return array{status: int, data: array, msg: string}
|
||
*/
|
||
public function sendCustomerServiceMessage($user, $order_num): array
|
||
{
|
||
$message = "用户ID:{$user['id']},订单号:{$order_num}";
|
||
return ['status' => 1, 'data' => $message];
|
||
}
|
||
|
||
public function getOptions()
|
||
{
|
||
$options = new Config();
|
||
$options->protocol = 'https';
|
||
$options->gatewayHost = 'openapi.alipay.com';
|
||
$options->signType = 'RSA2';
|
||
|
||
$options->appId = '2021005164623222';
|
||
|
||
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
||
$options->merchantPrivateKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCxWdAbbAdcsDJYCmilsMPCIOMVUwKJpfGZ9fWER054Rsv8SvAQgeKzQQfvqolRWFRbcgKVRHPVAIAGe4JvMYaeE93ty/XFI91I9xOwO+m7dGhmOSzeLGXqx/jiqhnYBjq2Kr76iegUtd18CcLV6I/mGyTdeV2bo8zBgr6fU3hnSm2iSz5fF/itIhEZr2/ZCMbZg56aqQIIYklSli8uoww7pXaEUztHB1FMU62rgRQQXJ7ynirN450ckbF2U9NUVST9gH6c8JWSmgCw13VwEeS2+oeuSPUm4HWvtwxpVF+thPNOFURDOQoMX8t7t08k4lZUt3IgFEXqu1gAbz/B3uZRAgMBAAECggEBAIwQ4aH2pK3YXjpYJiPNwh1cedwKjjOGApTUww2wf1HQoCPmT0jKO+/USV9IMWE0QJNkLJQU1uJ/nZ0BCJem5qmMCTdYYQdMduS8dik2eDdbDWTGfSAOsoNFYdQqAXUyHkZm5iParJdGtuDSmLT2EkfqYigkRQC50el2x/PvKWVw99fizTSSuLaGx1d2l/cbi1ioqAMt5TnTWEilWZsr4BCMq3Fzxmz8UsfkhnO3D/NLfe3pa11Wqz2xZY3Z0+IVzqao9FkpAn4GrlIAnOMhDIL4oyPNlTm3t1ffn0suiPCTk0k+fJa3LwT6Ti9JenlGWsrWv3HEKPvn4Bkcjn4GKh0CgYEA9S6q0u00p77obaI4TADHQ/BjM6/sNyxM8JnRKOyd6uL/SwP22gXzaaSk9btd3A7wxKMIGagOatav9XpQvhG+yW2/CLp4KeU34Kt1B9K9tWqayPm1ciwgVeqTIxAD3DV5U9KBJ6HyICHEkCmVIbT0M956OU/kFjNDoZ3l4bKLyP8CgYEAuSz9WD4eWGDFpoq4ceOfzfrjQIkViBNDWUkgGU1nQfpXEEr5ACLACCOnB908gmL9JTYLF8lY7iKVftX3LKUtbz1HNc2/9NHdOCtBnzWaBihMEbeD+J5dTDtQslw3+dD2SBuLD71d0KWCV7fD/K+L6B9y8ux4rl+eEtcuCBcvgK8CgYEA64e6KEggtihKBc+5PyHcSqKp2aRdSh8vhk5tRGARz+vijUCwqX7f95NnuHwWzWg42N040dTEjNPSUwH+gKhxW/UmeA8RkkWVaSmskihbf+R57ykaqiRDp+VhFWS0IJ3qAKEO23DSNRXbSxX3Ils/4HgPd/EMTltO0/noNOmdKScCgYBleQLnfTrRwU3uImxKdKrYjBhGABjddXjdj5XYZ7Mi05UsJ3llmqVSkH7yb0i+RpJj9saDZWGnNSpo08VcILPSFmlUUYhUF0C7i/Cs1wmtQB3XQ5I9BkMoCAUDiEdgTSL4hqdgm6uyL1BK0zfB20y2ia1K80x6eYk818+wvUvZnwKBgGl7rMTvH7iUzUyC8dunAylcwn/Yzp/4SEmkxfjKa+XWvt5kPPQ+Y8BUEg4VHT5frcJq82oZeoiGWrUw636gTb5m/3Awh5IO/SIODi+BenbYgQPhaR8sDOtctCnZaQe384+SwJ0yYFp/OHic3sAaMn/FPNiNpiIyfYU5oqYuRJv9';
|
||
|
||
// $options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
|
||
// $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
|
||
// $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';
|
||
|
||
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
|
||
$options->alipayPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl09lU/ezb0ZaNgmlOVp01ZruPdolQgTcV9a42vUv3tYImBGWMGuGF555YGV5yHLVUmHaF0fh6AYWhxOe9iZTlp20vKctkmSVBpjE1UJXlLgn/5L0nzy08+wFCvJ0v3D8/ZwJ4nryjx6rfKbzs+/Ub5X2XuMJj4IhficN/T9tJAWmn8tfccWVw+DGgouIbcCftuGvbOZOAmqzGHU0ZYZXNjCqKithZjO9/9exbA0/5NkETFZqJEDVi09WZ7OS8B0twAPqEnnmYCGb0t8Tn+g1qoWaA/uU3ysQrrQk+q2Xl/PwOT07pCkAgFg4akUQ/poWA9yx2kDnq+3NJxiE0PoIrQIDAQAB';
|
||
//可设置异步通知接收服务地址(可选)
|
||
//如果需要使用文件上传接口,请不要设置该参数
|
||
$options->notifyUrl = "";
|
||
|
||
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
|
||
// $options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
|
||
return $options;
|
||
}
|
||
|
||
/**
|
||
* 根据订单号创建订单
|
||
* @param string $order_num 订单号
|
||
* @return array{status: int, data: array, msg: string}
|
||
*/
|
||
public function createWebPayOrderNotify($order_num)
|
||
{
|
||
// echo "H5支付:{$amount}分,订单号:{$orderId}\n";
|
||
// 实际项目中这里调用支付SDK
|
||
try {
|
||
$order_notify = OrderNotify::where('order_no', $order_num)->find();
|
||
$user_id = 0;
|
||
$extend = [];
|
||
$order_type = '';
|
||
if ($order_notify == null) {
|
||
return ['status' => 0, 'msg' => '未找到订单'];
|
||
}
|
||
if ($order_notify['status'] == 2) {
|
||
return ['status' => 0, 'msg' => '订单已失效'];
|
||
}
|
||
if ($order_notify['status'] == 1) {
|
||
return ['status' => 0, 'msg' => '订单已支付'];
|
||
}
|
||
// $this->appid
|
||
$web_domain = EnvHelper::getWebDomain();
|
||
$returnUrl = $web_domain . '/pages/other/web-pay-success';
|
||
$quitUrl = $web_domain . '/pages/other/web-pay-order';
|
||
if ($order_notify) {
|
||
$title = $order_notify['title'];
|
||
$price = $order_notify['pay_amount'];
|
||
if ($order_notify['extend'] == '') {
|
||
return ['status' => 0, 'msg' => '订单号错误'];
|
||
}
|
||
$extend = json_decode($order_notify['extend'], true);
|
||
$user_id = $extend['userId'];
|
||
$order_type = $extend['orderType'];
|
||
}
|
||
if ($price <= 0) {
|
||
return ['status' => 0, 'msg' => '金额错误'];
|
||
}
|
||
$user = User::where('id', $user_id)->find();
|
||
if ($user == null) {
|
||
//抛出异常
|
||
throw new \Exception('用户信息为空');
|
||
}
|
||
$title = mb_substr($title, 0, 30);
|
||
$order_no = $order_num;
|
||
$payment_type = 'zfbpay';
|
||
// 回调使用的随机数(与支付随机数分离)
|
||
$callback_nonce_str = $this->genRandomString();
|
||
// 生成新的支付通知URL
|
||
$notifyUrl = generatePayNotifyUrl($payment_type, $order_type, $user_id, $order_no, $callback_nonce_str);
|
||
$is_env_test = EnvHelper::getIsTest();
|
||
if ($is_env_test) {
|
||
$is_test = $user['istest'];
|
||
if ($is_test == 2) {
|
||
$price = 0.01;
|
||
}
|
||
}
|
||
//2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
|
||
$result = Factory::payment()
|
||
->wap()
|
||
->asyncNotify($notifyUrl)
|
||
->pay(
|
||
$title, // 支付标题
|
||
$order_no, // 商户订单号(唯一)
|
||
$price, // 金额
|
||
$quitUrl,// 取消地址
|
||
$returnUrl// 支付后返回地址
|
||
);
|
||
//3. 处理响应或异常
|
||
if (!empty($result->body)) {
|
||
// 将通知URL和随机字符串保存到order_notify表中
|
||
if ($order_notify) {
|
||
$order_notify->notify_url = $notifyUrl;
|
||
$order_notify->nonce_str = $callback_nonce_str;
|
||
}
|
||
$order_notify->save();
|
||
return [
|
||
'status' => 1,
|
||
'data' => [
|
||
'order_no' => $order_no,
|
||
'res' => $result->body
|
||
]
|
||
];
|
||
} else {
|
||
return ['status' => 0, 'msg' => '支付失败:' . $result];
|
||
}
|
||
} catch (\Exception $e) {
|
||
}
|
||
return ['status' => 0, 'msg' => '支付失败:'];
|
||
|
||
}
|
||
|
||
} |