314 lines
13 KiB
PHP
314 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;
|
||
use think\facade\Request;
|
||
/**
|
||
* 多端平台支付抽象基类
|
||
*/
|
||
class AppPlatform extends BasePlatform
|
||
{
|
||
public function __construct()
|
||
{
|
||
parent::__construct();
|
||
$this->code = 'APP_ANDROID';
|
||
$this->env = 'app';
|
||
Factory::setOptions($this->getOptions());
|
||
}
|
||
|
||
/**
|
||
* 获取系统配置
|
||
* @return void
|
||
*/
|
||
public function get_config(): array
|
||
{
|
||
// 获取订单编号
|
||
$version = Request::param('version', '1.0.0');
|
||
$versionNumber = (int) preg_replace('/[^0-9]/', '', $version);
|
||
$config = [
|
||
'isWebPay' => false,
|
||
'isCheck' => false,
|
||
'version' => '1.0.0',
|
||
];
|
||
$configVersion = (int) preg_replace('/[^0-9]/', '', $config['version']);
|
||
if ($versionNumber >= $configVersion) {
|
||
$config['isCheck'] = true;
|
||
}
|
||
return $config;
|
||
}
|
||
/**
|
||
* Summary of pay
|
||
* @param array $data
|
||
* @throws \Exception
|
||
* @return array|array{data: array, status: int}
|
||
*/
|
||
public function pay(array $data): array
|
||
{
|
||
|
||
|
||
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 = "AD";
|
||
if ($price <= 0) {
|
||
//生成订单号,订单号为:ML_DRAYDMP02025018.... ML_ 前缀(固定3位),DRA 商户号(固定3位),YD 项目,固定2位,MP0 表示微信小程序支付
|
||
$order_no = create_order_no_new($pre, "MON", $project_prefix, "APP");
|
||
return [
|
||
'status' => 1,
|
||
'data' => [
|
||
'order_no' => $order_no,
|
||
'res' => []
|
||
]
|
||
];
|
||
}
|
||
|
||
$prefix = "ZFA";//$this->GetPrefix();
|
||
$config = $this->get_config();
|
||
if ($config['isWebPay']) {
|
||
$domain = Request::domain();
|
||
// Request::param('return_url')
|
||
$host = parse_url($domain, PHP_URL_HOST);
|
||
$order_no = create_order_no_new($pre, "ZFA", 'H5', "ZFB");
|
||
$extend = [
|
||
'orderType' => $attach,
|
||
];
|
||
$extend_str = json_encode($extend, JSON_UNESCAPED_UNICODE);
|
||
// 将通知URL和随机字符串保存到order_notify表中
|
||
Db::name('order_notify')->insert([
|
||
'order_no' => $order_no,
|
||
'notify_url' => '',
|
||
'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'),
|
||
'extend' => $extend_str,
|
||
'title' => $title,
|
||
]);
|
||
return [
|
||
'status' => 1,
|
||
'data' => [
|
||
'order_no' => $order_no,
|
||
'res' => ['data' => ['order_num' => $order_no], 'requestPay' => $domain . '/api/send_web_pay_order', 'tips' => '']
|
||
]
|
||
];
|
||
}
|
||
$title = mb_substr($title, 0, 30);
|
||
|
||
|
||
//生成订单号,订单号为: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_env_test = EnvHelper::getIsTest();
|
||
if ($is_env_test) {
|
||
$is_test = $user['istest'];
|
||
if ($is_test == 2) {
|
||
$price = 0.01;
|
||
}
|
||
}
|
||
$returnUrl = urldecode($returnUrl);
|
||
//2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
|
||
$result = Factory::payment()
|
||
->app()
|
||
->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 $user_id
|
||
* @param mixed $order_num
|
||
* @return array{status: int, data: array, msg: string}
|
||
*/
|
||
public function sendCustomerServiceMessage($user, $order_num): array
|
||
{
|
||
// 获取订单编号
|
||
$return_url = Request::param('return_url', '');
|
||
|
||
|
||
$message = "用户ID:{$user['id']},订单号:{$order_num}";
|
||
$order_title = '';
|
||
$order_price = 0;
|
||
if ($user['openid'] == null) {
|
||
return ['status' => 0, 'msg' => '用户openid为空'];
|
||
}
|
||
$orderNotify = OrderNotify::where([
|
||
'order_no' => $order_num,
|
||
])->find();
|
||
$extend = [];
|
||
if ($orderNotify) {
|
||
if ($orderNotify['extend'] != '') {
|
||
$extend = json_decode($orderNotify['extend'], true);
|
||
}
|
||
$order_title = $orderNotify['title'];
|
||
$order_price = $orderNotify['pay_amount'];
|
||
}
|
||
if ($order_title == "") {
|
||
return ['status' => 0, 'msg' => '订单号错误'];
|
||
}
|
||
|
||
// $this->appid
|
||
$web_domain = EnvHelper::getWebDomain();
|
||
$extend['returnUrl'] = $return_url;
|
||
$extend['openId'] = $user['openid'];
|
||
$extend['userId'] = $user['id'];
|
||
$extend_str = json_encode($extend, JSON_UNESCAPED_UNICODE);
|
||
OrderNotify::where('order_no', $order_num)->update(['extend' => $extend_str]);
|
||
return ['status' => 1, 'data' => $web_domain . '/pages/other/web-pay-order?order_num=' . $order_num];
|
||
|
||
}
|
||
|
||
/**
|
||
* 发货
|
||
* @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' => ""];
|
||
}
|
||
|
||
|
||
|
||
public function getOptions()
|
||
{
|
||
$options = new Config();
|
||
$options->protocol = 'https';
|
||
$options->gatewayHost = 'openapi.alipay.com';
|
||
$options->signType = 'RSA2';
|
||
|
||
$options->appId = '2021005153608270';
|
||
|
||
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
||
$options->merchantPrivateKey = 'MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQDt4poXku4KIym89gds3U6nbzLUxky1x47G8cgloYdIObXDV8oDPiPVf7kaJGcVU5R3eU7myJHEkurlAnSzb40oyg2xV5qsHjjV4AO/9v5VPCQNl5qbL83PYC79eh7z4vGOwitSK3AjHilQJkeD8WNaX6D/UMZKz4KcXbcNbBjnu9FvwpiVwY6HZPxrTlR5QTxxuPWVUAbnMkBP9nLys0jMrIShns+PA4GD7Jhx32SUwMPwmjTmv93CR1KTgKe1I0WQgkCG2/5nyD2j+hm1SFC/5rnF0dElZbJcZxIXgvyyTEdbNuf2mzaVYpFZNSc6v/kQKCevL3sNPfHy5XWqFydxAgMBAAECggEBANyV6rN/cLH/xz1MmrgNQ1kpWOZK92j8ol3CaAjVDuAfe3enWVDGd24LYLZoGRqChUKAP3TreZfhcGVpcJPPFgND0YyoImoGIEfa0T9Zpp+dBAWClj/fBSaOFyS+8CLRR5NMY+VvGC3IUyDaTiiAVtO/p0f5O9a1M3URxaNxoGqISlMxpn/LIyWc1elpT2/RY0nkKQCXk2rav3yZZe9+td1ESHtKOFE4l9qPAP6H9AaE401qKhASa8vuLm1nUE35XqoGSxASLnckAq3Z+v6WnO2rli59DMiFFB45+XJxMSwumdfpKxsI/OO/VXBwpgNaLGXBO47op8SxqswYZiVRyVECgYEA9xK1SmCbGjyUJAiJI77zF3SQotz39PK2pe1l+5YUXTGRx5D9iOR9o2zMfHwlgGpPWB9XJU23ZOjPhNsz0pGXTfEelp0VJtmbX1SqKjGhAZ+sJxMmY5iP72MFfu+aIsfPnWPh3G9s2fDGxlmoXnCKbptrgM7T3TRsRPPx+7zvZ+0CgYEA9nrpGIipOn0n2lxHH1eSlLVYMvRT/gt1EWePlWXK/7D71iCL09SdbY1DKvZxRwcd+c3LBuTI+zWJiW+8b78B7Z/mMntoQokbB3EMM/C3voVfsuCOSea5ARDNbgQ0pRFw9Qov/dLlliDdYrSKiYJYwjnfXKE+qTX9dXNOAK2cBRUCgYEA7mAwZSg7vN6BlxpdJg3O/+xIt7k1yjB6JDCdWlR8JUXz/nVXB8JbrVcFG32zuOfY0Y67R5RpwoQT43yRzTEGp/5gorO/epIso5dN7hOf4a8qKzEAssq45B/HZ6bIMZJSLun1OfaPMN5rCWfrV+KAzSJKYCYsppkzdHtgFp885CkCgYEA9Kf0D+I2+FOa52iJQFcQrIOE1K8pYBXHUktVfpnX8g2fLGCJ6u40hbWeYlrU/gfWfUsEqAcYaCIwLze198XFCDWbrahJSSIGrlBMKJJcEMUaxNeY5UobgS9Iele6Wc8CLHi8QlrAgVCF75/9k5jKuZ/wUmXLaPKqb5bQamPpZjECgYEAuSTyMwJmOTLJ/LRGCzVKOUEetfjQU+RhDvp7K/9zAuhUx9iY869lzIdCV9gzrk92+inhmTxXBxxM+zcml7VfHxbsNoG0jnlMtC7f4jdzg1qMhk3vPlRDIC+TIB/kaNu+lAScg3BzYSniKVBu9zwZ1OIlVWFwAKNvBGXtRSdUN7k=';
|
||
|
||
// $options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
|
||
// $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
|
||
// $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';
|
||
|
||
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
|
||
$options->alipayPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlAGEC6+urK0+aC3HGy1KqxqK2lD8G+0/oNN3AcZpsXrztMNHbSpfw9zdea1jldF5HZsFD5JH/mb3ngMaepry/DnEyTpxqVfeBfWDhXgM+isQqm8mc/SpX4W1uh0edXh984U0YHNabg+HKdc4EdXpIW/0bRBUjmDQJPLDl6vjn7aFU1KTFSo06+e5PsVAwZuYZTEsgqn05zZxh+nccLVsjO64Kywv6NbbZRFRG3Dxequ35gRHtZOUvzt5o8bzpEnH0/8lNJqVtfpu41tFxinSdH8R4WFa8YKPqLqRnxCw7o5pTuznS2akOhPQ7ElO5tvotgFwOfVUGc/7yXxizGVgMwIDAQAB';
|
||
//可设置异步通知接收服务地址(可选)
|
||
//如果需要使用文件上传接口,请不要设置该参数
|
||
$options->notifyUrl = "";
|
||
|
||
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
|
||
// $options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
|
||
return $options;
|
||
}
|
||
|
||
} |