Commit 8fe1f2bf authored by 万继龙's avatar 万继龙

Merge branch 'develop' into 'test'

add: 调整支付回调兼容 change \order

See merge request bp/pay!7
parents c61001a0 9ae25f92
...@@ -19,5 +19,9 @@ class RefundException extends BaseException ...@@ -19,5 +19,9 @@ class RefundException extends BaseException
5 => '该订单不允许退款,请核对', 5 => '该订单不允许退款,请核对',
6 => '该退款单核对ping++状态为未支付,请稍后重试!', 6 => '该退款单核对ping++状态为未支付,请稍后重试!',
7 => '已核销计算订单不支持退款!', 7 => '已核销计算订单不支持退款!',
8 => '退款消息类型匹配失败,请管理员关注!',
9 => 'ping++退款消息格式错误,请管理员关注!',
10 => 'ping++退单状态异常,请管理员关注!',
11 => 'ping++退单缺少matadata!',
]; ];
} }
...@@ -34,17 +34,11 @@ class CallbackController extends Base ...@@ -34,17 +34,11 @@ class CallbackController extends Base
} }
//$str = gzuncompress(base64_decode($info['data'])); //$str = gzuncompress(base64_decode($info['data']));
//先记录在验签
$signature = $_SERVER['HTTP_X_PINGPLUSPLUS_SIGNATURE']?? '';
$sign = PingxxService::getInstance()->verifySignature($raw_data, $signature);
if($sign != 1) {
throw new \Exception('验证签名失败,请管理员关注');
}
$ret = $paySrv->call_back($data); $ret = $paySrv->call_back($data);
$this->success(['result'=>$ret]); $this->success(['result'=>$ret]);
}catch (Exception $e) { }catch (Exception $e) {
throw $e;
$this->failed('500', $e->getMessage()); $this->failed('500', $e->getMessage());
} }
} }
...@@ -64,20 +58,11 @@ class CallbackController extends Base ...@@ -64,20 +58,11 @@ class CallbackController extends Base
]); ]);
} }
//先记录在验签
$signature = $_SERVER['HTTP_X_PINGPLUSPLUS_SIGNATURE']?? '';
$sign = PingxxService::getInstance()->verifySignature($raw_data, $signature);
if($sign != 1) {
throw new \Exception('验证签名失败,请管理员关注');
}
$refundSrv = new RefundService(); $refundSrv = new RefundService();
$ret = $refundSrv->call_back($data); $ret = $refundSrv->call_back($data);
$this->success(['result'=>$ret]); $this->success(['result'=>$ret]);
}catch (Exception $e) { }catch (Exception $e) {
http_response_code(500);
$this->failed('500', $e->getMessage()); $this->failed('500', $e->getMessage());
} }
} }
......
...@@ -2,8 +2,11 @@ ...@@ -2,8 +2,11 @@
use App\Base\Base; use App\Base\Base;
use App\Exception\custom\PayException;
use App\Exception\custom\RefundException;
use App\Services\pay\PayService; use App\Services\pay\PayService;
use App\Services\refund\RefundService; use App\Services\refund\RefundService;
use Api\PhpUtils\Log\FileLog;
class OrderController extends Base class OrderController extends Base
...@@ -13,39 +16,65 @@ class OrderController extends Base ...@@ -13,39 +16,65 @@ class OrderController extends Base
*/ */
public function payAction() public function payAction()
{ {
$params = $this->params; try{
$user_id = $params['user_id']; $params = $this->params;
$order_id = $params['order_id']; $user_id = $params['user_id'];
$pay_method_id = $params['pay_method_id'] ?? 106; $order_id = $params['order_id'];
$pay_method_id = $params['pay_method_id'] ?? 106;
$paySrv = new PayService();
$ret = $paySrv->do_pay($order_id, $user_id, $pay_method_id); $paySrv = new PayService();
$ret = $paySrv->do_pay($order_id, $user_id, $pay_method_id);
//兼容C端客户端支付,调整返回结构,ping++ 返回 order,原生:微信、支付宝返回charge
$result = []; //兼容C端客户端支付,调整返回结构,ping++ 返回 order,原生:微信、支付宝返回charge
$result[] = ['k'=>'order', 'v'=>$ret]; $result = [];
$this->success(['result'=>$result]); $result[] = ['k'=>'order', 'v'=>$ret];
$this->success(['result'=>$result]);
}catch (PayException $e) {
$this->failed($e->getMessage(), $e->getCode());
} catch (\Exception $e) {
FileLog::error('pay-service:ping++支付报错', json_encode($params, JSON_UNESCAPED_UNICODE), $e);
$this->failed($e->getMessage(), $e->getCode());
}
} }
public function refundAction() { public function refundAction() {
$params = $this->params;
$user_id = $params['user_id'];
$order_item_id = $params['order_item_id'];
$refundSrv = new RefundService(); try{
$ret = $refundSrv->do_refund($order_item_id, $user_id); $params = $this->params;
$user_id = $params['user_id'];
$order_item_id = $params['order_item_id'];
$refundSrv = new RefundService();
$ret = $refundSrv->do_refund($order_item_id, $user_id);
$this->success(['result'=>$ret]); $this->success(['result'=>$ret]);
}catch (RefundException $e) {
$this->failed($e->getMessage(), $e->getCode());
} catch (\Exception $e) {
FileLog::error('pay-service:ping++退款报错', json_encode($params, JSON_UNESCAPED_UNICODE), $e);
$this->failed($e->getMessage(), $e->getCode());
}
} }
public function write_offAction() { public function write_offAction() {
$params = $this->params;
$life_account_id = $params['life_account_id'];
$order_item_id = $params['order_item_id'];
$refundSrv = new PayService(); try{
$ret = $refundSrv->write_off($order_item_id, $life_account_id); $params = $this->params;
$life_account_id = $params['life_account_id'];
$order_item_id = $params['order_item_id'];
$refundSrv = new PayService();
$ret = $refundSrv->write_off($order_item_id, $life_account_id);
$this->success(['result'=>$ret]);
$this->success(['result'=>$ret]); }catch (RefundException $e) {
$this->failed($e->getMessage(), $e->getCode());
} catch (\Exception $e) {
FileLog::error('pay-service:核销报错', json_encode($params, JSON_UNESCAPED_UNICODE), $e);
$this->failed($e->getMessage(), $e->getCode());
}
} }
} }
...@@ -142,7 +142,7 @@ class PayService ...@@ -142,7 +142,7 @@ class PayService
*/ */
public function call_back($data) public function call_back($data)
{ {
if ($data['type'] != 'order.succeeded') { if (!in_array($data['type'], ['order.succeeded', 'charge.succeeded'])) {
throw new PayException(['cus' => 7]); throw new PayException(['cus' => 7]);
} }
...@@ -151,37 +151,44 @@ class PayService ...@@ -151,37 +151,44 @@ class PayService
} }
$object = $data['data']['object']; $object = $data['data']['object'];
/*
$ping_data = PingxxService::getInstance()->getOrder($object['id']); if($data['type'] == 'charge.succeeded') {
if($ping_data['status'] != 'paid') { $pay_order_id = $object['order_no'];
$paid = $object['paid'];
$amount = $object['amount'];
} else {
$pay_order_id = $object['merchant_order_no'];
$paid = $object['paid'];
$amount = $object['actual_amount'];
}
if($paid != true) {
throw new PayException(['cus' => 6]);
}
$pay_order = PayOrder::get('*', ['pay_order_id' => $pay_order_id]);
if (empty($pay_order)) {
throw new PayException(['cus' => 5]); throw new PayException(['cus' => 5]);
} //actual_amount 金额核对 }
*/ if($pay_order['pay_amount'] != $amount) {
throw new PayException(['cus' => 15]);
}
// 修改状态,锁定业务 // 修改状态,锁定业务
$pay_order_id = $object['merchant_order_no'];
$edit = [ $edit = [
'pay_order_status' => Dictionary::O_PAY_STATUS_PAYED, 'pay_order_status' => 2, //已经支付
'pingxx_callback_success_time' => date('Y-m-d H:i:s'), 'pingxx_callback_success_time' => date('Y-m-d H:i:s'),
]; ];
$where = [ $where = [
'pay_order_id' => $pay_order_id, 'pay_order_id' => $pay_order_id,
'pay_order_status' => Dictionary::O_PAY_STATUS_WAIT 'pay_order_status' => 1, //待支付确认
]; ];
$cnt = PayOrder::update($edit, $where); $cnt = PayOrder::update($edit, $where);
$pay_order = PayOrder::getMaster('*', ['pay_order_id' => $pay_order_id]);
if (empty($pay_order)) {
throw new PayException(['cus' => 5]);
}
//保持幂等返回成功 //保持幂等返回成功
if($cnt == 0 && $pay_order['pay_order_status'] == Dictionary::O_PAY_STATUS_PAYED) { if($cnt == 0) {
return ['pay_order_id' => $pay_order_id]; return ['pay_order_id' => $pay_order_id];
} }
if($pay_order['pay_amount'] != $object['actual_amount']) {
throw new PayException(['cus' => 15]);
}
//获取订单 + 子单 + 分销信息 //获取订单 + 子单 + 分销信息
$ret = OrderService::getFullOrderData($pay_order['order_id'], $pay_order['user_id']); $ret = OrderService::getFullOrderData($pay_order['order_id'], $pay_order['user_id']);
if (empty($ret['result']['order_info'])) { if (empty($ret['result']['order_info'])) {
...@@ -289,7 +296,7 @@ class PayService ...@@ -289,7 +296,7 @@ class PayService
], ['order_item_id' => $order_item_id, 'can_notify_account' => 1]); ], ['order_item_id' => $order_item_id, 'can_notify_account' => 1]);
} catch (\Exception $e) { } catch (\Exception $e) {
FileLog::error('wallet: 钱包同步失败', json_encode($wallet_list, JSON_UNESCAPED_UNICODE), $e); FileLog::error('pay-service:wallet: 钱包同步失败', json_encode($wallet_list, JSON_UNESCAPED_UNICODE), $e);
//补偿处理 //补偿处理
PayOrderItem::update([ PayOrderItem::update([
'notify_account_status' => 0, 'notify_account_status' => 0,
...@@ -327,7 +334,7 @@ class PayService ...@@ -327,7 +334,7 @@ class PayService
//核对各项资金的合理性 //核对各项资金的合理性
if($wx_tip <0 || $distribution_tip <0 || $platform_tip < 0 || $merchant_cash < 0) { if($wx_tip <0 || $distribution_tip <0 || $platform_tip < 0 || $merchant_cash < 0) {
FileLog::error('pay: 支付预结算失败,结算资金为负', FileLog::error('pay-service: 支付预结算失败,结算资金为负',
json_encode([$this->order_info['payment'], $wx_tip, $distribution_tip, $platform_tip, $merchant_cash])); json_encode([$this->order_info['payment'], $wx_tip, $distribution_tip, $platform_tip, $merchant_cash]));
throw new PayException(['cus'=>14]); throw new PayException(['cus'=>14]);
} }
...@@ -505,7 +512,7 @@ class PayService ...@@ -505,7 +512,7 @@ class PayService
'ret'=>$ret, 'ret'=>$ret,
], JSON_UNESCAPED_UNICODE); ], JSON_UNESCAPED_UNICODE);
FileLog::error('pay:', '获取生活号管理员失败', $log_string); FileLog::error('pay-service:', '获取生活号管理员失败', $log_string);
throw new PayException(['cus'=>13]); throw new PayException(['cus'=>13]);
} }
...@@ -639,6 +646,7 @@ class PayService ...@@ -639,6 +646,7 @@ class PayService
108=>"wx_pub_qr", //微信 Native 支付 108=>"wx_pub_qr", //微信 Native 支付
109=>"wx_wap", //微信 H5 支付 109=>"wx_wap", //微信 H5 支付
110=>"wx_lite", //微信小程序支付 110=>"wx_lite", //微信小程序支付
120=>"alipay", //支付宝
]; ];
if($maps[$pay_method_id]) { if($maps[$pay_method_id]) {
......
...@@ -75,37 +75,64 @@ class RefundService ...@@ -75,37 +75,64 @@ class RefundService
return ['order_item_id'=>$order_item_id]; return ['order_item_id'=>$order_item_id];
}catch (\Exception $e) { }catch (\Exception $e) {
$edit = ['refund_order_status'=>Dictionary::REFUND_ORDER_STATUS_UNDO,]; $edit = ['refund_order_status'=>0];
RefundOrder::update($edit, ['refund_order_id'=>$data['refund_order']['refund_order_id']]); RefundOrder::update($edit, ['refund_order_id'=>$data['refund_order']['refund_order_id']]);
throw $e; throw $e;
} }
} }
/**
* @param $data
* @return array
* @throws RefundException
* 退款回调
*/
public function call_back($data) { public function call_back($data) {
if(empty($data['data']['object'])) {
throw new RefundException(['cus' => 6]); if (!in_array($data['type'], ['refund.succeeded', 'order.refunded'])) {
throw new RefundException(['cus' => 8]);
}
if (empty($data['data']['object'])) {
throw new RefundException(['cus' => 9]);
} }
$object = $data['data']['object']; $object = $data['data']['object'];
if(empty($object['metadata']['refund_order_id'])) {
throw new RefundException(['cus' => 0]); if($data['type'] == 'charge.succeeded') {
$refund_order_id = $object['order_no'];
$refunded = $object['succeed']; //"succeed": true,
} else {
$refund_order_id = $object['merchant_order_no'];
$refunded = $object['refunded'];
}
if($refunded != true) {
throw new RefundException(['cus' => 10]);
} }
/* $object = $data['data']['object'];
// 获取退款回调信息,确认订单ID if(empty($object['metadata']['refund_order_id'])) {
$charge = $object['charges']['data'][0]; throw new RefundException(['cus' => 11]);
$ping_refund = PingxxService::getInstance()->getRefund($charge['id'], $object['id']);
if($ping_refund['status'] != 'succeeded') {
throw new RefundException(['cus' => 6]);
} }
*/
// 修改状态,锁定业务 // 修改状态,锁定业务
$refund_order_id = $object['metadata']['refund_order_id']; $refund_order_id = $object['metadata']['refund_order_id'];
$this->lock_refund_clear($refund_order_id); $edit = [
'refund_order_status'=>2, //pingxx回调成功,
'pingxx_callback_success_time'=>date('Y-m-d H:i:s'), //回调成功时间
'notify_account_status'=>1, //不需要通知钱包
];
$cnt = RefundOrder::update($edit, ['refund_order_id'=>$refund_order_id, 'refund_order_status'=>1]);
//保持幂等操作
if($cnt == 0) {
return ['refund_order_id'=>$refund_order_id];
}
//不用搞清分核算,是否需要平台记账? return ['refund_order_id'=>$refund_order_id];
$this->make_refund_clear($refund_order_id);
//不用搞清分核算,是否需要平台记账
} }
/** /**
...@@ -202,42 +229,6 @@ class RefundService ...@@ -202,42 +229,6 @@ class RefundService
return $data; return $data;
} }
private function lock_refund_clear($refund_order_id) {
try{
RefundOrder::beginTransaction();
$refund_order = RefundOrder::get('*', ['refund_order_id'=>$refund_order_id]);
if(empty($refund_order)) {
throw new RefundException(['cus' => 0]);
}
$this->refund_order = $refund_order;
if($refund_order['refund_order_status'] == Dictionary::REFUND_ORDER_STATUS_OK) {
throw new RefundException(['cus' => 0]);
}
$edit = [
'refund_order_status'=>Dictionary::REFUND_ORDER_STATUS_OK,
'pingxx_callback_success_time'=>date('Y-m-d H:i:s'),
'notify_account_status'=>Dictionary::SEND_ACCOUNT_STATUS_DONE,
];
RefundOrder::update($edit, ['refund_order_id'=>$refund_order_id]);
RefundOrder::commit();
}catch (\PDOException $e) {
RefundOrder::rollback();
throw $e;
}
}
private function make_refund_clear() {
return true;
//仅处理WX手续费,平台补贴,商户补贴
}
private function gen_refund_order_id($user_id) private function gen_refund_order_id($user_id)
{ {
$number = substr($user_id, -2); $number = substr($user_id, -2);
......
...@@ -16,7 +16,6 @@ class Wallet implements DaemonServiceInterface ...@@ -16,7 +16,6 @@ class Wallet implements DaemonServiceInterface
*/ */
public function run() public function run()
{ {
FileLog::info('Daemon:', 'start');
if (!empty($data['LIMIT'])) { if (!empty($data['LIMIT'])) {
$where['LIMIT'] = $data['LIMIT']; $where['LIMIT'] = $data['LIMIT'];
} }
...@@ -26,6 +25,7 @@ class Wallet implements DaemonServiceInterface ...@@ -26,6 +25,7 @@ class Wallet implements DaemonServiceInterface
} }
$where['can_notify_account'] = 1; $where['can_notify_account'] = 1;
$where['notify_account_times[<]'] = 5;
$where['ORDER'] = 'can_notify_account_time ASC'; $where['ORDER'] = 'can_notify_account_time ASC';
$where['LIMIT'] = [0, 10]; $where['LIMIT'] = [0, 10];
...@@ -38,7 +38,7 @@ class Wallet implements DaemonServiceInterface ...@@ -38,7 +38,7 @@ class Wallet implements DaemonServiceInterface
} }
if(empty($list)) { if(empty($list)) {
sleep(10); sleep(120);
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment