/**
  * 
  * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  * appid、mchid、spbill_create_ip、nonce_str不需要填入
  * @param WxPayUnifiedOrder $inputObj
  * @param int $timeOut
  * @throws WxPayException
  * @return 成功时返回,其他抛异常
  */
 public static function unifiedOrder($inputObj, $timeOut = 6, $notify = '')
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     //检测必填参数
     if (!$inputObj->IsOut_trade_noSet()) {
         throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
     } else {
         if (!$inputObj->IsBodySet()) {
             throw new WxPayException("缺少统一支付接口必填参数body!");
         } else {
             if (!$inputObj->IsTotal_feeSet()) {
                 throw new WxPayException("缺少统一支付接口必填参数total_fee!");
             } else {
                 if (!$inputObj->IsTrade_typeSet()) {
                     throw new WxPayException("缺少统一支付接口必填参数trade_type!");
                 }
             }
         }
     }
     //关联参数
     if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
     }
     if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
     }
     //异步通知url未设置,则使用配置文件中的url
     if (!$inputObj->IsNotify_urlSet()) {
         $inputObj->SetNotify_url($notify);
         //异步通知url
     }
     if ($inputObj->GetTrade_type() == 'APP') {
         $inputObj->SetAppid(WxPayConfig::APP_APPID);
         //开放平台APPID
         $inputObj->SetMch_id(WxPayConfig::APP_MCHID);
         //商户平台商户号
     } else {
         $inputObj->SetAppid(WxPayConfig::APPID);
         //公众账号ID
         $inputObj->SetMch_id(WxPayConfig::MCHID);
         //商户号
     }
     $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);
     //终端ip
     //$inputObj->SetSpbill_create_ip("1.1.1.1");
     $inputObj->SetNonce_str(self::getNonceStr());
     //随机字符串
     //签名
     $inputObj->SetSign();
     $xml = $inputObj->ToXml();
     $startTimeStamp = self::getMillisecond();
     //请求开始时间
     $response = self::postXmlCurl($xml, $url, false, $timeOut);
     $result = WxPayResults::Init($response);
     self::reportCostTime($url, $startTimeStamp, $result);
     //上报请求花费时间
     return $result;
 }
Beispiel #2
0
 /**
  * 
  * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  * appid、mchid、spbill_create_ip、nonce_str不需要填入
  * @param WxPayUnifiedOrder $inputObj
  * @param int $timeOut
  * @throws WxPayException
  * @return 成功时返回,其他抛异常
  */
 public static function unifiedOrder($inputObj, $timeOut = 6)
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     $inputObj->SetAppid(APPID);
     //公众账号ID
     $inputObj->SetMch_id(MCHID);
     //商户号
     $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);
     //终端ip
     //$inputObj->SetSpbill_create_ip("1.1.1.1");
     $inputObj->SetNonce_str(self::getNonceStr());
     //随机字符串
     //签名
     $inputObj->SetSign();
     $xml = $inputObj->ToXml();
     $startTimeStamp = self::getMillisecond();
     //请求开始时间
     $response = self::postXmlCurl($xml, $url, false, $timeOut);
     $result = WxPayResults::Init($response);
     return $result;
 }
 public function pay_get()
 {
     $order = I('get.order', '');
     $m = M('orders');
     // $map['from_member_id'] = $this->uid;
     $map['order_number'] = $order;
     // $map['status'] = 0;
     $info = $m->where($map)->find();
     if (empty($info)) {
         //未支付订单不存在
         $this->error(5015);
     }
     $total = $info['total'] * 100;
     $describe = 'testname';
     require_once APP_PATH . "SDK/Payment/wechat/lib/WxPay.Api.php";
     $input = new \WxPayUnifiedOrder();
     //设置公众账号ID
     $input->SetAppid(\WxPayConfig::APPID);
     //设置商户号
     $input->SetMch_id(\WxPayConfig::MCHID);
     //设置随机字符串
     $input->SetNonce_str(\WxPayApi::getNonceStr());
     //设置商品描述
     $input->SetBody($describe);
     //设置商户订单号
     $input->SetOut_trade_no($info['order_number']);
     //设置总金额
     $input->SetTotal_fee($total);
     //设置终端IP
     $input->SetSpbill_create_ip(get_client_ip());
     //设置通知地址
     $input->SetNotify_url(U('Wxpay/callback@api'));
     //设置交易类型
     $input->SetTrade_type('APP');
     $order = \WxPayApi::unifiedOrder($input);
     $this->success($order);
 }
Beispiel #4
0
 public function doMobileAjaxpay()
 {
     global $_GPC, $_W;
     load()->func('communication');
     $fromopenid = $_GPC['fromopenid'];
     $myopenid = $_W['openid'];
     $num = 1;
     $content = $_GPC['content'];
     include_once '../addons/beauty_zhongchou/WxPay.Api.php';
     $WxPayApi = new WxPayApi();
     $input = new WxPayUnifiedOrder();
     //				$key=$this->module['config']['apikey'];//商户支付秘钥(API秘钥)
     $key = 'xLeBESmggiQMGpba2ieADmRQDlDPmIL8';
     $account_info = pdo_fetch("select * from" . tablename('account_wechats') . "where uniacid={$_W['uniacid']}");
     //身份标识(appid)
     $appid = $account_info['key'];
     //$mchid=$this->module['config']['mchid'];//微信支付商户号(mchid)
     $mchid = '1239385202';
     $input->SetAppid($appid);
     $input->SetMch_id($mchid);
     $input->SetBody("test");
     $input->SetAttach("test");
     $input->SetOut_trade_no($mchid . date("YmdHis"));
     $input->SetTotal_fee($num);
     $input->SetTime_start(date("YmdHis"));
     $input->SetTime_expire(date("YmdHis", time() + 600));
     $input->SetGoods_tag("test");
     $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
     $input->SetTrade_type("JSAPI");
     $input->SetOpenid($myopenid);
     $result = $WxPayApi->unifiedOrder($input, 6, $key);
     //随机字符串
     $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
     $str = "";
     for ($i = 0; $i < 32; $i++) {
         $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
     }
     //签名
     $sign = $input->SetSign($key);
     $data = array();
     $data['pack'] = $result['prepay_id'];
     $data['appId'] = $appid;
     $data['timeStamp'] = TIMESTAMP;
     $data['nonceStr'] = $str;
     $data['signType'] = "MD5";
     $data['paySign'] = $sign;
     //生成订单
     //		$data2 = array(
     //			'uniacid' => $_W['uniacid'],
     //			'openid' => $myopenid,
     //			'ordersn' => date('Ymd').substr(time(), -5).substr(microtime(), 2, 5).sprintf('%02d', rand(0, 99)),
     //			'content'=>$content,
     //			'num' => $num,
     //			'status' => 0,//订单状态,-1取消状态,0普通状态,1为已付款,2为已发货,3为成功
     //			'fromopenid'=>$fromopenid,
     //			'content'=>
     //			'createtime' => TIMESTAMP
     //		);
     pdo_insert('beatuty_zhongchou_orders', $data2);
     $orderid = pdo_insertid();
     $order = pdo_fetch("SELECT * FROM " . tablename('beatuty_zhongchou_orders') . " WHERE id ={$orderid}");
     $params['tid'] = $order['ordersn'];
     $params['user'] = $_W['fans']['from_user'];
     $params['fee'] = $order['num'];
     $params['title'] = $_W['account']['name'];
     $params['ordersn'] = $order['ordersn'];
     $params['module'] = "beatuty_zhongchou";
     //转成json
     $dataall = array('data' => $data, 'params' => $params);
     return json_encode($dataall);
 }
 public function setbalance()
 {
     //登录验证
     $this->checkSessionUser();
     $this->layout = 'default_full';
     //引入模版
     $this->pageTitle = $this->ld['user_deposit'] . ' - ' . $this->configs['shop_title'];
     Configure::write('debug', 0);
     $this->loadModel('PaymentApiLog');
     if (!empty($_GET['code']) && !empty($_GET['other_data'])) {
         $other_data_str = $_GET['other_data'];
         $other_data_arr = explode("_", $other_data_str);
         $payment_api_id = isset($other_data_arr[2]) ? $other_data_arr[2] : 0;
         $payment_log_info = $this->PaymentApiLog->find('first', array('conditions' => array('PaymentApiLog.id' => $payment_api_id)));
         $this->data['pay']['money'] = isset($other_data_arr[1]) ? $other_data_arr[1] : 0;
         $this->data['pay']['payment_type'] = isset($other_data_arr[0]) ? $other_data_arr[0] : 0;
     }
     if ($this->RequestHandler->isPost() || isset($this->data['pay'])) {
         if (isset($this->data)) {
             $this->data = $this->clean_xss($this->data);
         }
         $pay_url = '';
         $message = '操作失败';
         $code = '0';
         if (isset($this->data['pay']) && !empty($this->data['pay'])) {
             $this->data['pay']['payment_type'] = intval($this->data['pay']['payment_type']);
             $this->data['pay']['money'] = floatval($this->data['pay']['money']);
             $payment = $this->Payment->find('first', array('conditions' => array('Payment.id' => $this->data['pay']['payment_type'], 'Payment.status' => '1')));
             if (isset($payment) && !empty($payment)) {
                 //用户Id
                 $user_id = $_SESSION['User']['User']['id'];
                 //获取用户信息
                 $user_info = $this->User->find('first', array('conditions' => array('User.id' => $user_id)));
                 //定义路径
                 $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
                 $this->set('payment_code', $payment['Payment']['code']);
                 if ($payment['Payment']['code'] == 'weixinpay') {
                     $payment_amount = $this->data['pay']['money'];
                     $amount_money = $payment_amount;
                     //在线支付增加api日志
                     $payment_api_log = array('id' => isset($payment_log_info['PaymentApiLog']['id']) ? $payment_log_info['PaymentApiLog']['id'] : 0, 'payment_code' => $payment['Payment']['code'], 'type' => 2, 'type_id' => $user_id, 'order_currency' => 'CHY', 'amount' => $payment_amount);
                     $this->PaymentApiLog->save($payment_api_log);
                     $payment_api_log['id'] = $this->PaymentApiLog->id;
                     $payment_config = unserialize($payment['Payment']['config']);
                     $amt = $amount_money * 100;
                     try {
                         $wechatpay_type = false;
                         if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
                             App::import('Vendor', 'Weixinpay', array('file' => 'WxPayPubHelper.php'));
                             $jsApi = new JsApi_pub($payment_config['APPID'], $payment_config['MCHID'], $payment_config['KEY'], $payment_config['APPSECRET']);
                             if (empty($_GET['code'])) {
                                 $request_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                                 $other_data = $this->data['pay']['payment_type'] . "_" . $this->data['pay']['money'] . "_" . $payment_api_log['id'];
                                 $request_url .= "?other_data=" . $other_data;
                                 //触发微信返回code码
                                 $wechat_pay_url = $jsApi->createOauthUrlForCode($request_url);
                                 Header("Location: {$wechat_pay_url}");
                             } else {
                                 //获取code码,以获取openid
                                 $code = $_GET['code'];
                                 $jsApi->setCode($code);
                                 $openid = $jsApi->getOpenId();
                             }
                             if (!empty($openid)) {
                                 $unifiedOrder = new UnifiedOrder_pub($payment_config['APPID'], $payment_config['MCHID'], $payment_config['KEY'], $payment_config['APPSECRET']);
                                 $unifiedOrder->setParameter("openid", "{$openid}");
                                 //商品描述
                                 $unifiedOrder->setParameter("body", "用户充值[金额:" . $payment_amount . "]");
                                 //商品描述
                                 //自定义订单号,此处仅作举例
                                 $timeStamp = time();
                                 $out_trade_no = $payment_api_log['id'];
                                 $unifiedOrder->setParameter("out_trade_no", "{$out_trade_no}");
                                 //商户订单号
                                 $unifiedOrder->setParameter("total_fee", $amt);
                                 //总金额
                                 $unifiedOrder->setParameter("notify_url", 'http://' . $host . $this->webroot . 'responds/weixin_balance');
                                 //通知地址
                                 $unifiedOrder->setParameter("trade_type", "JSAPI");
                                 //交易类型
                                 $prepay_id = $unifiedOrder->getPrepayId();
                                 $jsApi->setPrepayId($prepay_id);
                                 $jsApiParameters = $jsApi->getParameters();
                                 if (!empty($jsApiParameters)) {
                                     $json_result = json_decode($jsApiParameters);
                                     $code_url = isset($json_result->paySign) ? $jsApiParameters : '';
                                 }
                             } else {
                                 throw new SDKRuntimeException("支付失败,OpenId 获取失败");
                             }
                         } else {
                             Configure::write('debug', 0);
                             $this->layout = 'ajax';
                             $wechatpay_type = true;
                             App::import('Vendor', 'Weixinpay', array('file' => 'WxPay.Api.php'));
                             App::import('Vendor', 'Phpqcode', array('file' => 'phpqrcode.php'));
                             $input = new WxPayUnifiedOrder();
                             $input->SetKey($payment_config['KEY']);
                             $input->SetBody("用户充值[金额:" . $payment_amount . "]");
                             $input->SetAttach("用户充值");
                             $input->SetOut_trade_no($payment_api_log['id'] . "_" . time() . "_" . rand(0, 1000));
                             $input->SetAppid($payment_config['APPID']);
                             $input->SetMch_id($payment_config['MCHID']);
                             $input->SetTotal_fee($amt);
                             $input->SetTime_start(date("YmdHis"));
                             $input->SetTime_expire(date("YmdHis", time() + 600));
                             $input->SetGoods_tag("用户充值");
                             $input->SetNotify_url('http://' . $host . $this->webroot . 'responds/weixin_balance');
                             $input->SetProduct_id($payment_api_log['id']);
                             $input->SetTrade_type("NATIVE");
                             $notify = new NativePay();
                             $result = $notify->GetPayUrl($input);
                             $code_url = isset($result["code_url"]) ? $result["code_url"] : '';
                         }
                         $this->set('wechatpay_type', $wechatpay_type);
                         $message = '';
                         $code = '1';
                     } catch (Exception $e) {
                         $message = '支付失败,Caught exception: ' . $e->getMessage();
                         $code = '0';
                     }
                 } else {
                     //判断支付方式是否存在
                     $payment['Payment']['code'] = strtolower($payment['Payment']['code']);
                     try {
                         $payment_config = unserialize($payment['Payment']['config']);
                         App::import('Vendor', 'payments/' . $payment['Payment']['code']);
                         $balance_payment = new $payment['Payment']['code']();
                         if ($payment['Payment']['is_online'] == 1) {
                             //在线支付增加api日志
                             $payment_api_log = array('payment_code' => $payment['Payment']['code'], 'type' => 2, 'type_id' => $user_id, 'order_currency' => 'CHY', 'amount' => $this->data['pay']['money']);
                             $this->PaymentApiLog->save($payment_api_log);
                             //记录支付日志Id
                             $payment_api_log['id'] = $this->PaymentApiLog->id;
                         }
                         $payment_api_log['name'] = $user_info['User']['name'];
                         $payment_api_log['payerAdderss'] = $user_info['User']['address_id'];
                         $payment_api_log['payerName'] = $user_info['User']['name'];
                         $payment_api_log['created'] = date('Y-m-d H:i:s', time());
                         $payment_config['cancel_return'] = 'http://' . $host . $this->webroot;
                         $payment_config['return_url'] = 'http://' . $host . $this->webroot . 'responds/return_code/' . $payment['Payment']['code'];
                         //描述
                         $payment_api_log['subject'] = '[' . $user_info['User']['name'] . ']用户充值';
                         $payment_api_log['host'] = $host;
                         if ($payment['Payment']['code'] == 'money' || $payment['Payment']['code'] == 'bank_trans' || $payment['Payment']['code'] == 'pos_pay') {
                             $payment_config['co'] = '';
                         }
                         $api_code = $balance_payment->go2($payment_api_log, $payment_config);
                         $_SESSION['api_code'] = $api_code;
                         $message = '';
                         $code = '1';
                     } catch (Exception $e) {
                         $message = '支付失败,Caught exception: ' . $e->getMessage();
                         $code = '0';
                     }
                 }
             } else {
                 $message = '该支付方式无效或不可用!';
                 $code = '0';
             }
         }
         if (isset($api_code)) {
             $this->layout = null;
             $result['pay_url'] = isset($api_code) ? $api_code : $pay_url;
             $this->set('pay_url', $api_code);
         } else {
             if (isset($code_url) && $code_url != "") {
                 $this->set('pay_url', $code_url);
                 $this->set('payment_api_id', $payment_api_log['id']);
             } else {
                 //跳转到提示页
                 $this->flash($message, '/users/deposit', '');
             }
         }
     } else {
         $this->redirect('/users/deposit');
     }
 }
Beispiel #6
0
 /**
  * 
  * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  * appid、mchid、spbill_create_ip、nonce_str不需要填入
  * @param WxPayUnifiedOrder $inputObj
  * @param int $timeOut
  * @throws WxPayException
  * @return 成功时返回,其他抛异常
  */
 public static function unifiedOrder($inputObj, $timeOut = 6)
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     //检测必填参数
     if (!$inputObj->IsOut_trade_noSet()) {
         throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
     } else {
         if (!$inputObj->IsBodySet()) {
             throw new WxPayException("缺少统一支付接口必填参数body!");
         } else {
             if (!$inputObj->IsTotal_feeSet()) {
                 throw new WxPayException("缺少统一支付接口必填参数total_fee!");
             } else {
                 if (!$inputObj->IsTrade_typeSet()) {
                     throw new WxPayException("缺少统一支付接口必填参数trade_type!");
                 }
             }
         }
     }
     //关联参数
     if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
     }
     if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
     }
     //异步通知url未设置,则使用配置文件中的url
     if (!$inputObj->IsNotify_urlSet()) {
         $inputObj->SetNotify_url(WxPayConfig::NOTIFY_URL);
         //异步通知url
     }
     $inputObj->SetAppid(WxPayConfig::APPID);
     //公众账号ID
     $inputObj->SetMch_id(WxPayConfig::MCHID);
     //商户号
     $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);
     //终端ip
     //$inputObj->SetSpbill_create_ip("1.1.1.1");
     $inputObj->SetNonce_str(self::getNonceStr());
     //随机字符串
     //签名
     $inputObj->SetSign();
     $xml = $inputObj->ToXml();
     $startTimeStamp = self::getMillisecond();
     //请求开始时间
     $response = self::postXmlCurl($xml, $url, false, $timeOut);
     $result = WxPayResults::Init($response);
     // 统一下单接口返回正常的prepay_id,再按签名规范重新生成签名后,将数据传输给APP。
     // 参与签名的字段名为appId,partnerId,prepayId,nonceStr,timeStamp,package。注意:package的值格式为Sign=WXPay
     $time_stamp = time();
     $pack = 'Sign=WXPay';
     //输出参数列表
     $prePayParams = array();
     $prePayParams['appid'] = $result['appid'];
     $prePayParams['partnerid'] = $result['mch_id'];
     $prePayParams['prepayid'] = $result['prepay_id'];
     $prePayParams['noncestr'] = $result['nonce_str'];
     $prePayParams['package'] = $pack;
     $prePayParams['timestamp'] = $time_stamp;
     //echo json_encode($prePayParams);
     $result = WxPayResults::InitFromArray($prePayParams, true)->GetValues();
     self::reportCostTime($url, $startTimeStamp, $result);
     //上报请求花费时间
     return $result;
 }
 /**
  *函数balance_deposit 用于资金存放.
  */
 public function balance_deposit2()
 {
     $this->pageTitle = '支付 - ' . $this->configs['shop_title'];
     if (!empty($_GET['code']) && !empty($_GET['other_data'])) {
         $other_data_str = $_GET['other_data'];
         $other_data_arr = explode("_", $other_data_str);
         $_POST['amount_num'] = isset($other_data_arr[0]) ? $other_data_arr[0] : 0;
         $_POST['payment_id'] = isset($other_data_arr[1]) ? $other_data_arr[1] : 0;
         $_POST['invoice'] = isset($other_data_arr[2]) ? $other_data_arr[2] : 0;
         $_POST['item_name'] = isset($other_data_arr[3]) ? $other_data_arr[3] : 0;
     }
     if (!isset($_POST['amount_num']) || !($_POST['amount_num'] > 0) || !isset($_POST['payment_id']) || !($_POST['payment_id'] > 0)) {
         die('参数错误');
     }
     $orderid = $_POST['invoice'];
     $order_code = $_POST['item_name'];
     if (constant('Product') == 'AllInOne') {
         $orfo = $this->Order->find('first', array('conditions' => array('Order.id' => $orderid)));
         if (!empty($orfo)) {
             $orfo['Order']['sub_pay'] = $_POST['payment_id'];
             $this->Order->save($orfo['Order']);
         }
     }
     $modified = date('Y-m-d H:i:s');
     $user_id = $_SESSION['User']['User']['id'];
     $user_info = $this->User->findbyid($user_id);
     $user_money = $user_info['User']['balance'] + $_POST['amount_num'];
     $amount_money = $_POST['amount_num'];
     $payment_id = $_POST['payment_id'];
     $payment = $this->Payment->find('first', array('conditions' => array('Payment.id' => $payment_id)));
     $this->Cookie->write('pay_type', $payment_id);
     $account_info = array('user_id' => $user_id, 'amount' => $amount_money, 'payment' => $payment_id, 'status' => 0);
     $this->UserAccount->save($account_info);
     $account_id = $this->UserAccount->id;
     $order_id = isset($_SESSION['order']['ever_id']) ? $_SESSION['order']['ever_id'] : $account_id;
     $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
     try {
         $payment_config = unserialize($payment['Payment']['config']);
         if ($payment['Payment']['code'] == "weixinpay") {
             $this->layout = 'default';
             $amt = $amount_money * 100;
             $wechatpay_type = false;
             if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
                 App::import('Vendor', 'Weixinpay', array('file' => 'WxPayPubHelper.php'));
                 $jsApi = new JsApi_pub($payment_config['APPID'], $payment_config['MCHID'], $payment_config['KEY'], $payment_config['APPSECRET']);
                 if (empty($_GET['code'])) {
                     $request_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                     $other_data = $amount_money . "_" . $payment_id . "_" . $orderid . "_" . $order_code;
                     $request_url .= "?other_data=" . $other_data;
                     //触发微信返回code码
                     $wechat_pay_url = $jsApi->createOauthUrlForCode($request_url);
                     Header("Location: {$wechat_pay_url}");
                 } else {
                     //获取code码,以获取openid
                     $code = $_GET['code'];
                     $jsApi->setCode($code);
                     $openid = $jsApi->getOpenId();
                 }
                 if (!empty($openid)) {
                     $unifiedOrder = new UnifiedOrder_pub($payment_config['APPID'], $payment_config['MCHID'], $payment_config['KEY'], $payment_config['APPSECRET']);
                     $unifiedOrder->setParameter("openid", $openid);
                     //商品描述
                     $unifiedOrder->setParameter("body", $order_code);
                     //商品描述
                     //自定义订单号,此处仅作举例
                     $timeStamp = time();
                     $out_trade_no = $order_code;
                     $unifiedOrder->setParameter("out_trade_no", "{$out_trade_no}");
                     //商户订单号
                     $unifiedOrder->setParameter("total_fee", $amt);
                     //总金额
                     $unifiedOrder->setParameter("notify_url", 'http://' . $host . $this->webroot . 'responds/weixin_notify');
                     //通知地址
                     $unifiedOrder->setParameter("trade_type", "JSAPI");
                     //交易类型
                     $prepay_id = $unifiedOrder->getPrepayId();
                     $jsApi->setPrepayId($prepay_id);
                     $jsApiParameters = $jsApi->getParameters();
                     if (!empty($jsApiParameters)) {
                         $json_result = json_decode($jsApiParameters);
                         $code_url = isset($json_result->paySign) ? $jsApiParameters : '';
                         $this->set('url2', $code_url);
                     }
                 } else {
                     throw new SDKRuntimeException("支付失败,OpenId 获取失败");
                 }
             } else {
                 $this->layout = "ajax";
                 $wechatpay_type = true;
                 App::import('Vendor', 'Weixinpay', array('file' => 'WxPay.Api.php'));
                 App::import('Vendor', 'Phpqcode', array('file' => 'phpqrcode.php'));
                 $input = new WxPayUnifiedOrder();
                 $notify = new NativePay();
                 $input->SetKey($payment_config['KEY']);
                 $input->SetBody($order_code);
                 $input->SetAttach($order_code);
                 $input->SetOut_trade_no($order_code);
                 $input->SetAppid($payment_config['APPID']);
                 $input->SetMch_id($payment_config['MCHID']);
                 $input->SetTotal_fee($amt);
                 $input->SetTime_start(date("YmdHis"));
                 $input->SetTime_expire(date("YmdHis", time() + 600));
                 $input->SetGoods_tag($order_code);
                 $input->SetNotify_url('http://' . $host . $this->webroot . 'responds/weixin_notify');
                 $input->SetTrade_type("NATIVE");
                 $input->SetProduct_id($order_code);
                 $result = $notify->GetPayUrl($input);
                 $url2 = isset($result["code_url"]) ? $result["code_url"] : '';
                 $this->set('url2', $url2);
             }
             $this->set('order_code', $order_code);
             $this->set('wechatpay_type', $wechatpay_type);
         } else {
             $pay_form_txt = "";
             $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
             if ($this->RequestHandler->isMobile() && $payment['Payment']['code'] == 'alipay') {
                 //手机支付宝支付访问
                 $payment_api_log = array('payment_code' => $payment['Payment']['code'], 'type' => 1, 'type_id' => $orderid, 'order_id' => $order_code, 'order_currency' => 'CHY', 'amount' => $amount_money);
                 $this->PaymentApiLog->save($payment_api_log);
                 $alipay_config = array();
                 //合作身份者id,以2088开头的16位纯数字
                 $alipay_config['partner'] = isset($payment_config['partner']) ? $payment_config['partner'] : '';
                 //收款支付宝账号,一般情况下收款账号就是签约账号
                 $alipay_config['seller_id'] = isset($payment_config['partner']) ? $payment_config['partner'] : '';
                 //商户的私钥(后缀是.pen)文件相对路径
                 $alipay_config['private_key_path'] = ROOT . '/vendors/payments/alipaywap/key/rsa_private_key.pem';
                 //支付宝公钥(后缀是.pen)文件相对路径
                 $alipay_config['ali_public_key_path'] = ROOT . '/vendors/payments/alipaywap/key/rsa_public_key.pem';
                 //签名方式 不需修改
                 $alipay_config['sign_type'] = strtoupper('RSA');
                 //字符编码格式 目前支持 gbk 或 utf-8
                 $alipay_config['input_charset'] = strtolower('utf-8');
                 //ca证书路径地址,用于curl中ssl校验
                 //请保证cacert.pem文件在当前文件夹目录中
                 $alipay_config['cacert'] = ROOT . '/vendors/payments/alipaywap/cacert.pem';
                 //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
                 $alipay_config['transport'] = 'http';
                 if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && $payment['Payment']['code'] == 'alipay') {
                     $alipay_config['is_wechat'] = '1';
                 } else {
                     $alipay_config['is_wechat'] = '0';
                 }
                 $out_trade_no = $this->PaymentApiLog->id . "_" . $order_code;
                 $alipay_parameter = array("service" => "alipay.wap.create.direct.pay.by.user", "partner" => trim($alipay_config['partner']), "seller_id" => trim($alipay_config['seller_id']), "payment_type" => '1', "notify_url" => 'http://' . $host . $this->webroot . 'responds/return_code/' . $payment['Payment']['code'] . '/1/wap', "return_url" => 'http://' . $host . $this->webroot . 'responds/return_code/' . $payment['Payment']['code'] . '/0/wap', "out_trade_no" => $out_trade_no, "subject" => '[' . $payment_api_log['order_id'] . ']' . ' - ' . $orfo['OrderProduct'][0]['product_name'], "total_fee" => $amount_money, "show_url" => '', "body" => '', "it_b_pay" => '', "extern_token" => '', "_input_charset" => trim(strtolower($alipay_config['input_charset'])));
                 $alipaySubmit_classfile = ROOT . "/vendors/payments/alipaywap/alipay_submit.class.php";
                 include_once $alipaySubmit_classfile;
                 $alipaySubmit = new AlipaySubmit($alipay_config);
                 $html_text = $alipaySubmit->buildRequestForm($alipay_parameter, "get", "支付");
                 $pay_form_txt = $html_text;
             } else {
                 App::import('Vendor', 'payments/' . $payment['Payment']['code']);
                 $balance_payment = new $payment['Payment']['code']();
                 if ($payment['Payment']['is_online'] == 1) {
                     //在线支付增加api日志
                     $payment_api_log = array('payment_code' => $payment['Payment']['code'], 'type' => 1, 'type_id' => $orderid, 'order_id' => $order_code, 'order_currency' => 'CHY', 'amount' => $amount_money);
                     $this->PaymentApiLog->save($payment_api_log);
                     $payment_api_log['id'] = $this->PaymentApiLog->id;
                     $payment_config['cancel_return'] = 'http://' . $host . $this->webroot;
                     $payment_config['return_url'] = 'http://' . $host . $this->webroot . 'responds/return_code/' . $payment['Payment']['code'] . '/0/pc';
                     $payment_config['notify_url'] = 'http://' . $host . $this->webroot . 'responds/return_code/' . $payment['Payment']['code'] . '/1/pc';
                     $payment_config['payerName'] = '[' . $payment_api_log['order_id'] . ']' . ' - ' . $orfo['OrderProduct'][0]['product_name'];
                     $payment_api_log['created'] = date('Y-m-d H:i:s');
                     $payment_api_log['subject'] = '[' . $payment_api_log['order_id'] . ']' . ' - ' . $orfo['OrderProduct'][0]['product_name'];
                     $payment_config['payerName'] = '[' . $payment_api_log['order_id'] . ']' . ' - ' . $orfo['OrderProduct'][0]['product_name'];
                     if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && $payment['Payment']['code'] == 'alipay') {
                         $payment_config['is_wechat'] = '1';
                     } else {
                         $payment_config['is_wechat'] = '0';
                     }
                     $api_code = $balance_payment->go($payment_api_log, $payment_config);
                     $pay_form_txt = $api_code;
                 } else {
                     $this->layout = 'usercenter';
                     $this->set('msg', $payment['PaymentI18n']['description']);
                 }
             }
             echo "<style type='text/css'>body{display:none;}</style>";
             if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && $payment['Payment']['code'] == 'alipay') {
                 $this->set('pay_form_txt', $pay_form_txt);
             } else {
                 echo $pay_form_txt;
                 exit;
             }
         }
     } catch (Exception $e) {
         echo 'Caught exception: ' . $e->getMessage() . "\n";
     }
 }