Commit fbc30077 authored by 顾文旭's avatar 顾文旭

fix

parent 33f0873f
......@@ -4,7 +4,6 @@
namespace App\Services\pay;
use Api\PhpServices\Idgen\Idgen;
use Api\PhpUtils\Http\Request;
use Api\PhpUtils\Log\FileLog;
use App\Models\Dictionary;
use App\Models\order\mysql\PayOrder;
......@@ -36,7 +35,7 @@ class PayService
public function do_pay($order_id, $user_id, $pingxx_id, $pay_method_id = 105, $extra = [])
{
if (empty($order_id)) {
if(empty($order_id)) {
throw new PayException(['cus' => 1]);
}
// 获取订单信息
......@@ -55,52 +54,52 @@ class PayService
}
$pay_expiration_time = strtotime($order_info['pay_expiration_time']);
if ($pay_expiration_time < time()) {
if($pay_expiration_time < time()) {
throw new PayException(['cus' => 16]);
}
if (empty($data['result']['order_item_list'])) {
if(empty($data['result']['order_item_list'])) {
throw new PayException(['cus' => 14]);
}
$metadata = [
'app_id' => 'merchant-c',
'pingxx_id' => PingxxService::getInstance($pingxx_id)->getAppId(),
'environ' => Application::app()->environ(),
'order_id' => $order_id,
'goods' => [],
'marketing' => [],
'order_item_id' => [],
'app_id'=>'merchant-c',
'pingxx_id'=>PingxxService::getInstance($pingxx_id)->getAppId(),
'environ'=>Application::app()->environ(),
'order_id'=>$order_id,
'goods'=>[],
'marketing'=>[],
'order_item_id'=>[],
];
$min_expire_time = $now = time();
foreach ($data['result']['order_item_list'] as $item) {
if (strtotime($item['expiration_time']) < $min_expire_time) {
if(strtotime($item['expiration_time']) < $min_expire_time) {
$min_expire_time = strtotime($item['expiration_time']);
}
if (empty($metadata['goods'][$item['goods_sku_id']])) {
if(empty($metadata['goods'][$item['goods_sku_id']])) {
$metadata['goods'][$item['goods_sku_id']] = 0;
}
$metadata['goods'][$item['goods_sku_id']] += $item['goods_num'];
if (!empty($item['marketing_id'])) {
if(!empty($item['marketing_id'])) {
$metadata['marketing'][$item['goods_sku_id']] = $item['marketing_id'];
}
array_push($metadata['order_item_id'], $item['order_item_id']);
}
if ($now > $min_expire_time) {
if($now > $min_expire_time) {
throw new PayException(['cus' => 15]);
}
//订单过期时间必须满足 70s+
if ($pay_expiration_time - $now < 70) {
if($pay_expiration_time - $now < 70 ) {
$pay_expiration_time = $now + 70;
}
$pay_order_id = $this->gen_id_by_blend('pay_order_id');
$pay_order_id = $this->gen_pay_order_id($user_id);
$new_pay_order = [
'pay_order_id' => $pay_order_id,
'user_id' => $order_info['user_id'],
......@@ -126,7 +125,7 @@ class PayService
}
//不支持修改订单过期时间,再次调用支付服务
if (strtotime($pay_order['expire_time']) < time()) {
if(strtotime($pay_order['expire_time']) < time()) {
throw new PayException(['cus' => 16]);
}
......@@ -135,14 +134,14 @@ class PayService
}
// 已经创建,需要查询返回
if (!empty($pay_order['third_order_id'])) {
if(!empty($pay_order['third_order_id'])) {
$ret = PingxxService::getInstance($pingxx_id)->getOrder($pay_order['third_order_id']);
} else {
$ret = PingxxService::getInstance($pingxx_id)->createOrder($pay_order, $metadata);
}
if (!empty($ret['error'])) {
throw new BaseException(['msg' => $ret['error']['message'], 'code' => '2301']);
throw new BaseException(['msg'=>$ret['error']['message'], 'code'=>'2301']);
}
if ($ret["status"] == 'paid' || $ret['status'] == 'refunded') {
......@@ -174,16 +173,16 @@ class PayService
}
//存在一个继续支付单订单
if ($pay_order_id != $pay_order['pay_order_id']) {
if($pay_order_id != $pay_order['pay_order_id']) {
$ret['exist'] = true;
}
$channel_exist = false;
$channel = $this->getChannel($pay_method_id);
if (!empty($ret['charges']['data'])) {
if(!empty($ret['charges']['data'])) {
//是否存在
foreach ($ret['charges']['data'] as $ch) {
if ($ch['channel'] == $channel) {
if($ch['channel'] == $channel) {
$channel_exist = true;
}
}
......@@ -191,18 +190,18 @@ class PayService
}
//判断是否做过pay操作 charge
if ($channel_exist == false) {
if($channel_exist == false) {
// 创建支付单
$payment = [
'charge_order_no' => $pay_order['pay_order_id'],
'charge_amount' => $pay_order['pay_amount'],
'channel' => $channel,
'extra' => $extra,
'charge_order_no'=> $pay_order['pay_order_id'],
'charge_amount'=> $pay_order['pay_amount'],
'channel'=> $channel,
'extra'=> $extra,
];
$pay = PingxxService::getInstance($pingxx_id)->pay($ret['id'], $payment);
if (!empty($pay['error'])) {
throw new BaseException(['msg' => $pay['error']['message'], 'code' => '2002']);
throw new BaseException(['msg'=>$pay['error']['message'], 'code'=>'2002']);
}
return $pay;
......@@ -219,20 +218,20 @@ class PayService
public function call_back($data)
{
if (!in_array($data['type'], ['order.succeeded', 'charge.succeeded'])) {
return ['error' => '支付回调类型不正确'];
return ['error'=>'支付回调类型不正确'];
}
if (empty($data['data']['object'])) {
return ['error' => '支付回调格式不正确'];
return ['error'=>'支付回调格式不正确'];
}
$object = $data['data']['object'];
if (empty($object['metadata']['app_id']) || $object['metadata']['app_id'] != "merchant-c") {
if(empty($object['metadata']['app_id']) || $object['metadata']['app_id'] != "merchant-c") {
//非支付中心发出的订单,需要忽略
return ['error' => '非merchant渠道订单,不做处理。'];
return ['error'=>'非merchant渠道订单,不做处理。'];
}
if ($data['type'] == 'charge.succeeded') {
if($data['type'] == 'charge.succeeded') {
$pay_order_id = $object['order_no'];
$paid = $object['paid'];
$amount = $object['amount'];
......@@ -242,15 +241,15 @@ class PayService
$amount = $object['actual_amount'];
}
if ($paid != true) {
return ['error' => '支付状态不正确,不做处理。'];
if($paid != true) {
return ['error'=>'支付状态不正确,不做处理。'];
}
$pay_order = PayOrder::get('*', ['pay_order_id' => $pay_order_id]);
if (empty($pay_order)) {
throw new PayException(['cus' => 7]);
}
if ($pay_order['pay_amount'] != $amount) {
if($pay_order['pay_amount'] != $amount) {
throw new PayException(['cus' => 8]);
}
......@@ -266,7 +265,7 @@ class PayService
$cnt = PayOrder::update($edit, $where);
//保持幂等返回成功
if ($cnt == 0) {
if($cnt == 0) {
return ['pay_order_id' => $pay_order_id];
}
......@@ -282,14 +281,14 @@ class PayService
$this->make_order_clearing($ret['result']);
//收集第一商品名称
if ($this->order_items) {
if($this->order_items) {
$title = $this->order_items[0]['goods_name'] ?? '商品名称';
} else {
$title = '';
}
try {
try{
//通知消息
$app_id = 'wx65e9ba99c333444f';
$template_id = 'EzOH2ZpfW-eUdFf9gzjZTAv-Z9PFmKHNsQ0VOGvvN7c';
......@@ -302,12 +301,12 @@ class PayService
date('Y-m-d'),
];
if (isset($ret['result']['order_info']['business_from']) && $ret['result']['order_info']['business_from'] != 2) {
if(isset($ret['result']['order_info']['business_from']) && $ret['result']['order_info']['business_from'] != 2) {
//接龙业务不发送消息
Msg::send($pay_order['user_id'], $app_id, $template_id, $page_url, $type, $query);
}
} catch (\Throwable $e) {
}catch (\Throwable $e) {
;
}
......@@ -321,49 +320,48 @@ class PayService
* @throws PayException
* 返回订单状态及订单渠道列表
*/
public function pay_charges($order_id)
{
public function pay_charges($order_id) {
$pay_order = PayOrder::get('*', ['order_id' => $order_id]);
if (empty($pay_order)) {
$pay_order = PayOrder::get('*', ['order_id'=>$order_id]);
if(empty($pay_order)) {
throw new PayException(['cus' => 18]);
}
$data = [
'status' => 'created',
'max_expire_time' => $pay_order['expire_time'],
'list' => []
'status'=>'created',
'max_expire_time'=>$pay_order['expire_time'],
'list'=>[]
];
$max_expire_time = strtotime($pay_order['expire_time']);
if ($max_expire_time < time()) {
if($max_expire_time < time()) {
$data["status"] = 'canceled';
}
if (empty($pay_order['third_order_id'])) {
if(empty($pay_order['third_order_id'])) {
return $data;
}
//paid、refunded、canceled、created
$ret = PingxxService::getInstance($pay_order['pingxx_id'])->getOrder($pay_order['third_order_id']);
if (!empty($ret['error'])) {
throw new BaseException(['msg' => $ret['error']['message'], 'code' => '2301']);
throw new BaseException(['msg'=>$ret['error']['message'], 'code'=>'2301']);
}
$data["status"] = $ret["status"];
if (!empty($ret['charges']['data'])) {
if(!empty($ret['charges']['data'])) {
//是否存在
foreach ($ret['charges']['data'] as $ch) {
$data["list"][] = [
'paid' => $ch['paid'],
'refunded' => $ch['refunded'],
'channel' => $ch['channel'],
'time_expire' => $ch['time_expire'],
'paid'=>$ch['paid'],
'refunded'=>$ch['refunded'],
'channel'=>$ch['channel'],
'time_expire'=>$ch['time_expire'],
];
if ($max_expire_time < $ch['time_expire']) {
if($max_expire_time < $ch['time_expire']) {
$max_expire_time = $ch['time_expire'];
}
}
......@@ -383,37 +381,37 @@ class PayService
{
$cnt = 0;
//锁定状态
try {
try{
PayOrderItem::beginTransaction();
$item = PayOrderItem::getMaster('*', ['order_item_id' => $order_item_id]);
if (empty($item)) {
throw new PayException(['cus' => 10]);
if(empty($item)) {
throw new PayException(['cus'=>10]);
}
if ($item['refund_order_status'] != 0) {
throw new PayException(['cus' => 9]);
if($item['refund_order_status'] != 0) {
throw new PayException(['cus'=>9]);
}
$pay_order = PayOrder::getMaster('*', ['pay_order_id' => $item['pay_order_id']]);
if (empty($pay_order) || $pay_order['life_account_id'] != $life_account_id) {
throw new PayException(['cus' => 11]);
$pay_order = PayOrder::getMaster('*', ['pay_order_id'=>$item['pay_order_id']]);
if(empty($pay_order) || $pay_order['life_account_id'] != $life_account_id) {
throw new PayException(['cus'=>11]);
}
$cnt = PayOrderItem::update([
'notify_account_status' => 0,
'can_notify_account' => 1,
'can_notify_account_time' => date('Y-m-d H:i:s'),
], ['order_item_id' => $order_item_id, 'can_notify_account' => 0, 'refund_order_status' => 0]);
], ['order_item_id' => $order_item_id, 'can_notify_account'=>0, 'refund_order_status'=>0]);
PayOrderItem::commit();
} catch (\Exception $e) {
}catch (\Exception $e) {
PayOrderItem::rollback();
throw new BaseException(['msg' => $e->getMessage(), 'code' => $e->getCode()]);
throw new BaseException(['msg'=>$e->getMessage(), 'code'=>$e->getCode()]);
}
if ($cnt == 0) {//幂等直接返回
if($cnt == 0) {//幂等直接返回
return ['order_item_id' => $order_item_id];
}
......@@ -427,14 +425,13 @@ class PayService
* @throws \App\Exception\custom\CodeSpecialException
* 同步钱包处理
*/
public function sync_account_wallet($order_item_id)
{
public function sync_account_wallet($order_item_id) {
$cnt = PayOrderItem::update([
'notify_account_status' => 1,
], ['order_item_id' => $order_item_id, 'can_notify_account' => 1, 'notify_account_status' => 0]);
], ['order_item_id' => $order_item_id, 'can_notify_account' => 1, 'notify_account_status'=>0]);
if ($cnt == 0) {//乐观锁,不满足条件不处理
if($cnt ==0) {//乐观锁,不满足条件不处理
return true;
}
......@@ -445,12 +442,12 @@ class PayService
$wallet_list = [];
foreach ($clearing_items as $row) {
if ($row['pay_amount'] <= 0) {
if($row['pay_amount'] <= 0) {
continue;
}
//处理商家结算价格的兼容
if ($row['clear_payment'] > 0 && $row['pay_sub_type'] == 102) {
if($row['clear_payment'] > 0 && $row['pay_sub_type'] == 102) {
$pay_amount = $row['clear_payment'];
} else {
$pay_amount = $row['pay_amount'];
......@@ -467,9 +464,9 @@ class PayService
];
}
try {
if ($wallet_list) { //同步成功,返回结果
WalletService::send(['list' => $wallet_list]);
try{
if($wallet_list) { //同步成功,返回结果
WalletService::send(['list'=>$wallet_list]);
}
PayOrderItem::update([
'notify_account_status' => 2,
......@@ -506,7 +503,7 @@ class PayService
$pay_order_items = [];
foreach ($this->order_items as $r) {
$pay_order_items[] = [
'pay_order_item_id' => $this->gen_id_by_blend('pay_order_item_id'),
'pay_order_item_id' => $this->gen_pay_order_item_id(),
'pay_order_id' => $this->pay_order['pay_order_id'],
'order_id' => $this->pay_order['order_id'],
'order_item_id' => $r['order_item_id'],
......@@ -521,10 +518,10 @@ class PayService
$merchant_cash = $this->order_info['payment'] - $wx_tip - $distribution_tip - $platform_tip;
//核对各项资金的合理性
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-service: 支付预结算失败,结算资金为负',
json_encode([$this->order_info['payment'], $wx_tip, $distribution_tip, $platform_tip, $merchant_cash]));
throw new PayException(['cus' => 12]);
throw new PayException(['cus'=>12]);
}
$this->make_merchant_clearing($merchant_cash); //商户收益
......@@ -549,11 +546,11 @@ class PayService
foreach ($this->order_items as $r) {
// 收集直接到账信息
//rule_refund 退款规则:1到期自动退款,2购买后不允许退款,3到期后不退款
if ($r['rule_refund'] == 2) { //分销商品一定是不可退
if($r['rule_refund'] == 2 ) { //分销商品一定是不可退
PayOrderItem::update([
'can_notify_account' => 1,
'can_notify_account_time' => date('Y-m-d H:i:s')
], ['order_item_id' => $r['order_item_id']]);
'can_notify_account'=>1,
'can_notify_account_time'=>date('Y-m-d H:i:s')
], ['order_item_id'=>$r['order_item_id']]);
}
}
}
......@@ -565,11 +562,11 @@ class PayService
*/
private function make_tip_clearing()
{
$wx_rate = config('pay', 'pay.wechat_rate') ?? 60; //微信利率 60 / 10000
$wx_account_id = config('pay', 'pay.wechat_account_id') ?? '1111';
$wx_rate = config('pay','pay.wechat_rate') ?? 60; //微信利率 60 / 10000
$wx_account_id = config('pay','pay.wechat_account_id') ?? '1111';
$float_tip = $wx_rate * $this->order_info['payment'] / 10000;
if ($float_tip >= 1) {
if($float_tip >= 1 ) {
$total_tip = round($float_tip);
} else {
$total_tip = 0;
......@@ -638,33 +635,33 @@ class PayService
$total_tip = 0;
foreach ($this->order_items as $r) {
//仅处理分销业务
if (empty($r['distributor_user_id']) || $r['marketing_type'] != 1) {
if(empty($r['distributor_user_id']) || $r['marketing_type'] != 1) {
continue;
}
$r['distributor_commission_value'] = intval($r['distributor_commission_value']);
$r['parent_commission_value'] = intval($r['parent_commission_value']);
$_one = $_two = 0;
if ($r['commission_mode'] == 1) {
if($r['commission_mode'] == 1) {
$_one = floor($r['distributor_commission_value'] * $this->order_info['total_price'] / 10000);
$_two = floor($r['parent_commission_value'] * $this->order_info['total_price'] / 10000);
} elseif ($r['commission_mode'] == 2) {
} elseif($r['commission_mode'] == 2) {
$_one = $r['distributor_commission_value'];
$_two = $r['parent_commission_value'];
}
$users[0] = [
'user_id' => $r['distributor_user_id'],
'value' => $r['distributor_commission_value'],
'amount' => $_one
'user_id'=>$r['distributor_user_id'],
'value'=>$r['distributor_commission_value'],
'amount'=>$_one
];
if (!empty($r['parent_user_id'])) {
if(!empty($r['parent_user_id'])) {
$users[1] = [
'user_id' => $r['parent_user_id'],
'value' => $r['parent_commission_value'],
'amount' => $_two
'user_id'=>$r['parent_user_id'],
'value'=>$r['parent_commission_value'],
'amount'=>$_two
];
}
......@@ -674,7 +671,7 @@ class PayService
}
$this->clear_items_list[] = [
'pay_order_clearing_item_id' => $this->gen_id_by_blend('pay_order_clearing_item_id'),
'pay_order_clearing_item_id' => $this->gen_pay_order_clearing_item_id(),
'pay_order_clearing_id' => '',
'pay_order_id' => $this->pay_order['pay_order_id'],
'order_id' => $this->pay_order['order_id'],
......@@ -704,14 +701,14 @@ class PayService
private function make_merchant_clearing($merchant_cash)
{
$ret = $this->get_life_account($this->order_info['life_account_id']);
if (empty($ret['result']['life_account_admin_id'])) {
if(empty($ret['result']['life_account_admin_id'])) {
$log_string = json_encode([
'life_account_id' => $this->order_info['life_account_id'],
'ret' => $ret,
'life_account_id'=>$this->order_info['life_account_id'],
'ret'=>$ret,
], JSON_UNESCAPED_UNICODE);
FileLog::error('pay-service:', '获取生活号管理员失败', $log_string);
throw new PayException(['cus' => 13]);
throw new PayException(['cus'=>13]);
}
$account = [
......@@ -735,7 +732,7 @@ class PayService
$cleared_amount = 0; //已经结算记录
$cleared_tip = 0; //已经结算记录
$pay_order_clearing_id = $this->gen_id_by_blend('pay_order_clearing_id');
$pay_order_clearing_id = $this->gen_pay_order_clearing_id();
$this->clear_list[] = [
'pay_order_clearing_id' => $pay_order_clearing_id,
'pay_order_id' => $this->pay_order['pay_order_id'],
......@@ -755,16 +752,16 @@ class PayService
foreach ($this->order_items as $r) {
$current_tip = Strategy::getTip($r['payment'], $total_amount, $cleared_amount, $total_tip, $cleared_tip);
$id = $this->gen_id_by_blend('pay_order_clearing_item_id');
$id = $this->gen_pay_order_clearing_item_id();
//商家资金需要显示商品名称
if ($account['pay_sub_type'] == 102) {
if($account['pay_sub_type'] == 102) {
$remark = $r['goods_name'] ?? '';
if (mb_strlen($remark) > 100) {
if(mb_strlen($remark) > 100) {
$remark = mb_substr($remark, 0, 100) . '...';
}
if (!empty($r['clear_price'])) {
if(!empty($r['clear_price'])) {
$clear_payment = $r['goods_num'] * $r['clear_price'];
} else {
$clear_payment = 0;
......@@ -796,62 +793,44 @@ class PayService
}
}
// private function gen_pay_order_id($user_id)
// {
// $number = substr($user_id, -2);
// $ids = $this->get_idgen_id($number, 1);
// return array_pop($ids);
// }
// private function gen_pay_order_item_id()
// {
// $number = substr($this->order_info['user_id'], -2);
// $ids = $this->get_idgen_id($number, 1);
// return array_pop($ids);
// }
private function gen_id_by_blend($type)
private function gen_pay_order_id($user_id)
{
$params = [
'type' => 'bp_' . $type,
];
$url = config('interface', 'bp.id.blend');
if (empty($url)) {
FileLog::error("interface", sprintf("%s 接口配置不存在", 'bp.id.blend'));
}
$res = (new Request())->post($url, $params);
if (!empty($res) && !empty($res['response']) && $res['response']['code'] == 0) {
return $res['response']['reason'];
} else {
FileLog::error("interface", sprintf("%s 接口调用失败", 'bp.id.blend'));
return -1;
}
$number = substr($user_id, -2);
$ids = $this->get_idgen_id($number, 1);
return array_pop($ids);
}
// private function gen_pay_order_clearing_id()
// {
// $number = substr($this->order_info['user_id'], -2);
// $ids = $this->get_idgen_id($number, 1);
// return array_pop($ids);
// }
//
// private function gen_pay_order_clearing_item_id()
// {
// $number = substr($this->order_info['user_id'], -2);
// $ids = $this->get_idgen_id($number, 1);
// return array_pop($ids);
// }
// private function get_idgen_id($number, $count = 1)
// {
// $number = intval($number);
// $res = Idgen::get(appConfig('idgen.partner'), appConfig('idgen.key'), [], [[
// "type" => "55c768",
// 'number' => $number,
// "count" => $count]]);
// $id = $res['id_datetime']['55c768'] ?? [];
// return $id;
// }
private function gen_pay_order_item_id()
{
$number = substr($this->order_info['user_id'], -2);
$ids = $this->get_idgen_id($number, 1);
return array_pop($ids);
}
private function gen_pay_order_clearing_id()
{
$number = substr($this->order_info['user_id'], -2);
$ids = $this->get_idgen_id($number, 1);
return array_pop($ids);
}
private function gen_pay_order_clearing_item_id()
{
$number = substr($this->order_info['user_id'], -2);
$ids = $this->get_idgen_id($number, 1);
return array_pop($ids);
}
private function get_idgen_id($number, $count = 1)
{
$number = intval($number);
$res = Idgen::get(appConfig('idgen.partner'), appConfig('idgen.key'), [], [[
"type" => "55c768",
'number' => $number,
"count" => $count]]);
$id = $res['id_datetime']['55c768'] ?? [];
return $id;
}
private function get_life_account($life_account_id)
{
......@@ -863,27 +842,25 @@ class PayService
return Sdk::call($url, $params);
}
private function getChannel($pay_method_id)
{
private function getChannel($pay_method_id) {
$maps = [
105 => "wx", //微信 App 支付
106 => "alipay", //支付宝
107 => "wx_pub", //微信 JSAPI 支付
108 => "wx_pub_qr", //微信 Native 支付
109 => "wx_wap", //微信 H5 支付
110 => "wx_lite", //微信小程序支付
105=>"wx", //微信 App 支付
106=>"alipay", //支付宝
107=>"wx_pub", //微信 JSAPI 支付
108=>"wx_pub_qr", //微信 Native 支付
109=>"wx_wap", //微信 H5 支付
110=>"wx_lite", //微信小程序支付
];
if (!empty($maps[$pay_method_id])) {
if(!empty($maps[$pay_method_id])) {
return $maps[$pay_method_id];
} else {
return $maps[105];
}
}
private function getNextTime($times = 0)
{
private function getNextTime($times = 0) {
return time() + pow(3, $times + 1);
}
}
\ 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