优化支付动作记录,及支付行为

This commit is contained in:
Anyon 2020-12-16 10:57:15 +08:00
parent aba2f12fae
commit a45f89622c
4 changed files with 101 additions and 85 deletions

View File

@ -147,20 +147,20 @@ abstract class PaymentService extends Service
* @param string $code 订单单号
* @param string $payno 交易单号
* @param string $amount 支付金额
* @param string $paytype 支付类型
* @param null|string $paytype 支付类型
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function updateOrder(string $code, string $payno, string $amount, string $paytype): bool
public function updateOrder(string $code, string $payno, string $amount, ?string $paytype = null): bool
{
// 检查订单支付状态
$map = ['order_no' => $code, 'payment_status' => 0, 'status' => 2];
$order = $this->app->db->name('StoreOrder')->where($map)->find();
if (empty($order)) return false;
// 更新订单支付状态
$this->app->db->name('StoreOrder')->where($map)->update([
$data = [
'status' => 3,
'payment_code' => $payno,
'payment_type' => $paytype,
@ -168,17 +168,65 @@ abstract class PaymentService extends Service
'payment_amount' => $amount,
'payment_remark' => '微信在线支付',
'payment_datetime' => date('Y-m-d H:i:s'),
]);
];
if (empty($data['payment_type'])) unset($data['payment_type']);
$this->app->db->name('StoreOrder')->where($map)->update($data);
// 调用用户升级机制
return OrderService::instance()->syncAmount($order['order_no']);
}
/**
* 支付通知处理
* @param string $param 支付通道-支付编号
* @return string
* 创建支付行为
* @param string $param 通道-编号
* @param string $orderNo 商户订单单号
* @param string $payTitle 商户订单标题
* @param string $payAmount
*/
abstract public function notify(string $param = ''): string;
protected function createPaymentAction(string $param, string $orderNo, string $payTitle, string $payAmount)
{
if (is_numeric(stripos($param, '-'))) {
[$paymentType, $paymentId] = explode('-', $param);
} else {
[$paymentType, $paymentId] = [$param ?: static::$type, static::$id];
}
// 创建支付记录
$this->app->db->name('DataPaymentItem')->insert([
'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount,
'payment_id' => $paymentId, 'payment_type' => $paymentType,
]);
}
/**
* 更新支付记录并更新订单
* @param string $param 通道-编号
* @param string $orderNo 商户订单单号
* @param string $paymentCode 平台交易单号
* @param string $paymentAmount 实际到账金额
* @return boolean
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function updatePaymentAction(string $param, string $orderNo, string $paymentCode, string $paymentAmount): bool
{
if (is_numeric(stripos($param, '-'))) {
[$paymentType, $paymentId] = explode('-', $param);
} else {
[$paymentType, $paymentId] = [$param ?: static::$type, static::$id];
}
// 更新支付记录
data_save('DataPaymentItem', [
'order_no' => $orderNo,
'payment_id' => $paymentId,
'payment_type' => $paymentType,
'payment_code' => $paymentCode,
'payment_amount' => $paymentAmount,
'payment_status' => 1,
'payment_datatime' => date('Y-m-d H:i:s'),
], 'order_no', ['payment_id' => $paymentId, 'payment_type' => $paymentType]);
// 更新记录状态
return $this->updateOrder($orderNo, $paymentCode, $paymentAmount, $paymentType);
}
/**
* 订单主动查询
@ -187,6 +235,13 @@ abstract class PaymentService extends Service
*/
abstract public function query(string $orderNo): array;
/**
* 支付通知处理
* @param string $param 支付通道-支付编号
* @return string
*/
abstract public function notify(string $param = ''): string;
/**
* 创建支付订单
* @param string $openid 会员OPENID

View File

@ -72,29 +72,15 @@ class AlipayPaymentService extends PaymentService
*/
public function notify(string $param = ''): string
{
if (is_numeric(stripos($param, '-'))) {
[$payType, $payId] = explode('-', $param);
} else {
[$payType, $payId] = [$param ?: static::$type, static::$id];
}
$notify = \AliPay\App::instance($this->params)->notify();
if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
// 更新支付记录
data_save('DataPaymentItem', [
'order_no' => $notify['out_trade_no'],
'payment_id' => $payId,
'payment_type' => $payType,
'payment_code' => $notify['trade_no'],
'payment_amount' => $notify['total_amount'],
'payment_status' => 1,
'payment_datatime' => date('Y-m-d H:i:s'),
], 'order_no', ['payment_id' => $payId, 'payment_type' => $payType, 'payment_status' => 0]);
// 更新记录状态
if ($this->updateOrder($notify['out_trade_no'], $notify['trade_no'], $notify['total_amount'], $payType)) {
if ($this->updatePaymentAction($param, $notify['out_trade_no'], $notify['trade_no'], $notify['total_amount'])) {
return 'success';
} else {
return 'error';
}
} else {
return 'error';
return 'success';
}
}
@ -151,10 +137,8 @@ class AlipayPaymentService extends PaymentService
if (!empty($payRemark)) $data['body'] = $payRemark;
$result = $payment->apply($data);
// 创建支付记录
$this->app->db->name('DataPaymentItem')->insert([
'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount,
'payment_id' => static::$id, 'payment_type' => static::$type,
]);
$this->createPaymentAction($tradeParam, $orderNo, $payTitle, $payAmount);
// 返回支付参数
return ['result' => $result];
} catch (\think\Exception $exception) {
throw $exception;

View File

@ -94,10 +94,7 @@ class JoinPaymentService extends PaymentService
$result = $this->_doReuest($data);
if (is_array($result) && isset($result['ra_Code']) && intval($result['ra_Code']) === 100) {
// 创建支付记录
$this->app->db->name('DataPaymentItem')->insert([
'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount,
'payment_id' => static::$id, 'payment_type' => static::$type,
]);
$this->createPaymentAction($tradeParam, $orderNo, $payTitle, $payAmount);
// 返回支付参数
return json_decode($result['rc_Result'], true);
} elseif (is_array($result) && isset($result['rb_CodeMsg'])) {
@ -133,33 +130,20 @@ class JoinPaymentService extends PaymentService
*/
public function notify(string $param = ''): string
{
if (is_numeric(stripos($param, '-'))) {
[$payType, $payId] = explode('-', $param);
} else {
[$payType, $payId] = [$param ?: static::$type, static::$id];
}
$notify = $this->app->request->get();
foreach ($notify as &$item) $item = urldecode($item);
if (empty($notify['hmac']) || $notify['hmac'] !== $this->_doSign($notify)) {
return 'error';
}
if (isset($notify['r6_Status']) && intval($notify['r6_Status']) === 100) {
// 更新支付记录
data_save('DataPaymentItem', [
'order_no' => $notify['r2_OrderNo'],
'payment_id' => $payId,
'payment_type' => $payType,
'payment_code' => $notify['r9_BankTrxNo'],
'payment_amount' => $notify['r3_Amount'],
'payment_status' => 1,
'payment_datatime' => date('Y-m-d H:i:s'),
], 'order_no', ['payment_id' => $payId, 'payment_type' => $payType, 'payment_status' => 0]);
// 更新记录状态
if ($this->updateOrder($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'], $payType)) {
if ($this->updatePaymentAction($param, $notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'])) {
return 'success';
} else {
return 'error';
}
} else {
return 'success';
}
return 'error';
}
/**

View File

@ -33,18 +33,6 @@ class WechatPaymentService extends PaymentService
return $this;
}
/**
* 查询微信支付订单
* @param string $orderNo
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException
*/
public function query(string $orderNo): array
{
return $this->payment->query(['out_trade_no' => $orderNo]);
}
/**
* 创建微信支付订单
* @param string $openid 会员OPENID
@ -69,6 +57,7 @@ class WechatPaymentService extends PaymentService
$data = [
'body' => $body,
'openid' => $openid,
'attach' => $tradeParam,
'out_trade_no' => $orderNo,
'total_fee' => $payAmount * 100,
'trade_type' => $tradeType ?: '',
@ -79,10 +68,7 @@ class WechatPaymentService extends PaymentService
$info = $this->payment->create($data);
if ($info['return_code'] === 'SUCCESS' && $info['result_code'] === 'SUCCESS') {
// 创建支付记录
$this->app->db->name('DataPaymentItem')->insert([
'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount,
'payment_id' => static::$id, 'payment_type' => static::$type,
]);
$this->createPaymentAction($tradeParam, $orderNo, $payTitle, $payAmount);
// 返回支付参数
return $this->payment->jsapiParams($info['prepay_id']);
}
@ -98,6 +84,27 @@ class WechatPaymentService extends PaymentService
}
}
/**
* 查询微信支付订单
* @param string $orderNo 订单单号
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function query(string $orderNo): array
{
$result = $this->payment->query(['out_trade_no' => $orderNo]);
if (isset($result['return_code']) && isset($result['result_code']) && isset($result['attach'])) {
if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
$this->updatePaymentAction($result['attach'], $result['out_trade_no'], $result['cash_fee'] / 100, $result['transaction_id']);
}
}
return $result;
}
/**
* 支付结果处理
* @param string $param 支付通道
@ -109,26 +116,12 @@ class WechatPaymentService extends PaymentService
*/
public function notify(string $param = ''): string
{
if (is_numeric(stripos($param, '-'))) {
[$payType, $payId] = explode('-', $param);
} else {
[$payType, $payId] = [$param ?: static::$type, static::$id];
}
$notify = $this->payment->getNotify();
if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
// 更新支付记录
data_save('DataPaymentItem', [
'order_no' => $notify['out_trade_no'],
'payment_id' => $payId,
'payment_type' => $payType,
'payment_code' => $notify['transaction_id'],
'payment_amount' => $notify['cash_fee'] / 100,
'payment_status' => 1,
'payment_datatime' => date('Y-m-d H:i:s'),
], 'order_no', ['payment_id' => $payId, 'payment_type' => $payType, 'payment_status' => 0]);
// 更新记录状态
if ($this->updateOrder($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, $payType)) {
if ($this->updatePaymentAction($param, $notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100)) {
return $this->payment->getNotifySuccessReply();
} else {
return 'error';
}
} else {
return $this->payment->getNotifySuccessReply();