public function order($code = null) { $pay = new \Org\Wechat\WxPay(); if ($code == null) { $url = $pay->createOauthUrlForCode("http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"); Header("Location: {$url}"); } else { // 换取openId $openId = $pay->getOpenId($code); // 填充已知数据 $pay->setParameter("openid", "{$openId}"); $pay->setParameter("body", "办公室水果预约"); $timeStamp = time(); $out_trade_no = C('WECHAT.appid') . substr($openId, -4) . "{$timeStamp}"; $pay->setParameter("out_trade_no", "{$out_trade_no}"); $pay->setParameter("notify_url", "http://{$_SERVER['HTTP_HOST']}/wechat/wxpay/notify"); $pay->setParameter("trade_type", "JSAPI"); // 计算价格 $moneys = array(5, 4, 3, 2, 1, 5, 5); /* $money_n = $moneys[date('w')]; */ $week = date('w'); $this->assign('week', $week); $money_n = $moneys[$week]; $money = $money_n * 4.9 * 100; $this->assign('money', $money / 100); // 查找代金券 $where['openId'] = $openId; $where['valid'] = 1; $user_voucher = M('UserVoucher')->where($where)->order('ctime asc')->find(); if ($user_voucher != null) { $voucher = M('Voucher')->find($user_voucher['voucherId']); $pay->setParameter("attach", $user_voucher['id']); $money -= $voucher['money']; if ($money <= 0) { $money = 1; } $this->assign('info', '(代金券立减4.89元)'); } else { $this->assign('info', ''); } $pay->setParameter("total_fee", floor($money)); // 获得支付ID $pay->getPrepayId(); // 生成package $jsApiParameters = $pay->getParameters(); $this->assign('jsApiParameters', $jsApiParameters); // 查找用户手机号,名字,地点 $this->assign("locationId", 1); $map['openId'] = $openId; $map['payment'] = array('neq', 0); $order = M('UserOrder')->where($map)->order('ctime desc')->find(); if ($order != null) { $this->assign("name", $order['name']); $this->assign("phone", $order['phone']); $this->assign("locationId", $order['locationId']); } else { $user = D('User')->getUser($openId); if ($user != null) { $map['qrScene'] = $user['qrScene']; $location = M('Location')->where($map)->find(); if ($location != null) { $this->assign("locationId", $location['id']); } } $this->assign('info', '(首次下单立减4.89元)'); } $this->assign('openId', $openId); $this->assign('trade', $out_trade_no); // 填充地点信息 $locations = M('Location')->where('valid=1')->select(); $this->assign('locations', $locations); // 填充本周或者下周 if ($money_n == 5) { $this->assign('current_or_next', '下周'); } else { $this->assign('current_or_next', '本周'); } $this->display(); } }