echo "</html>";
            } else {
                QRcode::png($url);
            }
            die;
        }
        $res->redirect();
    } catch (\Exception $e) {
        var_dump($e->getMessage());
    }
}
if ($_GET['pay_type'] == 'APP') {
    wechat_redirect($config, 'prepay');
    exit;
}
$jsApi = new \Omnipay\Wechat\Sdk\JsApi();
$jsApi->init(array('app_id' => $config['app_id'], 'mch_id' => $config['mch_id'], 'app_secret' => $config['app_secret'], 'pay_sign_key' => $config['pay_sign_key'], 'cert_path' => $config['cert_path'], 'cert_key_path' => $config['cert_key_path']));
if (array_get($_GET, 'code', false)) {
    $jsApi->setCode($_GET['code']);
    //    var_dump($_GET);
    //    $url = $jsApi->createOauthUrlForOpenid();
    //Header('Location: '. $url);
    $open_id = $jsApi->getOpenid();
    $config['open_id'] = $open_id;
    //    var_dump($open_id);
    wechat_redirect($config);
} else {
    $r_link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $url = $jsApi->createOauthUrlForCode(urlencode($r_link));
    Header('Location: ' . $url);
}
 /**
  * @param $out_trade_no string 交易号
  * @param $total_fee float 总金额
  * @param $subject string
  * @param null|string $trade_type
  * @param bool $redirect 是否重定向
  * @return Response|\Symfony\Component\HttpFoundation\StreamedResponse
  * @throws Exception
  */
 public function wechat_pay_url($out_trade_no, $total_fee, $subject, $trade_type = null, $redirect = false)
 {
     $config = Config::get('payments.wechat', array());
     if (is_null($trade_type) || WechatExpressGateway::Trade_Type_JSAPI === $trade_type) {
         //JS API方式支付
         //初始化wechat js api
         $jsApi = new \Omnipay\Wechat\Sdk\JsApi();
         $jsApi->init(array('app_id' => $config['app_id'], 'mch_id' => $config['mch_id'], 'app_secret' => $config['app_secret'], 'pay_sign_key' => $config['pay_sign_key'], 'cert_path' => $config['cert_path'], 'cert_key_path' => $config['cert_key_path']));
         //回调parameters,GET方式传给回调url
         $parameters = array('out_trade_no' => $out_trade_no, 'total_fee' => $total_fee, 'subject' => $subject);
         $r_link = url('pay/wechat/jsapi?' . http_build_query($parameters));
         $url = $jsApi->createOauthUrlForCode(urlencode($r_link));
         if (false == $redirect) {
             //造二维码
             $qrFactory = new QRFactory();
             return $qrFactory->make($url)->response();
         } else {
             return \Response::make('', 302, array('Location' => $url));
         }
     }
     if (WechatExpressGateway::Trade_Type_NATIVE === $trade_type) {
         //native方式
         $gateway = $this->wechat();
         $parameters = array('out_trade_no' => $out_trade_no, 'total_fee' => $total_fee, 'subject' => $subject, 'mch_id' => array_get($config, 'mch_id'), 'notify_url' => array_get($config, 'notify_url'));
         $result = $gateway->prePurchase($parameters, WechatExpressGateway::Trade_Type_NATIVE)->send();
         $result = $result->getTransactionReference();
         if (is_array($result) && array_get($result, 'return_code') === 'SUCCESS') {
             $url = array_get($result, 'code_url');
             if (false == $redirect) {
                 $qrFactory = new QRFactory();
                 return $qrFactory->make($url)->response();
             } else {
                 return \Response::make('', 302, array('Location' => $url));
             }
         } else {
             $e = new Exception();
             $e->setExceptionMetaData('wechat-qrcode-native-api-fail', $result);
             throw $e;
         }
     }
 }