更新支付

This commit is contained in:
mkm 2024-05-06 18:04:21 +08:00
parent 68f32cc0d0
commit a3134c0804
6 changed files with 564 additions and 141 deletions

View File

@ -5,6 +5,9 @@ namespace app\api\controller;
use app\common\enum\user\UserTerminalEnum;
use app\common\service\pay\WeChatPayService;
use app\Request;
use Error;
use support\Log;
/**
* 支付

View File

@ -17,7 +17,9 @@ namespace app\common\logic;
use app\common\enum\PayEnum;
use app\common\service\pay\WeChatPayService;
use Webman\Config;
use Yansongda\Pay\Pay;
use app\common\model\user\UserAuth;
/**
* 支付逻辑
@ -26,8 +28,18 @@ use app\common\service\pay\WeChatPayService;
*/
class PaymentLogic extends BaseLogic
{
/**
* 授权信息
* @var UserAuth|array|\think\Model
*/
protected $auth;
public function __construct()
{
$config = Config::get('payment');
Pay::config($config);
}
/**
* @notes 支付
* @param $payWay
@ -54,10 +66,26 @@ class PaymentLogic extends BaseLogic
}
switch ($payWay) {
case PayEnum::WECHAT_PAY:
$payService = (new WeChatPayService($terminal, $order['uid'] ?? null));
$order['pay_sn'] = $paySn;
$order['redirect_url'] = $redirectUrl;
$result = $payService->pay($from, $order);
// $payService = (new WeChatPayService($terminal, $order['uid'] ?? null));
// $order['pay_sn'] = $paySn;
// $order['redirect_url'] = $redirectUrl;
// $result = $payService->pay($from, $order);
$auth = UserAuth::where(['user_id' => $order['uid'], 'terminal' => $terminal])->findOrEmpty();
$order = [
'out_trade_no' => $paySn,
'description' => '商品',
'amount' => [
'total' => intval($order['actual'] * 100),
'currency' => 'CNY',
],
"payer" => [
"openid" => $auth['openid']??0
],
'attach' => $from
];
$result = Pay::wechat()->mini($order);
break;
default:
self::$error = '订单异常';

View File

@ -15,6 +15,8 @@ use app\Request;
use EasyWeChat\Pay\Application;
use EasyWeChat\Pay\Message;
use support\Log;
use Webman\Config;
use Yansongda\Pay\Pay;
/**
* 微信支付
@ -92,7 +94,6 @@ class WeChatPayService extends BasePayService
'pay_way' => PayEnum::WECHAT_PAY
];
} catch (\Exception $e) {
d($e);
$this->setError($e->getMessage());
return false;
}
@ -235,33 +236,36 @@ class WeChatPayService extends BasePayService
*/
public function notify()
{
Log::error(json_encode(Request()->post()));
$config = Config::get('payment');
Pay::config($config);
$result = Pay::wechat()->callback(request()->post());
d($result);
// Log::error(json_encode(Request()->post()));
// $server = $this->app->getServer();
// // 支付通知
// $server->handlePaid(function (Message $message) {
// if ($message['trade_state'] === 'SUCCESS') {
// $extra['transaction_id'] = $message['transaction_id'];
// $attach = $message['attach'];
// $message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
// switch ($attach) {
// case 'cashierclass':
// $order = Cashierclass::where(['number' => $message['out_trade_no']])->findOrEmpty();
// if($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
// return true;
// }
// PayNotifyLogic::handle('cashierclass', $message['out_trade_no'], $extra);
// break;
// }
// }
// return true;
// });
$server = $this->app->getServer();
// 支付通知
$server->handlePaid(function (Message $message) {
if ($message['trade_state'] === 'SUCCESS') {
$extra['transaction_id'] = $message['transaction_id'];
$attach = $message['attach'];
$message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
switch ($attach) {
case 'cashierclass':
$order = Cashierclass::where(['number' => $message['out_trade_no']])->findOrEmpty();
if($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
return true;
}
PayNotifyLogic::handle('cashierclass', $message['out_trade_no'], $extra);
break;
}
}
return true;
});
// 退款通知
$server->handleRefunded(function (Message $message) {
return true;
});
return $server->serve();
// // 退款通知
// $server->handleRefunded(function (Message $message) {
// return true;
// });
// return $server->serve();
}

View File

@ -44,10 +44,11 @@
"doctrine/annotations": "^1.14",
"illuminate/redis": "^10.22",
"symfony/cache": "^6.4",
"yansongda/pay": "^3.6",
"tinywan/jwt": "^1.9",
"next/var-dumper": "^0.1.0",
"w7corp/easywechat": "^6.8"
"w7corp/easywechat": "^6.8",
"yansongda/pay": "^3.6",
"hyperf/pimple": "~2.2.0"
},
"suggest": {
"ext-event": "For better performance. "

521
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "cb269cd47d94c38e34c51802a740203d",
"content-hash": "31c73aec55a41927680c578e301dcb28",
"packages": [
{
"name": "aliyuncs/oss-sdk-php",
@ -1336,6 +1336,360 @@
],
"time": "2023-12-03T19:50:20+00:00"
},
{
"name": "hyperf/context",
"version": "v3.1.15",
"source": {
"type": "git",
"url": "https://github.com/hyperf/context.git",
"reference": "ad913fd50eb5f738c038e172c120bc6956c0da69"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hyperf/context/zipball/ad913fd50eb5f738c038e172c120bc6956c0da69",
"reference": "ad913fd50eb5f738c038e172c120bc6956c0da69",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"hyperf/engine": "^2.0",
"php": ">=8.1"
},
"suggest": {
"swow/psr7-plus": "Required to use RequestContext and ResponseContext"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
}
},
"autoload": {
"psr-4": {
"Hyperf\\Context\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "A coroutine/application context library.",
"homepage": "https://hyperf.io",
"keywords": [
"Context",
"hyperf",
"php",
"swoole"
],
"support": {
"docs": "https://hyperf.wiki",
"issues": "https://github.com/hyperf/hyperf/issues",
"pull-request": "https://github.com/hyperf/hyperf/pulls",
"source": "https://github.com/hyperf/hyperf"
},
"funding": [
{
"url": "https://hyperf.wiki/#/zh-cn/donate",
"type": "custom"
},
{
"url": "https://opencollective.com/hyperf",
"type": "open_collective"
}
],
"time": "2024-03-23T11:28:51+00:00"
},
{
"name": "hyperf/contract",
"version": "v3.1.2",
"source": {
"type": "git",
"url": "https://github.com/hyperf/contract.git",
"reference": "f5379df6df65363d645506f373888372135ac0c6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hyperf/contract/zipball/f5379df6df65363d645506f373888372135ac0c6",
"reference": "f5379df6df65363d645506f373888372135ac0c6",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"php": ">=8.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
}
},
"autoload": {
"psr-4": {
"Hyperf\\Contract\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "The contracts of Hyperf.",
"homepage": "https://hyperf.io",
"keywords": [
"hyperf",
"php",
"swoole"
],
"support": {
"docs": "https://hyperf.wiki",
"issues": "https://github.com/hyperf/hyperf/issues",
"pull-request": "https://github.com/hyperf/hyperf/pulls",
"source": "https://github.com/hyperf/hyperf"
},
"funding": [
{
"url": "https://hyperf.wiki/#/zh-cn/donate",
"type": "custom"
},
{
"url": "https://opencollective.com/hyperf",
"type": "open_collective"
}
],
"time": "2023-12-11T03:14:01+00:00"
},
{
"name": "hyperf/engine",
"version": "v2.10.5",
"source": {
"type": "git",
"url": "https://github.com/hyperf/engine.git",
"reference": "b3e1a025e388815612815a0b08fc4f2439140676"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hyperf/engine/zipball/b3e1a025e388815612815a0b08fc4f2439140676",
"reference": "b3e1a025e388815612815a0b08fc4f2439140676",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"hyperf/engine-contract": "~1.9.0",
"php": ">=8.0"
},
"conflict": {
"ext-swoole": "<5.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"hyperf/guzzle": "^3.0",
"hyperf/http-message": "^3.0",
"mockery/mockery": "^1.5",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.4",
"swoole/ide-helper": "5.*"
},
"suggest": {
"ext-sockets": "*",
"ext-swoole": ">=5.0",
"hyperf/http-message": "Required to use ResponseEmitter.",
"psr/http-message": "Required to use WebSocket Frame."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.10-dev"
},
"hyperf": {
"config": "Hyperf\\Engine\\ConfigProvider"
}
},
"autoload": {
"files": [
"src/Functions.php"
],
"psr-4": {
"Hyperf\\Engine\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Coroutine engine provided by swoole.",
"keywords": [
"engine",
"hyperf",
"php",
"swoole"
],
"support": {
"issues": "https://github.com/hyperf/engine/issues",
"source": "https://github.com/hyperf/engine/tree/v2.10.5"
},
"funding": [
{
"url": "https://hyperf.wiki/#/zh-cn/donate",
"type": "custom"
},
{
"url": "https://opencollective.com/hyperf",
"type": "open_collective"
}
],
"time": "2024-03-12T06:06:19+00:00"
},
{
"name": "hyperf/engine-contract",
"version": "v1.9.1",
"source": {
"type": "git",
"url": "https://github.com/hyperf/engine-contract.git",
"reference": "fec2e45f35404b2e5b4c3eaf1b0dce67d60771eb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hyperf/engine-contract/zipball/fec2e45f35404b2e5b4c3eaf1b0dce67d60771eb",
"reference": "fec2e45f35404b2e5b4c3eaf1b0dce67d60771eb",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"php": ">=8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"mockery/mockery": "^1.0",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": ">=7.0",
"psr/http-message": "^1.0",
"swoole/ide-helper": "^4.5"
},
"suggest": {
"psr/http-message": "Required to use WebSocket Frame."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.9-dev"
}
},
"autoload": {
"psr-4": {
"Hyperf\\Engine\\Contract\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Contract for Coroutine Engine",
"keywords": [
"contract",
"coroutine",
"engine",
"hyperf",
"php"
],
"support": {
"issues": "https://github.com/hyperf/engine-contract/issues",
"source": "https://github.com/hyperf/engine-contract/tree/v1.9.1"
},
"funding": [
{
"url": "https://hyperf.wiki/#/zh-cn/donate",
"type": "custom"
},
{
"url": "https://opencollective.com/hyperf",
"type": "open_collective"
}
],
"time": "2023-12-15T07:37:14+00:00"
},
{
"name": "hyperf/pimple",
"version": "v2.2.2",
"source": {
"type": "git",
"url": "https://github.com/hyperf-cloud/pimple-integration.git",
"reference": "7bd07745c256b83679471c06ec2a11e901d37277"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hyperf-cloud/pimple-integration/zipball/7bd07745c256b83679471c06ec2a11e901d37277",
"reference": "7bd07745c256b83679471c06ec2a11e901d37277",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"hyperf/context": "^3.0",
"hyperf/contract": "^3.0",
"php": ">=8.0",
"pimple/pimple": "^3.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"hyperf/support": "^3.0",
"mockery/mockery": "^1.3",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": ">=7.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
},
"hyperf": {
"config": "Hyperf\\Pimple\\ConfigProvider"
}
},
"autoload": {
"psr-4": {
"Hyperf\\Pimple\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Pimple Container",
"keywords": [
"container",
"hyperf",
"php",
"psr11"
],
"support": {
"issues": "https://github.com/hyperf-cloud/pimple-integration/issues",
"source": "https://github.com/hyperf-cloud/pimple-integration/tree/v2.2.2"
},
"time": "2023-06-10T04:41:29+00:00"
},
{
"name": "illuminate/collections",
"version": "v10.48.4",
@ -2530,112 +2884,6 @@
],
"time": "2024-03-08T06:41:54+00:00"
},
{
"name": "overtrue/wechat",
"version": "6.7.4",
"source": {
"type": "git",
"url": "https://github.com/w7corp/easywechat.git",
"reference": "9cc301d21584a63b13ae21b85a0ea878fa6e4a4f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/w7corp/easywechat/zipball/9cc301d21584a63b13ae21b85a0ea878fa6e4a4f",
"reference": "9cc301d21584a63b13ae21b85a0ea878fa6e4a4f",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"ext-curl": "*",
"ext-fileinfo": "*",
"ext-libxml": "*",
"ext-openssl": "*",
"ext-simplexml": "*",
"ext-sodium": "*",
"monolog/monolog": "^2.2",
"nyholm/psr7": "^1.5",
"nyholm/psr7-server": "^1.0",
"overtrue/socialite": "^3.5|^4.0.1",
"php": ">=8.0.2",
"psr/http-client": "^1.0",
"psr/simple-cache": "^1.0|^2.0|^3.0",
"symfony/cache": "^5.4|^6.0",
"symfony/http-client": "^5.4|^6.0",
"symfony/http-foundation": "^5.4|^6.0",
"symfony/mime": "^5.4|^6.0",
"symfony/polyfill-php81": "^1.25",
"symfony/psr-http-message-bridge": "^2.1.2"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.8",
"friendsofphp/php-cs-fixer": "^3.5.0",
"jetbrains/phpstorm-attributes": "^1.0",
"mikey179/vfsstream": "^1.6",
"mockery/mockery": "^1.4.4",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.5",
"symfony/var-dumper": "^5.2"
},
"type": "library",
"extra": {
"hooks": {
"pre-commit": [
"composer check-style",
"composer phpstan",
"composer test"
],
"pre-push": [
"composer check-style"
],
"config": {
"stop-on-failure": [
"pre-commit",
"pre-push"
]
}
}
},
"autoload": {
"psr-4": {
"EasyWeChat\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "overtrue",
"email": "anzhengchao@gmail.com"
}
],
"description": "微信SDK",
"keywords": [
"easywechat",
"sdk",
"wechat",
"weixin",
"weixin-sdk"
],
"support": {
"issues": "https://github.com/w7corp/easywechat/issues",
"source": "https://github.com/w7corp/easywechat/tree/6.7.4"
},
"funding": [
{
"url": "https://github.com/overtrue",
"type": "github"
}
],
"abandoned": "w7corp/easywechat",
"time": "2022-08-29T09:32:29+00:00"
},
{
"name": "php-di/invoker",
"version": "2.3.4",
@ -3019,6 +3267,65 @@
],
"time": "2023-11-12T21:59:55+00:00"
},
{
"name": "pimple/pimple",
"version": "v3.5.0",
"source": {
"type": "git",
"url": "https://github.com/silexphp/Pimple.git",
"reference": "a94b3a4db7fb774b3d78dad2315ddc07629e1bed"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/silexphp/Pimple/zipball/a94b3a4db7fb774b3d78dad2315ddc07629e1bed",
"reference": "a94b3a4db7fb774b3d78dad2315ddc07629e1bed",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"php": ">=7.2.5",
"psr/container": "^1.1 || ^2.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.4@dev"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.4.x-dev"
}
},
"autoload": {
"psr-0": {
"Pimple": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "Pimple, a simple Dependency Injection Container",
"homepage": "https://pimple.symfony.com",
"keywords": [
"container",
"dependency injection"
],
"support": {
"source": "https://github.com/silexphp/Pimple/tree/v3.5.0"
},
"time": "2021-10-28T11:13:42+00:00"
},
{
"name": "psr/cache",
"version": "3.0.0",

80
config/payment.php Normal file
View File

@ -0,0 +1,80 @@
<?php
/**
* @desc 支付配置文件
* @author Tinywan(ShaoBo Wan)
* @date 2022/03/11 20:15
*/
return [
'alipay' => [
'default' => [
// 必填-支付宝分配的 app_id
'app_id' => '20160909004708941',
// 必填-应用私钥 字符串或路径
'app_secret_cert' => 'MIIEpAIBAAKCxxxxxxxxxxxxxxP4r3m4OUmD/+XDgCg==',
// 必填-应用公钥证书 路径
'app_public_cert_path' => base_path().'/payment/appCertPublicKey_2016090900470841.crt',
// 必填-支付宝公钥证书 路径
'alipay_public_cert_path' => base_path().'/payment/alipayCertPublicKey_RSA2.crt',
// 必填-支付宝根证书 路径
'alipay_root_cert_path' => base_path().'/payment/alipayRootCert.crt',
// 选填-同步回调地址
'return_url' => 'https://webman.tinywan.cn/payment/alipay-return',
// 选填-异步回调地址
'notify_url' => 'https://webman.tinywan.cn/payment/alipay-notify',
// 选填-服务商模式下的服务商 id当 mode 为 Pay::MODE_SERVICE 时使用该参数
'service_provider_id' => '',
// 选填-默认为正常模式。可选为: MODE_NORMAL, MODE_SANDBOX, MODE_SERVICE
'mode' => \Yansongda\Pay\Pay::MODE_SANDBOX,
]
],
'wechat' => [
'default' => [
// 必填-商户号,服务商模式下为服务商商户号
'mch_id' => '1654274867',
// 必填-商户秘钥
'mch_secret_key' => 'F6C2A57790C667607D8E8C0C00CCF9B6',
// 必填-商户私钥 字符串或路径
'mch_secret_cert' => getenv(runtime_path('/cart/c208f16ad08e6d2516e0b73e6d0ab8da.pem')),
// 必填-商户公钥证书路径
'mch_public_cert_path' =>getenv(runtime_path('/cart/82fdc3426e3aeb9e5df91a9547ac5ef3.pem')),
// 必填
'notify_url' => 'https://erp.lihaink.cn/api/pay/notifyMnp',
// 选填-公众号 的 app_id
'mp_app_id' => '',
// 选填-小程序 的 app_id
'mini_app_id' => 'wxdee751952c8c2027',
// 选填-app 的 app_id
'app_id' => '',
// 选填-合单 app_id
'combine_app_id' => '',
// 选填-合单商户号
'combine_mch_id' => '',
// 选填-服务商模式下,子公众号 的 app_id
'sub_mp_app_id' => '',
// 选填-服务商模式下,子 app 的 app_id
'sub_app_id' => '',
// 选填-服务商模式下,子小程序 的 app_id
'sub_mini_app_id' => '',
// 选填-服务商模式下子商户id
'sub_mch_id' => '',
// 选填-微信公钥证书路径, optional强烈建议 php-fpm 模式下配置此参数
'wechat_public_cert_path' => [
],
// 选填-默认为正常模式。可选为: MODE_NORMAL, MODE_SERVICE
'mode' => \Yansongda\Pay\Pay::MODE_SANDBOX,
]
],
'logger' => [
'enable' => false,
'file' => runtime_path().'/logs/alipay.log',
'level' => 'debug', // 建议生产环境等级调整为 info开发环境为 debug
'type' => 'single', // optional, 可选 daily.
'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
],
'http' => [ // optional
'timeout' => 5.0,
'connect_timeout' => 5.0,
// 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
],
'_force' => true,
];