Files
liche/api/controllers/wxapp/app/Payment.php
T
2021-08-13 10:56:24 +08:00

85 lines
3.0 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';
class Payment extends Wxapp{
public function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->load->model('apporder/order_purchase_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;
}
$this->config->load('wxpay');
$wx_config = $this->config->item('default');
$this->load->library('WechatPayV3');
$params = [
'merchantId' => $wx_config['mchid'],
'merchantSerialNumber' => $wx_config['merchantSerialNumber'],
'merchantPrivateKey' => $wx_config['merchantPrivateKey'],
'wechatpayCertificate' => $wx_config['wechatpayCertificate'],
];
$WechatPayV3 = new WechatPayV3($params);
$n_time = time();
$json = [
'sp_appid' => $wx_config['appid'],
'sp_mchid' => $wx_config['mchid'],
'sub_appid' => $wx_config['sub_appid'],
'sub_mchid' => $row['mch_id'],
'description' => $row['item_title'],
'out_trade_no' => $row['sid'],
'notify_url' => $notify_url,
'settle_info' => [
'profit_sharing' => true
],
'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;
}
}