1. 程式人生 > >php對接app微信支付的請求介面

php對接app微信支付的請求介面

<?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);