/** * 兑换优惠券 * @param $couponId * @param $openId * @return bool */ public function exchangeCoupon($couponId, $openId) { $customerModel = new CustomerModel(); $customer = $customerModel->readOne($openId); // 获得积分 if (!$customer) { return false; } $coupon = $this->readOne($couponId); if (!$couponId) { return false; } if ($coupon['remain_number'] <= 0) { return false; } // 用户的积分 >= 所需要的积分 if ($customer['credits'] >= $coupon['exchange_credits']) { $customerModel->subCredits($openId, $coupon['exchange_credits']); $this->subCouponNumber($couponId); } else { return false; } }
/** * 微信支付后的异步回调 */ public function notice() { $weixin = new WeixinPayUtil(); //通知微信 $notice = $weixin->notifyData(); // 签名成功, 返回数组, 否则返回xml数据 if (!is_array($notice) || !isset($notice['transaction_id'])) { exit($notice); } //签名成功,处理数据 /** * 返回的数据 * 'appid' => string 'wxf5b5e87a6a0fde94' (length=18) * 'bank_type' => string 'CFT' (length=3) * 'fee_type' => string 'CNY' (length=3) * 'is_subscribe' => string 'N' (length=1) * 'mch_id' => string '10000097' (length=8) * 'nonce_str' => string 'dz8nirk7gmxhhxn38zgib28yx14ul2gf' (length=32) * 'openid' => string 'ozoKAt-MmA74zs7MBafCix6Dg8o0' (length=28) * 'out_trade_no' => string 'wxf5b5e87a6a0fde941409708791' (length=28) * 'result_code' => string 'SUCCESS' (length=7) * 'return_code' => string 'SUCCESS' (length=7) * 'sign' => string 'EDACA525F6C675337B2DAC25B7145028' (length=32) * 'sub_mch_id' => string '10000097' (length=8) * 'time_end' => string '20140903094659' (length=14) * 'total_fee' => string '1' (length=1) * 'trade_type' => string 'NATIVE' (length=6) * 'transaction_id' => string '1004400737201409030005091526' (length=28) //微信支付单号 */ // $notice = array( // 'out_trade_no' => '201512271710391206225994', // 'transaction_id' => '1004400737201409030005091526' // ); $orderNo = $notice['out_trade_no']; $wxOrderNo = $notice['transaction_id']; $openId = $notice['openid']; $orderModel = new OrderModel(); // 获得订单 $orders = $orderModel->orders(array('order_no' => $orderNo)); if (!$orders || !$orders[0]) { exit($weixin->notifyFailure()); } // 判断是否已经支付 $order = $orders[0]; if ($order['order_sign'] == OrderModel::ORDER_PAYED) { exit($weixin->notifyPayed()); } // 更新订单信息 $this->db->trans_start(); $orderModel->payed($orderNo, $wxOrderNo); // 更新积分 $customerModel = new CustomerModel(); $score = $order['total_fee']; $customer = $customerModel->readOne($openId); if ($customer) { $customerModel->addCredits($openId, $score); } // 事物完成 $this->db->trans_complete(); if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); exit($weixin->notifyFailure()); } else { $this->db->trans_commit(); // 获得access token $weixinUtil = new WeixinUtil(); $token = $weixinUtil->getToken(); if ($token) { //foreach ($orders as $order) { $orderNo = $order['order_no']; $appointmentDay = DateUtil::buildDateTime($order['appointment_day'], $order['appointment_start_time']); $shops = (new ShopModel())->getAllShops(); $shop = $shops[$order['shop_id']]; $beautician = (new BeauticianModel())->readOne($order['beautician_id']); $beauticianName = $beautician['name']; $project = (new CurdUtil(new OrderProjectModel()))->readOne(array('order_id' => $order['order_id'])); $projectName = $project['project_name']; // 计算总积分 $totalCredits = $customer['credits'] + $score; // 发送模板消息 // $orderNo, $appointmentDay, $shop, $beautician, $projectName $weixinUtil->sendOrderMessage($orderNo, $appointmentDay, $shop, $beauticianName, $projectName, $openId, $token, $totalCredits); //} } exit($weixin->notifySuccess()); } }
/** * 检测微信登录 */ public function loginCallback($code) { if (!$code) { return false; } $accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" . "appid={$this->appId}&secret={$this->appSecret}&code={$code}&grant_type=authorization_code"; $accessToken = RequestUtil::get($accessTokenUrl); LogUtil::weixinLog('授权登录:', $accessToken); if (!$accessToken || $accessToken['error']) { return false; } $this->saveAuthorize($accessToken); // 判断是否已经获取了微信用户信息 $customerModel = new CustomerModel(); $customer = $customerModel->readOne($this->getOpenId()); if (!$customer) { $userInfo = $this->getWeixinUserInfo($this->getToken(), $this->getOpenId()); $customerModel->insert($this->getOpenId(), 0, $userInfo['nickname'], $userInfo['headimgurl'], $userInfo['city'], $userInfo['province'], $userInfo['sex']); } else { if (!$customer['nick_name']) { $userInfo = $this->getWeixinUserInfo($this->getToken(), $this->getOpenId()); $customerModel->update($this->getOpenId(), $userInfo['nickname'], $userInfo['headimgurl'], $userInfo['city'], $userInfo['province'], $userInfo['sex']); } else { } } return true; }
public function getExchangeGoods($exchangeGoodsId) { $openId = (new WeixinUtil())->getOpenId(); if (!$openId) { ResponseUtil::failure('错误的授权!'); } // 查询是否已经领取优惠券 if ((new CustomerExchangeGoodsModel())->readOne($exchangeGoodsId, $openId)) { ResponseUtil::failure('您已经兑换了此商品!'); } $exchangeGoods = (new ExchangeGoodsModel())->readOne($exchangeGoodsId); // 剩余数量为0 if ($exchangeGoods['remain_number'] <= 0) { ResponseUtil::failure('商品已经兑换完!'); } $today = date('Y-m-d'); // 是否到领取时间 if ($today < $exchangeGoods['start_time']) { ResponseUtil::failure('商品未到兑换时间!'); } if ($today > $exchangeGoods['expire_time']) { ResponseUtil::failure('商品已过兑换时间!'); } // 判断积分 $customerModel = new CustomerModel(); $customer = $customerModel->readOne($openId); if ($exchangeGoods['exchange_credits'] > $customer['credits']) { ResponseUtil::failure('积分不足,兑换商品失败!'); } $userName = $this->input->post('contact_name', true); $phoneNumber = $this->input->post('contact_phone', true); $userName = urldecode($userName); // 检查用户 $userName = trim(strip_tags($userName)); if (empty($userName)) { ResponseUtil::failure('联系人不能为空,请检查!'); } if (!preg_match('~^1\\d{10}$~', $phoneNumber)) { ResponseUtil::failure('手机号错误,请检查!'); } $this->db->trans_start(); // 领取 $data = array('exchange_goods_id' => $exchangeGoodsId, 'open_id' => $openId, 'is_get' => 0, 'contact_name' => $userName, 'contact_phone' => $phoneNumber, 'exchange_time' => DateUtil::now()); $customerExchangeGoodsId = (new CustomerExchangeGoodsModel())->create($data); if ($customerExchangeGoodsId) { (new ExchangeGoodsModel())->subExchangeGoodsNumber($exchangeGoodsId); } // 积分 $customerModel->subCredits($openId, $exchangeGoods['exchange_credits']); $this->db->trans_complete(); if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); ResponseUtil::failure('兑换商品失败!'); } else { $this->db->trans_commit(); ResponseUtil::executeSuccess('兑换商品成功!'); } }