Files
liche/api/controllers/wxapp/liche/Pay.php
T
2021-10-08 10:25:34 +08:00

147 lines
6.5 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by vim
* User: lcc
* Desc: 支付接口
* Date: 2021/06/29
* Time: 19:47
*/
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
require_once COMMPATH.'libraries/WechatPayV3.php';
class Pay extends Wxapp{
const MIN_PRICE = 5000; //最低单笔金额
public function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->load->model('app/liche/app_liche_orders_model');
$this->load->model('receiver/order/receiver_orders_model','orders_model');
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_series_model');
$this->load->model('biz/biz_model');
$this->uid = $this->session['uid'];
}
//支付
public function put(){
$sid = $this->input_param('sid');
$price = $this->input_param('price');
$row = $this->app_liche_orders_model->get(['sid'=>$sid,'uid'=>$this->session['uid']]);
$type_arr = $this->app_liche_orders_model->get_type_arr();
if(!$row){
throw new Exception('订单不存在', API_CODE_FAIL);
}
if($row['status']==1){
throw new Exception('订单已支付', API_CODE_FAIL);
}
if($row['type']==3){ //判断服务费是否支付
$srv_pay = $this->app_liche_orders_model->count(['o_id'=>$row['o_id'],'uid'=>$this->uid,'type'=>2,'status'=>0]);
if($srv_pay){
throw new Exception('请先支付委托服务费', API_CODE_FAIL);
}
}
if($row['type']==1){ //判断是否存在未支付意向金
$inten_pay = $this->app_liche_orders_model->count(['o_id'=>$row['o_id'],'uid'=>$this->uid,'type'=>4,'status'=>0]);
if($inten_pay){
throw new Exception('请先支付意向金', API_CODE_FAIL);
}
}
if($row['total_price']>0){
$url = http_host_com('api');
$notify_url = $url."/wxapp/{$this->app_key}/wxnotify_v3/v2";
if($row['type']==3){ //尾款多笔支付
$is_pay = $this->app_liche_orders_model->sum('total_price',['status'=>1,'uid'=>$this->uid,'pid'=>$row['id']]); //已支付金额
$need_pay = $row['total_price'] - $is_pay['total_price']; //需支付金额
if($need_pay<=0){
throw new Exception('订单已支付完成无需支付'.$need_pay, API_CODE_FAIL);
}
if($price && $price<self::MIN_PRICE){
throw new Exception('单笔金额不得低于'.self::MIN_PRICE, API_CODE_FAIL);
}
if($price>$need_pay){
throw new Exception('输入金额有误,你最高只需支付'.$need_pay, API_CODE_FAIL);
}
$total = $price ? $price : $need_pay;
$sub_order = $this->app_liche_orders_model->get(['total_price'=>$total,'uid'=>$this->uid,'pid'=>$row['id'],'status'=>0]); //金额相同未支付订单
if(!$sub_order){
$sid = create_order_no(350200,'liche',1,$row['type']);
$sub_data = [
'o_id' => $row['o_id'],
'sid' => $sid,
'uid' => $this->uid,
'mch_id' => $row['mch_id'],
'pid' => $row['id'],
'brand_id' => $row['brand_id'],
's_id' => $row['s_id'],
'v_id' => $row['v_id'],
'cor_id' => $row['cor_id'],
'incor_id' => $row['incor_id'],
'total_price' => $total,
'type' => 3,
'c_time' => time()
];
$res = $this->app_liche_orders_model->add($sub_data);
if(!$res){
throw new Exception('创建订单失败', API_CODE_FAIL);
}
$sub_order['sid'] = $sid;
}
$out_trade_no = $sub_order['sid'];
}else{
$total = $row['total_price'];
$out_trade_no = $row['sid'];
}
if($this->uid<=10){
$total = 0.01;
}
$order_row = $this->orders_model->get(['id'=>$row['o_id']],'name,brand_id,s_id,biz_id');
$brand_row = $this->auto_brand_model->get(['id'=>$order_row['brand_id']],'name');
$s_row = $this->auto_series_model->get(['id'=>$order_row['s_id']],'name');
$biz_row = $this->biz_model->get(['id'=>$order_row['biz_id']],'biz_name');
$type_name = $type_arr[$row['type']];
$description = "{$brand_row['name']}{$s_row['name']}-{$order_row['name']}-{$biz_row['biz_name']}-{$type_name}";
$this->config->load('wxpay');
$wx_config = $this->config->item('default');
$params = [
'merchantId' => $wx_config['mchid'],
'merchantSerialNumber' => $wx_config['merchantSerialNumber'],
'merchantPrivateKey' => $wx_config['merchantPrivateKey'],
'wechatpayCertificate' => $wx_config['wechatpayCertificate'],
];
$WechatPayV3 = new WechatPayV3($params);
$json = [
'sp_appid' => $wx_config['appid'],
'sp_mchid' => $wx_config['mchid'],
'sub_appid' => $wx_config['sub_appid'],
'sub_mchid' => $row['mch_id'],
'description' => $description,
'out_trade_no' => $out_trade_no,
'notify_url' => $notify_url,
'settle_info' => [
'profit_sharing' => false
],
'amount' => [
'total' => $total*100,
],
'payer' => [
'sub_openid' => $this->session['openid']
],
];
$noncestr = getNonceStr(20);
$resq = $WechatPayV3->unifiedOrder($json,$json['sub_appid'],$noncestr);
if(!$resq['code']){
debug_log("[下单失败]:" . $resq['msg'], 'underorder.log','wxpay');
throw new Exception('微信下单失败', API_CODE_FAIL);
}
$result = $resq['data'];
}else{
$this->load->service('apporder/payment_service', array('app_id' => $this->app_id));
$result = $this->payment_service->after_pay_liche($sid);
}
return $result;
}
}