<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 15/12/10
 * Time: 下午6:01
 */

ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);

define(WXPAY_PATH, BASE_DATA_PATH.'/api/open_wxpay');

require_once (WXPAY_PATH . '/lib/OpenWxPay.Api.php');
require_once (WXPAY_PATH . '/lib/OpenWxPay.Config.php');
require_once (WXPAY_PATH . '/lib/OpenWxPay.Data.php');
require_once (WXPAY_PATH . '/lib/OpenWxPay.Exception.php');
require_once (WXPAY_PATH . '/lib/OpenWxPay.Notify.php');

require_once (BASE_DATA_PATH . '/logic/delivery.logic.php');
require_once (BASE_ROOT_PATH . '/helper/pay_helper.php');


//xml格式
//$content = '<xml><appid><![CDATA[wx24c5645aa986234a]]></appid>
//<attach><![CDATA[120503418859341217]]></attach>
//<bank_type><![CDATA[CFT]]></bank_type>
//<cash_fee><![CDATA[1]]></cash_fee>
//<fee_type><![CDATA[CNY]]></fee_type>
//<is_subscribe><![CDATA[N]]></is_subscribe>
//<mch_id><![CDATA[1279745801]]></mch_id>
//<nonce_str><![CDATA[smzubtf1eul75d7snt7qzqyh258hwns3]]></nonce_str>
//<openid><![CDATA[o-E2Rw9BKvBKnHgp41VYdxAHMJqg]]></openid>
//<out_trade_no><![CDATA[127974580120151214143419]]></out_trade_no>
//<result_code><![CDATA[SUCCESS]]></result_code>
//<return_code><![CDATA[SUCCESS]]></return_code>
//<sign><![CDATA[F43C02BBBB3B5E341D78EE477D67F598]]></sign>
//<time_end><![CDATA[20151214143425]]></time_end>
//<total_fee>1</total_fee>
//<trade_type><![CDATA[APP]]></trade_type>
//<transaction_id><![CDATA[1002150763201512142078937308]]></transaction_id>
//</xml>';

fcgi_header("Content-Type: text/html; charset=UTF-8");

try
{
    $content = $_SERVER['original_querystring'];
    Log::record("content = {$content}",Log::DEBUG);
    $result = OpenWxPayResults::Init($content);
    $ret_wx = array();
    $ret_code = $result['result_code'];
    Log::record("result_code= {$ret_code}",Log::DEBUG);
    if($ret_code == 'SUCCESS')
    {
        $out_trade_no = $result['out_trade_no'];
        $pay_sn = $result['attach'];
        $trade_no = $result['transaction_id'];

        Log::record("out_trade_no={$out_trade_no} and pay_sn={$pay_sn} and trade_no = {$trade_no}",Log::DEBUG);
        $cb_info = pay_helper::update_order($pay_sn,$trade_no,'wxpay');
        if($cb_info['state'] == false) {
            Log::record('wxpay error.',Log::DEBUG);
        }
        else
        {
            Log::record('wxpay success.',Log::DEBUG);

            if(is_pushoms()) {
                $logic_delivery = Logic('delivery');
                $ret = $logic_delivery->putOrder($pay_sn, $trade_no);
            }
            $pay = new pay_helper();
            $pay->OnSuccess($pay_sn);
        }

        $ret_wx['return_code'] = 'SUCCESS';
        $ret_wx['return_msg'] = 'OK';
    } else {
        Log::record('wxpay error 2.',Log::DEBUG);
        $ret_wx['return_code'] = 'FAIL';
        $ret_wx['return_msg'] = 'Error.';
    }

    $retoper = new OpenWxPayResults();
    $retoper->FromArray($ret_wx);
    $xml = $retoper->ToXml();
    Log::record("return to wx= {$xml}",Log::DEBUG);

    echo($xml);
} catch (OpenWxPayException $e){
    Log::record($e->getTraceAsString(),Log::ERR);
    $msg = $e->errorMessage();
    return false;
}

