Пример #1
0
 public function pay()
 {
     $totalprice = $_GET['totalprice'];
     //$cart_names = $_GET['cart_name'];
     //$openid = $_GET['uid'];
     $openid = $_SESSION['uid'];
     $orderid = $_GET['orderid'];
     $agent = $_SERVER['HTTP_USER_AGENT'];
     if (!strpos($agent, "Android")) {
         $this->assign("tbtspic", "ios.png");
     } else {
         $this->assign("tbtspic", "android.png");
     }
     if (!strpos($agent, "icroMessenger")) {
         $alipay = M("Alipay")->find();
         $url = 'http://' . $_SERVER['SERVER_NAME'] . __ROOT__ . '/api/wapalipay/alipayapi.php?WIDseller_email=' . $alipay['alipayname'] . '&WIDout_trade_no=' . $orderid . '&WIDsubject=' . $orderid . '&WIDtotal_fee=' . $totalprice;
         header("Location: {$url}");
         exit;
     }
     import('Wechat', APP_PATH . 'Common/Wechat', '.class.php');
     $config = M("Wxconfig")->where(array("id" => "1"))->find();
     $options = array('token' => $config["token"], 'encodingaeskey' => $config["encodingaeskey"], 'appid' => $config["appid"], 'appsecret' => $config["appsecret"], 'partnerid' => $config["partnerid"], 'partnerkey' => $config["partnerkey"], 'paysignkey' => $config["paysignkey"]);
     $weObj = new Wechat($options);
     if (strlen($openid) <= 10) {
         $info = $weObj->getOauthAccessToken();
         if (!$info) {
             $callback = 'http://' . $_SERVER['SERVER_NAME'] . U("App/Index/pay", $_GET);
             $url = $weObj->getOauthRedirect($callback, '', 'snsapi_base');
             header("Location: {$url}");
             exit;
         } else {
             $openid = $info['openid'];
         }
     }
     $order_info = M('Order')->where(array('orderid' => $orderid))->find();
     if (empty($order_info)) {
         exit('订单信息错误');
     }
     $cartdata = json_decode($order_info['cartdata'], true);
     $cart_name = $cartdata[0]['name'];
     $cart_num = $cartdata[0]['num'];
     $cart_price = $cartdata[0]['price'];
     $cart_goods_id = $cartdata[0]['id'];
     $goodsdata = M("Good")->where(array("id" => $cart_goods_id))->find();
     $userdata = M("User")->where(array("uid" => $_SESSION['uid']))->find();
     if (empty($userdata)) {
         exit('用户信息错误');
     }
     $username = $userdata['username'];
     $phone = $userdata['phone'];
     $address = $userdata['address'];
     $this->assign("goodsdata", $goodsdata);
     $this->assign("username", $username);
     $this->assign("phone", $phone);
     $this->assign("address", $address);
     $this->assign("cart_name", $cart_name);
     $this->assign("cart_num", $cart_num);
     $this->assign("cart_price", $cart_price);
     $coptions = array('token' => $config["token"], 'encodingaeskey' => $config["encodingaeskey"], 'appid' => $config["cappid"], 'appsecret' => $config["cappsecret"], 'partnerid' => $config["partnerid"], 'partnerkey' => $config["partnerkey"], 'paysignkey' => $config["paysignkey"]);
     $cweObj = new Wechat($coptions);
     $cinfo = $cweObj->getOauthAccessToken();
     if (!$cinfo) {
         $callback = 'http://' . $_SERVER['SERVER_NAME'] . U("App/Index/pay", $_GET);
         $url = $cweObj->getOauthRedirect($callback, '', 'snsapi_base');
         header("Location: {$url}");
         exit;
     }
     $appid = $coptions['appid'];
     $mch_id = $coptions['partnerid'];
     $out_trade_no = $orderid;
     $body = $cart_name;
     $total_fee = $cart_price * $cart_num * 100;
     $notify_url = 'http://' . $_SERVER['SERVER_NAME'];
     $spbill_create_ip = $_SERVER["REMOTE_ADDR"];
     $nonce_str = $cweObj->generateNonceStr();
     $copenid = $cinfo['openid'];
     $pay_xml = $cweObj->createPackageXml($appid, $mch_id, $nonce_str, $body, $out_trade_no, $total_fee, $spbill_create_ip, $notify_url, $copenid);
     $pay_xml = $cweObj->get_pay_id($pay_xml);
     if ($pay_xml['err_code'] == "ORDERPAID") {
         $this->redirect('App/Index/payover', array('out_trade_no' => $out_trade_no, 'uid' => $_SESSION['uid']));
         eixt();
     }
     $prepay_id = $pay_xml['prepay_id'];
     $jsApiObj["appId"] = $appid;
     $timeStamp = time();
     $jsApiObj["timeStamp"] = "{$timeStamp}";
     $jsApiObj["nonceStr"] = $nonce_str;
     $jsApiObj["package"] = "prepay_id={$prepay_id}";
     $jsApiObj["signType"] = "MD5";
     $jsApiObj["paySign"] = $cweObj->getPaySignature($jsApiObj);
     $url = json_encode($jsApiObj);
     $returnUrl = 'http://' . $_SERVER['SERVER_NAME'] . U('App/Index/payover', array('out_trade_no' => $out_trade_no, 'uid' => $_SESSION['uid']));
     $info = R("Api/Api/gettheme");
     C("DEFAULT_THEME", $info["theme"]);
     $this->assign("price", $cart_price * $cart_num);
     $this->assign("info", $info);
     $this->assign("url", $url);
     $this->assign("returnUrl", $returnUrl);
     $this->display();
 }
Пример #2
0
 public function wxpayapi()
 {
     $this->checkUser();
     if (!$_GET['payid']) {
         $this->error("没有找到相关支付订单");
     }
     $payment_info = M('Payment')->where(array('id' => $_GET['payid']))->find();
     $alipay_config = $this->alipay_config(2);
     $price = $payment_info['discount'] + $payment_info['paymoney'];
     import('Wechat', APP_PATH . 'Common/Wechat', '.class.php');
     $options = array('appid' => 'wx0d502aa5ef3e60b0', 'appsecret' => '08844e4bafaf557818c520598dc7a029', 'partnerid' => '1280963601', 'partnerkey' => 'tianshiweishang8tianshimama8zhou', 'paysignkey' => '08844e4bafaf557818c520598dc7a029');
     $weObj = new Wechat($options);
     $appid = $options['appid'];
     $mch_id = $options['partnerid'];
     $out_trade_no = $payment_info['payno'];
     $body = '积分充值';
     $total_fee = $price * 100;
     $notify_url = 'http://' . $_SERVER['SERVER_NAME'] . U('Member/wxNotify_url', array('out_trade_no' => $out_trade_no));
     $spbill_create_ip = $_SERVER['REMOTE_ADDR'];
     $nonce_str = $weObj->generateNonceStr();
     $pay_xml = $weObj->createNativePackageXml($appid, $mch_id, $nonce_str, $body, $out_trade_no, $total_fee, $notify_url, $spbill_create_ip);
     $pay_xml = $weObj->get_pay_id($pay_xml);
     if ($pay_xml['err_code'] == "ORDERPAID") {
         $this->error('商品已支付');
         eixt();
     }
     $this->assign('pay_id', $_GET['payid']);
     $this->assign('paymoney', $price);
     $this->assign('paytypename', $alipay_config['payname']);
     $this->assign('code_url', $pay_xml['code_url']);
     $this->seo('支付确认', '', '', 'pay');
     C('TOKEN_ON', false);
     //关闭表单令牌
     $this->display('alipayapi');
 }