84 lines
3.1 KiB
PHP
84 lines
3.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';
|
|
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['total_price']>0){
|
|
$url = http_host_com('api');
|
|
$notify_url = $url."/wxapp/{$this->app_key}/wxnotify";
|
|
$result = $this->pay($sid, $row['total_price']=0.01, $this->session['openid'], $row['item_title'], $notify_url);
|
|
}else{
|
|
$this->load->service('apporder/payment_service', array('app_id' => $this->app_id));
|
|
$result = $this->payment_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")); //交易起始时间
|
|
$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);
|
|
}
|
|
}
|
|
|
|
}
|