Commit 9b9c9981 authored by wanjilong's avatar wanjilong

add: 调整订单支付逻辑,增加退款发送,增加退款回调

parent 11e8f98f
......@@ -318,13 +318,13 @@ class PayService
'need_recorded' => $account['need_recorded'],
];
if($is_wx) { //微信不计算手续费拆弹
return true;
}
foreach ($this->order_items as $r) {
if($is_wx) {
$current_tip = Strategy::getWxTip($r['payment'], $total_amount, $cleared_amount, $total_tip, $cleared_tip);
} else {
$current_tip = Strategy::getTip($r['payment'], $total_amount, $cleared_amount, $total_tip, $cleared_tip);
}
$current_tip = Strategy::getTip($r['payment'], $total_amount, $cleared_amount, $total_tip, $cleared_tip);
$this->clear_items_list[] = [
'pay_order_clearing_item_id' => $this->gen_pay_order_clearing_item_id(),
......
......@@ -19,7 +19,12 @@ use App\Exception\custom\RefundException;
class RefundService
{
/**
* @param $order_item_id
* @param $user_id
* @throws RefundException
* 发起退款请求
*/
public function do_refund($order_item_id, $user_id) {
// 获取子单信息
......@@ -27,26 +32,84 @@ class RefundService
if(empty($order_item)) {
throw new RefundException(['cus' => 1]);
}
$where = [
'order_item_id'=>$order_item_id,
'user_id'=>$user_id,
'order_id'=>$order_item['order_id'],
'refund_amount'=>$order_item['payment'],
'refund_order_id'=>$this->gen_refund_order_id(),
];
$refund_order = $this->can_refund($where);
$refund_order = $this->can_refund($where, $order_item);
// 重新计算手续费
// 发起ping++ 创建退款
try {
// 通过发起一次退款请求创建一个新的 refund 对象,只能对已经发生交易并且没有全额退款的 charge 对象发起退款
$re = \Pingpp\Refund::create(
$refund_order['third_order_id'],
[
'amount' => $refund_order['refund_amount'],
'description' => '订单申请退款',
'metadata'=>['refund_order_id'=>$refund_order['refund_order_id']]
]
);
$ret = json_decode($re, true);
if(!empty($ret["data"])) {
$edit = [
'request_pingxx_success_time'=>date('Y-m-d H:i:s'),
];
} else {
$edit = [
'refund_order_status'=>Dictionary::REFUND_ORDER_STATUS_UNDO,
];
}
} catch (\Pingpp\Error\Base $e) {
$edit = [
'refund_order_status'=>Dictionary::REFUND_ORDER_STATUS_UNDO,
];
Logger::error($e->getHttpBody());
}
RefundOrder::update($edit, ['refund_order_id'=>$refund_order['refund_order_id']]);
}
public function call_back($data) {
if(empty($data['data']['object'])) {
Logger::error('回调参数错误:', $data);
}
$object = $data['data']['object'];
if(empty($object['metadata']['refund_order_id'])) {
throw new RefundException(['cus' => 0]);
}
$refund_order_id = $object['metadata']['refund_order_id'];
try {
// 查询 refund 对象列表
$re_list = \Pingpp\Refund::all('ch_L8qn10mLmr1GS8e5OODmHaL4', [
'limit' => 10,
]);
echo $re_list; // 输出 Ping++ 返回的 refund 对象列表
} catch (\Pingpp\Error\Base $e) {
if ($e->getHttpStatus() != null) {
header('Status: ' . $e->getHttpStatus());
echo $e->getHttpBody();
} else {
echo $e->getMessage();
}
}
// 创建退款订单,计算微信退款手续费
// 修改订单支付状态,
// 发起ping++ 创建退款
$this->send_pingxx($order_item);
}
/**
......@@ -55,7 +118,7 @@ class RefundService
* @throws RefundException
* 判断订单是否可退,判断是否存在退款申请
*/
private function can_refund($where) {
private function can_refund($where, $order_item) {
/**
* 业务方自行解决,订单关闭、订单超过时长不允许退款等逻辑
......@@ -86,16 +149,46 @@ class RefundService
} else {
$refund_order = $where;
$refund_order['refund_order_status'] = Dictionary::REFUND_ORDER_STATUS_SEND;
$refund_order['refund_order_status'] = Dictionary::REFUND_ORDER_STATUS_UNDO;
$refund_order['refund_amount'] = 0;
$cnt = RefundOrder::insert($refund_order, ['rowCount'=>true]);
if($cnt == 0) {
throw new RefundException(['cus' => 0]);
}
}
//重新计算微信手续费
$cleared_amount = $cleared_tip = 0;
// 总共手续费
$wx_tip = PayOrderClearing::getMaster('*', [
'order_id'=>$pay_order['order_id'],
'pay_order_id'=>$pay_order['pay_order_id'],
'pay_type'=>Dictionary::PAY_TYPE_IN]
);
// 计算已退金额、已退手续费
$list = RefundOrder::selectMaster('*', ['order_id'=>$where['order_id']]);
foreach ($list as $r) {
if($r['refund_order_status'] == Dictionary::REFUND_ORDER_STATUS_UNDO) {
continue;
}
$cleared_amount += $r['refund_amount'];
$cleared_tip += $r['refund_wx_tip'];
}
$wx_tip = Strategy::getWxTip($order_item['payment'], $pay_order['payment'], $cleared_amount, $wx_tip['pay_amount'], $cleared_tip);
$edit = [
'refund_order_status'=>Dictionary::REFUND_ORDER_STATUS_SEND,
'refund_amount'=>$order_item['payment'],
'refund_wx_tip'=>$wx_tip,
];
RefundOrder::update($edit, ['refund_order_id'=>$refund_order['refund_order_id']]);
$refund_order['third_order_id'] = $pay_order['third_order_id'];
RefundOrder::commit();
}catch (\PDOException $e) {
......
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