微信和支付寶支付成功之後的回調方法

//微信回調地址
    public function actionNotify(){
       // $msg = array();
        $postStr = file_get_contents('php://input');
        //$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        $msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $out_trade_no = $msg['out_trade_no'];//訂單號
        if (empty($msg['out_trade_no'])) {
            return Helper::format_data(PARAM_ERROR,['data'=>'1']);    
        }
        $order = new Orders();
        if ($msg['result_code'] == 'SUCCESS') {
            $data = $order->find()->where(['order_sn'=>$out_trade_no])->one();
            $data->status = 2;
            $data->pay_time = time();
            if($data->save()){
                $wxpay = new Wxpay();
                $str = $wxpay->find()->where(['order_sn'=>$msg['out_trade_no']])->one();
                if(empty($str)){
                    $wxpay->user_id = $data->user_id;
                    $wxpay->total_fee = $msg['total_fee']/100;
                    $wxpay->order_sn = $msg['out_trade_no'];
                    $wxpay->time_end = $msg['time_end'];
                    $wxpay->create_time = time();
                    if($wxpay->save(false)){
                        return Helper::format_data(SUCC,$data->orders_id);
                    }    
                }
            }
        } else {
           return Helper::format_data(PARAM_ERROR, $msg);
        }
    }
    //支付寶回調
    public function actionNotifys(){
        $out_trade_no = $_POST['out_trade_no'];
        if (empty($_POST['out_trade_no'])) {
           return Helper::format_data(PARAM_ERROR,['id'=>'修改充值記錄沒有獲取POST']);
        }
        if(!empty($_POST['trade_status'])){         //狀態值不爲空
                //檢查該帳單是否已支付.....
                if($_POST['trade_status'] == 'TRADE_FINISHED' OR $_POST['trade_status']  == 'TRADE_SUCCESS') {
                    $order = new Orders();
                    $data = $order->find()->where(['order_sn'=>$_POST['out_trade_no']])->one();
                    if(empty($data)){
                         return Helper::format_data(PARAM_ERROR,['id'=>'無效的訂單號']);
                    }
                    $data->status = 2;
                    $data->pay_time = time();
                    $zfbpay = new Zfbpay();
                    $str = $zfbpay->find()->where(['order_sn'=>$_POST['out_trade_no']])->one();
                    if(!empty($str)){
                         return Helper::format_data(PARAM_ERROR,['id'=>'已經存在']);
                    }
                    $zfbpay->user_id = $data->user_id;
                    $zfbpay->total_fee = $_POST['invoice_amount'];
                    $zfbpay->order_sn = $_POST['out_trade_no'];
                    $zfbpay->create_time = time();
                    if($data->save() && $zfbpay->save(false)){
                        return Helper::format_data(SUCC,$data->orders_id);
                    }
                }else {
                   return Helper::format_data(PARAM_ERROR, $_POST);
                }
            }
    }
    //將XML轉爲array
    public function actionxmlToArray($xml)
    {
        //禁止引用外部xml實體
        libxml_disable_entity_loader(true);
        $array = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $array;
    }
相關文章
相關標籤/搜索