public function notify()
 {
     Log::record('cftpay notify get:' . print_r($_GET, true));
     Log::save();
     // 由于二级域名redirect的原因,我们会自动在GET参数里添加shop=merchant_code,需要显示的去掉该参数
     unset($_GET['shop']);
     unset($_GET['_URL_']);
     $out_trade_no = $_GET['sp_billno'];
     $transaction_id = $_GET['transaction_id'];
     if (empty($out_trade_no) || empty($transaction_id)) {
         echo 'fail';
         exit;
     }
     // verify whether trade record existing in our db for <order_sn>
     $trade = M('b2c_cfttrade')->where(array('order_sn' => $out_trade_no))->find();
     if (empty($trade)) {
         Log::record('cftpay notify non existing order_sn get:' . print_r($_GET, true));
         Log::save();
         echo 'fail';
         exit;
     }
     // trade status verification
     if ($trade['n_pay_result'] != -1) {
         // invalid state: this may be caused by wx server retry logics, ignore this request
         Log::record('cftpay duplicated cft notify entries. trade status:' . $trade['n_pay_result'] . ' get:' . print_r($_GET, true));
         Log::save();
         $updateData = array();
         M('b2c_cfttrade')->where(array('id' => $trade['id']))->setInc('trade_notify_times');
         // inform cft with SUCCESS stats
         echo 'success';
         exit;
     }
     // get cftpay config
     $token = $trade['token'];
     $payment = M('b2c_payment')->where(array('token' => $token, 'pay_code' => 'cftpay'))->find();
     $cftpay_config = unserialize($payment['pay_config']);
     if (empty($cftpay_config)) {
         Log::record('cftpay fail: empty cft config. get:' . print_r($_GET, true));
         Log::save();
         echo 'fail';
         exit;
     }
     $partnerId = $cftpay_config['partnerId'];
     $partnerKey = $cftpay_config['partnerKey'];
     // url decode: get raw get parameter value for verification
     $DECODED_GET = array();
     foreach ($_GET as $k => $v) {
         $DECODED_GET[$k] = urldecode($v);
     }
     import("@.ORG.CftWapPayHelper");
     // url signature verification
     $cftWapPayHelper = new CftWapPayHelper($partnerId, $partnerKey);
     foreach ($DECODED_GET as $k => $v) {
         $cftWapPayHelper->setParameter($k, $v);
     }
     if (!$cftWapPayHelper->isTenpaySign()) {
         // sign mismatch
         Log::record('cftpay url sign mismatch: .GET:' . print_r($_GET, true));
         Log::save();
         echo 'fail';
         exit;
     }
     Log::record('cftpay url sign verification passed!', Log::INFO);
     Log::save();
     // get pay result
     $pay_result = $_GET['pay_result'];
     // ask CFT server for confirmation
     $query_order_helper = new CftWapPayHelper($partnerId, $partnerKey);
     $query_order_helper->setParameter('ver', '2.0');
     $query_order_helper->setParameter('bargainor_id', $partnerId);
     $query_order_helper->setParameter('transaction_id', $transaction_id);
     $query_order_helper->setParameter('charset', '1');
     $query_order_resp = $query_order_helper->query_order();
     $confirm_pay_result = (string) $query_order_resp->pay_result;
     if (strcasecmp($pay_result, $confirm_pay_result) != 0) {
         // pay result mismatch, ignore
         Log::record('cftpay pay result mismatch. notify:' . $pay_result . ' confirm:' . $confirm_pay_result);
         Log::save();
         echo 'fail';
         exit;
     }
     Log::record('cftpay pay_result confirm passed order:' . $out_trade_no . ' trade status:' . $pay_result, Log::INFO);
     Log::save();
     // save data to db
     unset($audit);
     $audit['n_charset'] = $_GET['charset'];
     $audit['n_bank_type'] = $_GET['bank_type'];
     $audit['n_bank_billno'] = $_GET['bank_billno'];
     $audit['n_pay_result'] = intval($_GET['pay_result']);
     $audit['n_pay_info'] = $_GET['pay_info'];
     $audit['n_purchase_alias'] = $_GET['purchase_alias'];
     $audit['n_bargainor_id'] = $_GET['bargainor_id'];
     $audit['n_transaction_id'] = $_GET['transaction_id'];
     $audit['n_total_fee'] = intval($_GET['total_fee']);
     $audit['n_fee_type'] = intval($_GET['fee_type']);
     $audit['n_time_end'] = $_GET['time_end'];
     $audit['trade_notify_timestamp'] = time();
     $audit['trade_notify_times'] = 1;
     // if pay is successful, update order status
     if ($audit['n_pay_result'] == 0) {
         $updateData = array();
         $updateData['status'] = 2;
         // 已付款
         $updateData['update_time'] = time();
         $ret = M('b2c_order')->where(array('sn' => $out_trade_no))->save($updateData);
         //减库存
         ShopAction::minusInventory($out_trade_no);
         if ($ret === FALSE) {
             $err = M('b2c_order')->getDbError();
             Log::record('cftpay update order status fail. err:' . $err);
             Log::save();
             echo 'fail';
             // wait wx retry
             exit;
         }
     }
     Log::record('cftpay order:' . $out_trade_no . ' update order status succeed.', Log::INFO);
     Log::save();
     // update trade status. THIS SHOULD BE AFTER ORDER STATUS UPDATE.
     $ret = M('b2c_cfttrade')->where(array('id' => $trade['id']))->save($audit);
     if ($ret === FALSE) {
         $err = M('b2c_cfttrade')->getDbError();
         Log::record('cftpay order:' . $out_trade_no . ' update cfttrade table fail.err:' . $err);
         Log::save();
         echo 'fail';
         exit;
     }
     // well done
     Log::record('cftpay order:' . $out_trade_no . ' succeed.', Log::INFO);
     Log::save();
     echo 'success';
     exit;
 }
 public function notify()
 {
     $shop = null;
     //$notify_data = $alipayNotify->decrypt($_POST['notify_data']);
     $notify_data = $_POST['notify_data'];
     Log::record("Alipay notify data:" . print_r($notify_data, true), Log::INFO);
     //解析notify_data
     //注意:该功能PHP5环境及以上支持,需开通curl、SSL等PHP配置环境。建议本地调试时使用PHP开发软件
     $doc = new DOMDocument();
     $doc->loadXML($notify_data);
     //商户订单号
     $out_trade_no = $doc->getElementsByTagName("out_trade_no")->item(0)->nodeValue;
     $Model = new Model();
     // 实例化一个model对象 没有对应任何数据表
     $shop = $Model->query("select o.token, p.pay_config  from tp_b2c_order as o JOIN tp_b2c_payment as p on p.token = o.token where p.pay_code ='alipay' and o.sn = '{$out_trade_no}'");
     if ($shop == null) {
         Log::record("Illegle order sn:" . $out_trade_no, Log::NOTICE);
         $this->error('非法订单号', U('Wap/Shop/error'));
     }
     if (is_array($shop) && is_string($shop[0]['pay_config'])) {
         $store = unserialize($shop[0]['pay_config']);
         $shop[0]['pay_account'] = $store['pay_account'];
         //合作身份者id,以2088开头的16位纯数字
         $shop[0]['partner'] = $store['alipay_pid'];
         //安全检验码,以数字和字母组成的32位字符
         //如果签名方式设置为“MD5”时,请设置该参数
         $shop[0]['key'] = $store['alipay_key'];
     }
     Log::record("shop_config:" . print_r($shop, true), Log::INFO);
     import("@.ORG.AliWapPay.AlipayNotify");
     //计算得出通知验证结果
     $alipayNotify = new AlipayNotify($this->setconfig($shop[0]['partner'], $shop[0]['key']));
     $verify_result = $alipayNotify->verifyNotify();
     if ($verify_result) {
         if (!empty($doc->getElementsByTagName("notify")->item(0)->nodeValue)) {
             //支付宝交易号
             $trade_no = $doc->getElementsByTagName("trade_no")->item(0)->nodeValue;
             //交易状态
             $trade_status = $doc->getElementsByTagName("trade_status")->item(0)->nodeValue;
             $buyer_id = $doc->getElementsByTagName("buyer_id")->item(0)->nodeValue;
             //交易创建时间
             $gmt_create_time = strtotime($doc->getElementsByTagName("gmt_create")->item(0)->nodeValue);
             //用户付款时间
             $gmt_payment_time = $doc->getElementsByTagName("gmt_payment")->item(0)->nodeValue;
             $refund_status = $doc->getElementsByTagName("refund_status")->item(0)->nodeValue;
             $trade_db = M('b2c_trade');
             $trade_where = array('order_sn' => $out_trade_no);
             $trade = $trade_db->field('trade_id', 'token', 'trade_no')->where($trade_where)->find();
             if ($trade && empty($trade['trade_no'])) {
                 $data = array('status' => 2, 'trade_no' => $trade_no, 'buyer_id' => $buyer_id, 'alipay_create_time' => $gmt_create_time, 'payment_time' => $gmt_payment_time, 'refund_status' => $refund_status);
                 $trade_db->where($trade_where)->save($data);
                 $order_ret = M('b2c_order')->where(array('sn' => $out_trade_no))->save(array('status' => 2));
                 //减库存
                 ShopAction::minusInventory($out_trade_no);
             }
             Log::save();
             if ($trade_status == 'TRADE_FINISHED') {
                 //判断该笔订单是否在商户网站中已经做过处理
                 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
                 //如果有做过处理,不执行商户的业务程序
                 //注意:
                 //该种交易状态只在两种情况下出现
                 //1、开通了普通即时到账,买家付款成功后。
                 //2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。
                 //调试用,写文本函数记录程序运行情况是否正常
                 //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
                 echo "success";
                 //请不要修改或删除
             } else {
                 if ($trade_status == 'TRADE_SUCCESS') {
                     //判断该笔订单是否在商户网站中已经做过处理
                     //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
                     //如果有做过处理,不执行商户的业务程序
                     //注意:
                     //该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。
                     //调试用,写文本函数记录程序运行情况是否正常
                     //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
                     echo "success";
                     //请不要修改或删除
                 }
             }
         }
     } else {
         //验证失败
         echo "fail";
         Log::save();
         //调试用,写文本函数记录程序运行情况是否正常
         //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
     }
 }
 public function confirmOrder()
 {
     /* if ($this->is_need_auth) 
        {
            $this->error("您还未登录。");
        }*/
     $order_sn = intval($_GET['oid']);
     $order_db = M('b2c_order');
     $token = $this->_get('token');
     $where = array('sn' => $order_sn, 'token' => $token);
     if (!empty($this->branch_id)) {
         $where['branch_id'] = $this->branch_id;
     }
     $order = $order_db->where($where)->find();
     //检查权限
     if ($order == null || $order['wecha_id'] != $this->wechat_id || $order['status'] != 3) {
         $url = $this->generateUrl('Fxs/Shop/orderDetail', array('oid' => $order_sn));
         $this->error("您不能确认收货,请联系客服。", $url);
     }
     $status = $this->ORDER_STATUS_CONFIRM;
     if ($_GET['pay'] == 'cod') {
         $status = $this->ORDER_STATUS_PAYED;
         ShopAction::minusInventory($order_sn);
     }
     $order_db->where($where)->save(array('status' => $status));
     $url = $this->generateUrl('Shop/my', array('fxs_id' => $this->fxs_userid));
     $this->success("您的订单已经确认收货成功。", $url);
 }
 function change_order($data)
 {
     $out_trade_no = $data['out_trade_no'];
     $wecha_id = $data['openid'];
     $trade = M('b2c_wxtrade')->where(array('order_sn' => $out_trade_no, 'wecha_id' => $wecha_id))->find();
     if (empty($trade)) {
         Log::record('wxpay notify invalid notify request get: out_trade_no' . $out_trade_no);
         Log::save();
         echo 'fail';
         exit;
     }
     if ($trade['n_trade_state'] == -1) {
         $wxtrade = array('n_trade_state' => 0, 'n_bank_type' => $data['bank_type'], 'n_fee_type' => $data['fee_type'], 'n_IsSubscribe' => $data['is_subscribe'], 'n_NonceStr' => $data['nonce_str'], 'n_time_end' => $data['time_end'], 'n_transaction_id' => $data['transaction_id'], 'n_total_fee' => $data['total_fee']);
         M('b2c_wxtrade')->where(array('id' => $trade['id']))->save($wxtrade);
     }
     $pay_type = $_GET['pay_type'];
     if ($pay_type == '1') {
         M('hotel_order')->where(array('sn' => $out_trade_no))->save(array('order_status' => 6));
     } else {
         if ($pay_type == '2') {
             $order = M('dine_order')->where(array('sn' => $out_trade_no, 'status' => 2))->find();
         } else {
             $ret = M('b2c_order')->where(array('sn' => $out_trade_no))->save(array('status' => 2, 'update_time' => time()));
             if ($ret === FALSE) {
                 $err = M('b2c_order')->getDbError();
                 Log::record('wxpay notify update order status fail. out_trade_no: ' . $out_trade_no . ' err:' . $err);
                 Log::save();
                 echo 'fail';
                 exit;
             }
             //减库存
             ShopAction::minusInventory($out_trade_no);
         }
     }
 }
 function front_notify()
 {
     $orderId = $_POST['orderId'];
     $order = M('b2c_wingtrade')->where(array('order_sn' => $orderId, 'is_pay' => '0'))->find();
     if ($order) {
         if ($_POST['respCode'] == '00') {
             M('b2c_wingtrade')->where(array('order_sn' => $orderId))->save(array('is_pay' => '1', 'return_params' => serialize($_POST)));
         } else {
             M('b2c_wingtrade')->where(array('order_sn' => $orderId))->save(array('return_params' => serialize($_POST)));
         }
         $detail = unserialize($order['set_params']);
         if ($detail['pay_type'] == '1') {
             M('hotel_order')->where(array('sn' => $orderId))->save(array('order_status' => 6));
         } elseif ($detail['pay_type'] == '2') {
             M('dine_order')->where(array('sn' => $orderId))->save(array('status' => 3));
         } else {
             M('b2c_order')->where(array('sn' => $orderId))->save(array('status' => 2, 'update_time' => time()));
             //减库存
             ShopAction::minusInventory($orderId);
         }
         header('Location:' . $detail['return_url']);
     }
 }