240 lines
11 KiB
PHP
240 lines
11 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('receiver/order/receiver_orders_v2_model');
|
|
|
|
$this->load->model('auto/auto_brand_model');
|
|
$this->load->model('auto/auto_series_model');
|
|
$this->load->model('auto/auto_attr_model');
|
|
|
|
$this->load->model('biz/biz_model');
|
|
$this->load->model("sys/sys_company_model");
|
|
|
|
$this->load->library('receiver/orders_entity');
|
|
$this->load->library('receiver/orders_v2_entity');
|
|
|
|
$this->uid = $this->session['uid'];
|
|
}
|
|
|
|
//获取订单信息
|
|
public function get_order(){
|
|
$sid = $this->input_param('sid');
|
|
$row = $this->app_liche_orders_model->get(['sid'=>$sid,'pid>'=>0]);
|
|
if(!$row){
|
|
throw new Exception('订单不存在', API_CODE_FAIL);
|
|
}
|
|
$order_type = $this->app_liche_orders_model->get_type_arr();
|
|
$order_info = [];
|
|
$price = $row['total_price'];
|
|
$pay_status = $row['status'] ? true:false;
|
|
if($row['type']!=4){
|
|
$order = $this->receiver_orders_v2_model->get(['id'=>$row['o_id']]);
|
|
$money_json = json_decode($order['money_json'],true);
|
|
//品牌车型
|
|
$brands = $this->auto_brand_model->get(['id'=>$order['brand_id']],'id,name');
|
|
//车系车型
|
|
$series = $this->auto_series_model->get(['id'=>$order['s_id']],'id,name','id,name');
|
|
$brand_name = $brands['name'] ? $brands['name'] : '';
|
|
$serie_name = $series['name'] ? $series['name'] : '';
|
|
//获取车辆属性
|
|
$attr_id_arr = [
|
|
$order['v_id'], $order['cor_id'], $order['incor_id']
|
|
];
|
|
$attrs = $this->auto_attr_model->get_map_by_ids($attr_id_arr,'id,title,jsondata');
|
|
$cover = '';
|
|
if($attrs[$order['cor_id']]){
|
|
$jsondata = json_decode($attrs[$order['cor_id']][0]['jsondata'],true);
|
|
$cover = build_qiniu_image_url($jsondata['img']);
|
|
}
|
|
$order_info = [
|
|
'title' => $brand_name.$serie_name,
|
|
'cover' => $cover,
|
|
'price' => $money_json['price_discount'] ? $money_json['price_car'] - $money_json['price_discount'] : $money_json['price_car'],
|
|
'version' => $attrs[$order['v_id']] ? $attrs[$order['v_id']][0]['title'] : '',
|
|
'color' => $attrs[$order['cor_id']] ? $attrs[$order['cor_id']][0]['title'] : '',
|
|
'in_color' => $attrs[$order['incor_id']] ? $attrs[$order['incor_id']][0]['title'] : '',
|
|
];
|
|
}
|
|
if($row['type']==3){ //尾款
|
|
$is_pay = $this->app_liche_orders_model->sum('total_price',['status'=>1,'pid'=>$row['id']]); //已支付尾款金额
|
|
$price = $row['total_price'] - $is_pay['total_price'];
|
|
if($price<=0){
|
|
$price = $row['total_price'];
|
|
$pay_status = true;
|
|
}
|
|
}
|
|
$data = [
|
|
'nickname' => $this->session['nickname'],
|
|
'headimg' => $this->session['headimg'],
|
|
'text' => '我在狸车平台上相中了一款好车,需要你帮忙付款,感恩~',
|
|
'type' => $row['type'],
|
|
'type_cn' => $order_type[$row['type']],
|
|
'price' => $price,
|
|
'pay_status' => $pay_status,
|
|
'order_info' => $order_info
|
|
];
|
|
return $data;
|
|
}
|
|
//支付
|
|
public function put(){
|
|
$sid = $this->input_param('sid');
|
|
$price = $this->input_param('price');
|
|
$type = $this->input_param('type');
|
|
if($type){ //代付
|
|
$row = $this->app_liche_orders_model->get(['sid'=>$sid]);
|
|
}else{
|
|
$row = $this->app_liche_orders_model->get(['sid'=>$sid,"(uid={$this->uid} or entrust_uid={$this->uid})"=>null]);
|
|
}
|
|
$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['o_id']<=Orders_v2_entity::V2_START_ID) {
|
|
if ($row['type'] == 3) { //判断服务费是否支付
|
|
$srv_pay = $this->app_liche_orders_model->count(['o_id' => $row['o_id'], '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'], '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,'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<$need_pay && $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,'pid'=>$row['id'],'status'=>0]); //金额相同未支付订单
|
|
if(!$sub_order){
|
|
$sid = create_order_no(350200,'liche');
|
|
$sub_data = [
|
|
'o_id' => $row['o_id'],
|
|
'sid' => $sid,
|
|
'uid' => $row['uid'],
|
|
'pay_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,
|
|
'company_id' => $row['company_id'],
|
|
'entrust_uid' => $row['entrust_uid'],
|
|
'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;
|
|
}
|
|
if($row['o_id']>=Orders_v2_entity::V2_START_ID){
|
|
$order_row = $this->receiver_orders_v2_model->get(['id'=>$row['o_id']],'name,brand_id,s_id,biz_id,main_type,info_json,owner_name');
|
|
if($order_row['main_type']){//公司
|
|
$info_json = json_decode($order_row['info_json'],true);
|
|
$name = $info_json['c_company'];
|
|
}else{
|
|
$name = $order_row['owner_name'];
|
|
}
|
|
}else{
|
|
$order_row = $this->orders_model->get(['id'=>$row['o_id']],'name,brand_id,s_id,biz_id');
|
|
$name = $order_row['name'];
|
|
}
|
|
$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,company_id');
|
|
//$company_row = $this->sys_company_model->get(['id'=>$biz_row['company_id']],'short');
|
|
//if($row['o_id']>=Orders_v2_entity::V2_START_ID){
|
|
// $company_row = $this->sys_company_model->get(['id'=>$row['company_id']],'short');
|
|
//}
|
|
$type_name = $type_arr[$row['type']];
|
|
//$description = "{$brand_row['name']}{$s_row['name']}-{$order_row['name']}-{$biz_row['biz_name']}-{$type_name}-{$company_row['short']}";
|
|
$description = "{$brand_row['name']}{$s_row['name']}-{$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;
|
|
}
|
|
|
|
}
|