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

84 lines
3.5 KiB
PHP
Executable File

<?php
/**
* Created by PhpStorm.
* User: lcc
* Date: 2021/06/29
* Time: 20:17
*/
//微信支付回调
require_once APPPATH."third_party/WXpay/WxPay.Api.php";
class Wxnotify extends CI_Controller{
private $log_file = 'liche_pay.log';
private $log_dir = "wxapp_liche_pay";
private $app_id = 1;
private $notify;
public function __construct(){
parent::__construct();
$this->load->model('app/app_wxpaylog_model', 'wxpaylog_model');
$this->load->model('apporder/order_purchase_model','purchase_model');
$input = file_get_contents("php://input");
debug_log("[info] ". __FUNCTION__ . "# input:" . $input, $this->log_file);
//xml 转数组
$obj = simplexml_load_string($input,"SimpleXMLElement", LIBXML_NOCDATA);
$obj_array = json_decode(json_encode($obj),true);
$mch_id = $obj_array['mch_id'];
$config_file = APPPATH."third_party/WXconfig/liche_WxPay.Config.php";
if(!file_exists($config_file)){
debug_log("[error] ". __FUNCTION__ . ":商户配置文件不存在", $this->log_file);
exit();
}
try{
//如果返回成功则验证签名
$config = new WxPayConfig();
$wxpay = new WxPayNotifyResults();
$result = WxPayNotifyResults::Init($config, $input);
$this->notify = $result->GetValues();
}catch (WxPayException $e){
debug_log("[error] ". __FUNCTION__ . ":".$e->getMessage(), $this->log_file);
exit();
}
}
//支付回调
public function index(){
$sid = $this->notify['out_trade_no'];
if($sid&&$this->notify['out_trade_no']){
debug_log("[start] ". __FUNCTION__ . ": out_trade_no:".$this->notify['out_trade_no'], $this->log_file);
$order = $this->purchase_model->get(array('sid'=>$sid,'app_id'=>$this->app_id));
if(!$order){
debug_log("[error] ". __FUNCTION__ . ":{$sid}_订单不存在", $this->log_file);
}
//执行失败
$add = array(
'app_id' => $this->app_id,
'sid' => $sid?$sid:0,
'trade_no' => $this->notify['transaction_id'],
'notify_param' => json_encode($this->notify,JSON_UNESCAPED_UNICODE)
);
$ret = $this->wxpaylog_model->add($add);
if(!$ret){
debug_log("[error] ". __FUNCTION__ . ": sql:".$this->wxpaylog_model->db->last_query(), $this->log_file);
}
if($this->notify['result_code'] != 'SUCCESS'){ //支付失败
debug_log("[error] ". __FUNCTION__ . ":支付失败,sid={$sid},app_id=", $this->log_file);
}else{ //支付成功
$this->load->service('apporder/payment_service', array('app_id' => $this->app_id));
$result = $this->payment_service->after_pay($sid,$this->notify['cash_fee']/100);
if($result['code']){
debug_log("[success] ". __FUNCTION__ . ":操作成功", $this->log_file);
}else{
debug_log("[error] ". __FUNCTION__ . ":".$result['msg'], $this->log_file);
}
}
debug_log("[finish] ". __FUNCTION__ . ": out_trade_no:".$this->notify['out_trade_no'], $this->log_file);
}else{
debug_log("[finish] ". __FUNCTION__ . ": 参数错误:".json_encode($this->notify,JSON_UNESCAPED_UNICODE), $this->log_file);
}
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
return;
}
}