Files
2021-09-10 13:41:50 +08:00

174 lines
7.1 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 Payment extends Wxapp{
private $type_array = [
3 => '定金',
4 => '定金',
5 => '委托服务费',
6 => '尾款',
];
public function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->load->model('apporder/order_purchase_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');
$row = $this->order_purchase_model->get(['sid'=>$sid,'app_id'=>$this->app_id,'app_uid'=>$this->session['uid']]);
if(!$row){
throw new Exception('订单不存在', API_CODE_FAIL);
}
if($row['status']>1){
throw new Exception('订单已支付', API_CODE_FAIL);
}
if($row['status']=1 && $row['status_detail']!=11){
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";
if($this->uid<=10){
$row['total_price'] = 0.01;
}
if($row['app_id']==1){ //狸车
$order_row = $this->orders_model->get(['id'=>$row['item_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 = $this->type_array[$row['type']];
$description = "{$brand_row['name']}{$s_row['name']}-{$order_row['name']}-{$biz_row['biz_name']}-{$type_name}";
}else{
$description = $row['item_title'];
}
$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' => $row['sid'],
'notify_url' => $notify_url,
'settle_info' => [
'profit_sharing' => false
],
'amount' => [
'total' => $row['total_price']*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($sid);
}
return $result;
}
//好店支付方式
public function put_hd(){
$sid = $this->input_param('sid');
$this->load->model('bobing/bobing_user_model', 'mdBobingUser');
$this->app_id = $this->mdBobingUser->appConfig()['act_key'];
$row = $this->order_purchase_model->get(['sid'=>$sid,'app_id'=>$this->app_id,'app_uid'=>$this->session['uid']]);
if(!$row){
throw new Exception('订单不存在', API_CODE_FAIL);
}
if($row['status']>1){
throw new Exception('订单已支付', API_CODE_FAIL);
}
if($row['total_price']>0){
$url = http_host_com('api');
$notify_url = $url."/wxapp/liche/wxnotify";
$result = $this->pay($sid, $row['total_price'], $this->session['openid'], $row['item_title'], $notify_url);
}else{
$this->load->service('apporder/payhd_service', array('app_id' => $this->app_id));
$result = $this->payhd_service->after_pay($sid);
}
return $result;
}
/**
* 支付方法
* @param $trade_no
* @param $price
* @param $openid
* @param $body
* @param $notify_url
* @param $attach
* @param $detail
* @return bool|json数据,可直接填入js函数作为参数|mixed
* @throws WxPayException
*/
private function pay($trade_no,$price,$openid,$body,$notify_url,$attach = '',$detail = ''){
if(!$body){return false;}
require_once APPPATH."third_party/WXconfig/liche_WxPay.Config.php";
require_once APPPATH."third_party/WXpay/WxPay.Api.php";
$config = new WxPayConfig();
$wxpay = new WxPayUnifiedOrder();
$wxpay->SetVersion('1.0');
$wxpay->SetBody($body); //简单描述
$attach && $wxpay->SetAttach($attach); //附加信息
$wxpay->SetNotify_url($notify_url);
$wxpay->SetOut_trade_no($trade_no); //订单号
$wxpay->SetTotal_fee($price * 100); //支付价格
$wxpay->SetTime_start(date("YmdHis")); //交易起始时间
$redis = & load_cache('redis');
$out_time_key = 'WX_TRADE_NO_OUT_'.$trade_no;
$out_time = $redis->get($out_time_key);
if(!$out_time){
$out_time = date("YmdHis",time() + 29*60*60);
$redis->save($out_time_key,$out_time,30*60*60);
}
$wxpay->SetTime_expire($out_time); //交易结束时间
$wxpay->SetTrade_type("JSAPI"); //设置交易类型
$wxpay->SetOpenid($openid); //openid
$detail && $wxpay->SetDetail($detail);
$return = WxPayApi::unifiedOrder($config, $wxpay); //统一支付
if($return['result_code'] == 'SUCCESS') {
$wxpay_api = new WxPayJsApiPay();
$jsApiParameters = WxPayApi::GetJsApiParameters($return, $config, $wxpay_api);
$jsApiParameters = json_decode($jsApiParameters, true);
return $jsApiParameters;
}else{
throw new Exception($return['return_msg']?$return['return_msg'].$return['err_code_des']:$return['return_msg'], API_CODE_FAIL);
}
}
}