增加支付通道配置

This commit is contained in:
Anyon 2020-12-14 14:55:05 +08:00
parent ab12bb1429
commit fd9d7d6312
5 changed files with 55 additions and 33 deletions

View File

@ -16,16 +16,17 @@ class Notify extends Controller
/** /**
* 微信支付通知 * 微信支付通知
* @param string $scene 支付场景 * @param string $scene 支付场景
* @param string $type 支付通道
* @return string * @return string
* @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\InvalidResponseException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function wxpay(string $scene = 'order'): string public function wxpay(string $scene = 'order', string $type = ''): string
{ {
if (strtolower($scene) === 'order') { if (strtolower($scene) === 'order') {
return WechatPaymentService::instance()->notify(); return WechatPaymentService::instance()->notify($type);
} else { } else {
return 'success'; return 'success';
} }
@ -34,15 +35,16 @@ class Notify extends Controller
/** /**
* 汇聚支付通知 * 汇聚支付通知
* @param string $scene 支付场景 * @param string $scene 支付场景
* @param string $type 支付通道
* @return string * @return string
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function joinpay(string $scene = 'order'): string public function joinpay(string $scene = 'order', string $type = ''): string
{ {
if (strtolower($scene) === 'order') { if (strtolower($scene) === 'order') {
return JoinPaymentService::instance()->notify(); return JoinPaymentService::instance()->notify($type);
} else { } else {
return 'success'; return 'success';
} }

View File

@ -22,7 +22,7 @@ class Config extends Auth
{ {
$types = []; $types = [];
foreach (PaymentService::TYPES as $type => $arr) { foreach (PaymentService::TYPES as $type => $arr) {
if (isset($arr['auth']) && in_array($this->type, $arr['auth'])) { if (isset($arr['bind']) && in_array($this->type, $arr['bind'])) {
$types[] = $type; $types[] = $type;
} }
} }

View File

@ -19,21 +19,36 @@ abstract class PaymentService extends Service
const PAYMENT_JOINPAY_XCX = 'joinpay_xcx'; const PAYMENT_JOINPAY_XCX = 'joinpay_xcx';
// 微信支付通道 // 微信支付通道
const PAYMENT_WECHAT_MWEB = 'wechat_mweb';
const PAYMENT_WECHAT_JSAPI = 'wechat_jsapi'; const PAYMENT_WECHAT_JSAPI = 'wechat_jsapi';
const PAYMENT_WECHAT_NATIVE = 'wechat_native';
// 支付通道描述 // 支付通道描述
const TYPES = [ const TYPES = [
PaymentService::PAYMENT_JOINPAY_XCX => [ PaymentService::PAYMENT_JOINPAY_XCX => [
'name' => '汇聚小程序支付', 'type' => 'WEIXIN_XCX',
'auth' => [UserService::APITYPE_WXAPP], 'name' => '汇聚小程序JSAPI支付',
'bind' => [UserService::APITYPE_WXAPP],
], ],
PaymentService::PAYMENT_JOINPAY_GZH => [ PaymentService::PAYMENT_JOINPAY_GZH => [
'name' => '汇聚服务号支付', 'type' => 'WEIXIN_GZH',
'auth' => [UserService::APITYPE_WECHAT], 'name' => '汇聚服务号JSAPI支付',
'bind' => [UserService::APITYPE_WECHAT],
], ],
PaymentService::PAYMENT_WECHAT_JSAPI => [ PaymentService::PAYMENT_WECHAT_MWEB => [
'name' => '微信商户支付', 'type' => 'MWEB',
'auth' => [UserService::APITYPE_WXAPP, UserService::APITYPE_WECHAT], 'name' => '微信商户H5支付',
'bind' => [UserService::APITYPE_WAP],
],
PaymentService::PAYMENT_WECHAT_NATIVE => [
'type' => 'NATIVE',
'name' => '微信商户NATIVE支付',
'bind' => [UserService::APITYPE_WEB],
],
PaymentService::PAYMENT_WECHAT_JSAPI => [
'type' => 'JSAPI',
'name' => '微信商户JSAPI支付',
'bind' => [UserService::APITYPE_WXAPP, UserService::APITYPE_WECHAT],
], ],
]; ];
@ -123,9 +138,10 @@ abstract class PaymentService extends Service
/** /**
* 支付通知处理 * 支付通知处理
* @param string $type
* @return string * @return string
*/ */
abstract public function notify(): string; abstract public function notify(string $type = ''): string;
/** /**
* 订单主动查询 * 订单主动查询

View File

@ -68,12 +68,8 @@ class JoinPaymentService extends PaymentService
public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array
{ {
try { try {
$types = [ if (isset(static::TYPES[static::$type])) {
static::PAYMENT_JOINPAY_GZH => 'WEIXIN_GZH', $type = static::TYPES[static::$type]['type'];
static::PAYMENT_JOINPAY_XCX => 'WEIXIN_XCX',
];
if (isset($types[static::$type])) {
$type = $types[static::$type];
} else { } else {
throw new \think\Exception('支付类型[' . static::$type . ']未配置定义!'); throw new \think\Exception('支付类型[' . static::$type . ']未配置定义!');
} }
@ -85,7 +81,7 @@ class JoinPaymentService extends PaymentService
'p4_Cur' => '1', 'p4_Cur' => '1',
'p5_ProductName' => $payTitle, 'p5_ProductName' => $payTitle,
'p6_ProductDesc' => $payDescription, 'p6_ProductDesc' => $payDescription,
'p9_NotifyUrl' => sysuri('@data/api.notify/joinpay/scene/order', [], false, true), 'p9_NotifyUrl' => sysuri('@data/api.notify/joinpay/scene/order/type/' . static::$type, [], false, true),
'q1_FrpCode' => $type ?? '', 'q1_FrpCode' => $type ?? '',
'q5_OpenId' => $openid, 'q5_OpenId' => $openid,
'q7_AppId' => $this->appid, 'q7_AppId' => $this->appid,
@ -126,13 +122,15 @@ class JoinPaymentService extends PaymentService
/** /**
* 支付结果处理 * 支付结果处理
* @param string $type 支付通道
* @return string * @return string
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function notify(): string public function notify(string $type = ''): string
{ {
$type = $type ?: static::$type;
$notify = $this->app->request->get(); $notify = $this->app->request->get();
foreach ($notify as &$item) $item = urldecode($item); foreach ($notify as &$item) $item = urldecode($item);
if (empty($notify['hmac']) || $notify['hmac'] !== $this->_doSign($notify)) { if (empty($notify['hmac']) || $notify['hmac'] !== $this->_doSign($notify)) {
@ -142,14 +140,14 @@ class JoinPaymentService extends PaymentService
// 更新支付记录 // 更新支付记录
data_save('DataPaymentItem', [ data_save('DataPaymentItem', [
'order_no' => $notify['r2_OrderNo'], 'order_no' => $notify['r2_OrderNo'],
'payment_type' => static::$type, 'payment_type' => $type,
'payment_code' => $notify['r9_BankTrxNo'], 'payment_code' => $notify['r9_BankTrxNo'],
'payment_amount' => $notify['r3_Amount'], 'payment_amount' => $notify['r3_Amount'],
'payment_status' => 1, 'payment_status' => 1,
'payment_datatime' => date('Y-m-d H:i:s'), 'payment_datatime' => date('Y-m-d H:i:s'),
], 'order_no', ['payment_type' => static::$type, 'payment_status' => 0]); ], 'order_no', ['payment_type' => $type, 'payment_status' => 0]);
// 更新记录状态 // 更新记录状态
if ($this->updateOrder($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'], 'joinpay')) { if ($this->updateOrder($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'], $type)) {
return 'success'; return 'success';
} }
} }

View File

@ -3,7 +3,6 @@
namespace app\data\service\payment; namespace app\data\service\payment;
use app\data\service\PaymentService; use app\data\service\PaymentService;
use http\Exception;
use WePay\Order; use WePay\Order;
/** /**
@ -59,14 +58,19 @@ class WechatPaymentService extends PaymentService
public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array
{ {
try { try {
if (isset(static::TYPES[static::$type])) {
$type = static::TYPES[static::$type]['type'];
} else {
throw new \think\Exception('支付类型[' . static::$type . ']未配置定义!');
}
$body = empty($payDescription) ? $payTitle : ($payTitle . '-' . $payDescription); $body = empty($payDescription) ? $payTitle : ($payTitle . '-' . $payDescription);
$data = [ $data = [
'body' => $body, 'body' => $body,
'openid' => $openid, 'openid' => $openid,
'out_trade_no' => $orderNo, 'out_trade_no' => $orderNo,
'total_fee' => $payAmount * 100, 'total_fee' => $payAmount * 100,
'trade_type' => 'JSAPI', 'trade_type' => $type,
'notify_url' => sysuri('@data/api.notify/wxpay/scene/order', [], false, true), 'notify_url' => sysuri('@data/api.notify/wxpay/scene/order/type/' . static::$type, [], false, true),
'spbill_create_ip' => $this->app->request->ip(), 'spbill_create_ip' => $this->app->request->ip(),
]; ];
if (empty($data['openid'])) unset($data['openid']); if (empty($data['openid'])) unset($data['openid']);
@ -93,27 +97,29 @@ class WechatPaymentService extends PaymentService
/** /**
* 支付结果处理 * 支付结果处理
* @param string $type 支付通道
* @return string * @return string
* @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\InvalidResponseException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function notify(): string public function notify(string $type = ''): string
{ {
$type = $type ?: static::$type;
$notify = $this->payment->getNotify(); $notify = $this->payment->getNotify();
if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') { if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
// 更新支付记录 // 更新支付记录
data_save('DataPaymentItem', [ data_save('DataPaymentItem', [
'order_no' => $notify['out_trade_no'], 'order_no' => $notify['out_trade_no'],
'payment_type' => static::$type, 'payment_type' => $type,
'payment_code' => $notify['transaction_id'], 'payment_code' => $notify['transaction_id'],
'payment_amount' => $notify['cash_fee'] / 100, 'payment_amount' => $notify['cash_fee'] / 100,
'payment_status' => 1, 'payment_status' => 1,
'payment_datatime' => date('Y-m-d H:i:s'), 'payment_datatime' => date('Y-m-d H:i:s'),
], 'order_no', ['payment_type' => static::$type, 'payment_status' => 0]); ], 'order_no', ['payment_type' => $type, 'payment_status' => 0]);
// 更新记录状态 // 更新记录状态
if ($this->updateOrder($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, 'wechat')) { if ($this->updateOrder($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, $type)) {
return $this->payment->getNotifySuccessReply(); return $this->payment->getNotifySuccessReply();
} }
} else { } else {