1.获取请求参数接口
<?php
ini_set('date.timezone','Asia/Shanghai');
define("APP_TYPE", "app");
//require_once "../lib/WxPay.Api.php";
//require_once "WxPay.JsApiPay.php";
require_once(APPPATH."paylib/lib/WxPay.Api.php");
require_once(APPPATH."paylib/demo/WxPay.JsApiPay.php");
//①、获取用户openid
$tools = new JsApiPay();
//获取参数
$attach_value = $_POST['attach']?$_POST['attach']:$_GET['attach']; //其他参数
$body_value = $_POST['body']?$_POST['body']:$_GET['body']; //传输内容
$total_fee_value = $_POST['total_fee']?$_POST['total_fee']:$_GET['total_fee']; //支付的现金金额
$out_trade_no_value = $_POST['out_trade_no']?$_POST['out_trade_no']:$_GET['out_trade_no']; //支付订单号
$success_url = config_item('APP_DOMAIN');
$fail_url = config_item('APP_DOMAIN');;
$cancel_url = config_item('APP_DOMAIN');;
//②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody($body_value);
$input->SetAttach($attach_value);
$input->SetOut_trade_no($out_trade_no_value);
$input->SetTotal_fee($total_fee_value);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url(config_item('APP_DOMAIN')."Pay/notify_app");
$input->SetTrade_type("APP");
//$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
$order['success_url'] = $success_url;
$order['fail_url'] = $fail_url;
$order['cancel_url'] = $cancel_url;
$jsApiParameters = json_encode($order);
echo $jsApiParameters;exit();
2.微信通知接口
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
define("APP_TYPE", "app");
define('IN_ECS', true);
require_once APPPATH."paylib/lib/WxPay.Api.php";
require_once APPPATH.'paylib/lib/WxPay.Notify.php';
require_once APPPATH.'paylib/demo/log.php';
//初始化日志
$logHandler= new CLogFileHandler(APPPATH."paylib/logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
Log::DEBUG("query:start".date("Y-m-d H:i:s"));
//回调封装
class notify_app extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
// $this->load->library("session");
// $this->load->helper('url');
$this->load->model('Pay_model','pay');
}
/**商城回调
* @author xiexingqiao
* @param $order_sn 订单号
* @param $attach
* @param $flow_sn 微信流水号
* @param $total_fee 总金额 以元计算
* @param $pay_channel 支付渠道
* @param $pay_name 支付名称
*/
public function shop_pay_success($order_sn,$attach,$flow_sn,$total_fee,$pay_channel,$pay_name)
{
$this->db->trans_start();
$ret = $this->pay->shop_pay_success($order_sn,$attach,$flow_sn,$total_fee,$pay_channel,$pay_name);
$this->db->trans_complete();
echo json_encode($ret);
}
public function service_pay_success($order_sn,$attach,$flow_sn,$total_fee,$pay_channel,$pay_name)
{
$this->db->trans_start();
$ret = $this->pay->serivce_pay_success($order_sn,$attach,$flow_sn,$total_fee,$pay_channel,$pay_name);
$this->db->trans_complete();
echo json_encode($ret);
}
}
class PayNotifyCallBack extends WxPayNotify
{
//查询订单
public function Queryorder($transaction_id)
{
$input = new WxPayOrderQuery();
$input->SetTransaction_id($transaction_id);
$result = WxPayApi::orderQuery($input);
Log::DEBUG("query:" . json_encode($result));
if(array_key_exists("return_code", $result)
&& array_key_exists("result_code", $result)
&& $result["return_code"] == "SUCCESS"
&& $result["result_code"] == "SUCCESS")
{
return true;
}
return false;
}
//重写回调处理函数
public function NotifyProcess($data, &$msg)
{
Log::DEBUG("call back:" . json_encode($data));
$notfiyOutput = array();
if(!array_key_exists("transaction_id", $data)){
$msg = "输入参数不正确";
return false;
}
//查询订单,判断订单真实性
if(!$this->Queryorder($data["transaction_id"])){
$msg = "订单查询失败";
return false;
}
$transaction_id = $data["transaction_id"];
if($data['attach']=='shop_pay')
{
Log::DEBUG("query:start".date("Y-m-d H:i:s"));
//商城支付
$order_sn = $data['out_trade_no'];
$attach = 'shop_pay';
$flow_sn=$transaction_id;
$total_fee=$data['total_fee']/100;
$pay_channel ='weixinapp';
$pay_name = '微信支付';
$notify_obj = new notify_app();
$notify_obj->shop_pay_success($order_sn,$attach,$flow_sn,$total_fee,$pay_channel,$pay_name);
}elseif ($data['attach']=='service_pay')
{
Log::DEBUG("query:start".date("Y-m-d H:i:s"));
$order_sn = $data['out_trade_no'];
$attach = 'service_pay';
$flow_sn=$transaction_id;
$total_fee=$data['total_fee']/100;
$pay_channel ='weixinapp';
$pay_name = '微信支付';
$notify_obj = new notify_app();
$notify_obj->service_pay_success($order_sn,$attach,$flow_sn,$total_fee,$pay_channel,$pay_name);
}
//order_paid($order_sn, 2, APP_TYPE);
Log::DEBUG("NotifyProcess true");
return true;
}
}
Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();
$notify->Handle(false);