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(); } }
public function createOrder($openId = null) { $cart = new \Org\Util\Cart('openId'); $info = $cart->get_cart_info(); if ($info['total_price']) { $pay = new \Org\Wechat\WxPay(); $timeStamp = time(); $out_trade_no = C('WECHAT.appid') . substr($openId, -4) . "{$timeStamp}"; $data['openId'] = $openId; $data['trade'] = $out_trade_no; $data['locationId'] = 1; $data['name'] = 'name'; $data['phone'] = 'phone'; $data['cdate'] = date('Y-m-d'); $data['ctime'] = time(); $id = M('Order')->data($data)->add(); $week = date('w'); $min = array(1, 2, 3, 4, 5, 1, 1); $jiange = array(0, 0, 0, 0, 0, 5, 6); $items = array(); foreach ($info['products_list'] as $p) { if ($p['week'] < $min[$week]) { continue; } $item['orderId'] = $id; $item['productId'] = $p['id']; $item['quantity'] = $p['count']; $item['paymentDate'] = date('Y-m-d', strtotime("+" . ($p['week'] - $week + $jiange[$week]) . " day")); $item['status'] = 0; array_push($items, $item); } M('OrderItem')->addAll($items); // 填充已知数据 $pay->setParameter("openid", "{$openId}"); $pay->setParameter("body", "办公室水果预约"); $pay->setParameter("attach", $id); $pay->setParameter("out_trade_no", "{$out_trade_no}"); $pay->setParameter("notify_url", "http://{$_SERVER['HTTP_HOST']}/wechat/cart/notify"); $pay->setParameter("trade_type", "JSAPI"); $pay->setParameter("total_fee", floor($info['total_price'] * 100)); //$pay->setParameter("total_fee",1); $pay->getPrepayId(); $result['jsApiParameters'] = json_decode($pay->getParameters()); $result['test'] = $pay->getParameters(); $result['openId'] = $openId; $result['success'] = true; $cart->empty_cart(); $this->ajaxReturn($result); } else { $result['success'] = false; $result['message'] = 'session null!'; $this->ajaxReturn($result); } }