分销支付

This commit is contained in:
xucong 2024-11-07 15:12:22 +08:00
parent 18e379c892
commit 00597d2207
3 changed files with 104 additions and 0 deletions

@ -130,6 +130,22 @@ class Pay extends Api
$result = PayLogic::localWxPay($user_id,$post, $client);
return $result;
}
// 分销支付
public function distributionPay()
{
$post = $this->request->post();
$user_id = $this->user_id;
if(!isset($user_id)){
$user_id = $post['user_id'];
}
if(isset($this->client)){
$client = $this->client;
}else{
$client = 1;
}
$result = PayLogic::distributionPay($user_id,$post, $client);
return $result;
}
// 本地生活余额支付
public function localBalance()

@ -658,6 +658,36 @@ class PayLogic extends Logic
}
return JsonServer::success('微信支付发起成功', $res, 1);
}
//
public static function distributionPay($user_id,$post,$client){
$out_trade_no = createSn('user', 'order_no');
$res = WeChatPayServer::distributionserverWxPay($out_trade_no,$user_id,$post, $client);
if (false === $res) {
return JsonServer::error(WeChatPayServer::getError());
}
if ((is_object($res) || is_string($res)) && $client != Client_::pc) {
$res = (array)($res);
}
$data['order_sn'] = $out_trade_no;
$data['user_id'] = $user_id;
$data['order_amount'] = $post['money'];
$data['partner_id'] = $post['partner_id'];
$data['type_id'] = $post['type_id'];
$data['create_time'] = date("Y-m-d H:i:s");
$r = Db::name("local_pay")->save($data);
if (is_string($res)) {
$res['order_sn'] = $out_trade_no;
$data = [
'code' => 1,
'msg' => '微信支付发起成功',
'show' => 0,
'data' => $res
];
return json($data);
}
return JsonServer::success('微信支付发起成功', $res, 1);
}
//本地商家支付
public static function localWxPay($user_id,$post,$client){
// $post['lid'] = 10;

@ -205,6 +205,64 @@ class WeChatPayServer
return false;
}
}
public static function distributionserverWxPay($out_trade_no,$user_id,$post, $client){
try {
$order['user_id'] = $user_id;
$wechat_config = self::getWeChatConfig($order, $client);
$auth = $wechat_config['auth'];
$config = $wechat_config['config'];
$notify_url = $wechat_config['notify_url'];
$app = Factory::payment($config);
$attributes = [
'trade_type' => 'JSAPI',
'body' => '分销支付',
'total_fee' => $post['money'] * 100, // 单位:分
'notify_url' => $notify_url,
'openid' => $auth['openid'] ?? '',
'attach' => 'trade'
];
$attributes['out_trade_no'] = $out_trade_no. $attributes['trade_type'] . $client;
$result = $app->order->unify($attributes);
// var_dump($result);
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
//小程序,公众号
if (in_array($client, [Client_::mnp, Client_::oa])) {
$data = $app->jssdk->bridgeConfig($result['prepay_id'], false);
}
//app客户端
if (in_array($client, [Client_::ios, Client_::android])) {
$data = $app->jssdk->appConfig($result['prepay_id'], false);
}
//pc端
if ($client == Client_::pc) {
$data = self::getNativeCode($result, $order);
}
//h5(非微信环境)
if ($client == Client_::h5) {
$redirect_url = request()->domain() . '/mobile/pages/user_order/user_order';
$redirect_url = urlencode($redirect_url);
$data = $result['mweb_url'] . '&redirect_url=' . $redirect_url;
}
return $data;
} else {
if (isset($result['return_code']) && $result['return_code'] == 'FAIL') {
throw new Exception($result['return_msg']);
}
if (isset($result['err_code_des'])) {
throw new Exception($result['err_code_des']);
}
throw new Exception('未知原因');
}
} catch (Exception $e) {
self::$error = '支付失败:' . $e->getMessage();
return false;
}
}
/**
* @notes NATIVE 支付二维码
* @param $result