123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?php namespace Porder\Lib;
- use Dever;
- use Place;
- class Pay
- {
- protected $type = '';
- protected $order = [];
- protected $detail = [];
- protected $user = [];
- protected $coupon = [];
- protected $gift = [];
- protected $set = [];
- protected $place;
- public function __construct(Place $place)
- {
- $this->place = $place;
- if (!$this->place->uid) {
- Dever::error('请先登录');
- }
- }
- # 获取支付需要的信息
- public function get($detail, $set, $user = true)
- {
- if (empty($set['score'])) {
- Dever::error('积分规则不能为空');
- }
- $this->set = $set;
- $this->set['rebate'] = ['rule' => []];
- $this->order['name'] = '';
- $this->order['cash'] = $this->order['num'] = 0;
- $this->order['sales_type'] = $this->order['sales_id'] = 0;
- if ($this->place->info['sales_type'] && $this->place->info['sales_id']) {
- $this->order['sales_type'] = $this->place->info['sales_type'];
- $this->order['sales_id'] = $this->place->info['sales_id'];
- }
- foreach ($detail as $k => $v) {
- $this->getDetail($v);
- }
- $user && $this->getUser();
- $this->getOrder();
- return ['order' => $this->order, 'detail' => $this->detail, 'user' => $this->user, 'rebate' => $this->set['rebate']];
- }
- # 获取用户信息
- private function getUser()
- {
- $address_id = Dever::input('address_id');
- $coupon_id = Dever::input('coupon_id');
- $coupon_cash = Dever::input('coupon_cash');
- $gift_id = Dever::input('gift_id');
- $gift_cash = Dever::input('gift_cash');
- # 获取当前余额
- $this->user['wallet'] = Dever::load(\Pscore\Lib\User::class)->getInfo($this->place->uid, $this->set['score']);
- # 获取当前可用的优惠券
- $this->user['coupon'] = Dever::load(\Puser\Lib\Coupon::class)->getUseList($this->place->uid, $this->detail, $this->set['score'], $coupon_id, $coupon_cash);
- # 获取当前可用的礼品卡
- $this->user['gift'] = Dever::load(\Puser\Lib\Gift::class)->getUseList($this->place->uid, $this->detail, $this->set['score'], $gift_id, $gift_cash);
- # 获取地址
- if (isset($this->order['deliver_type']) && $this->order['deliver_type'] == 1 && $this->order['is_address'] == 1) {
- if ($address_id) {
- $this->user['address'] = Dever::load(\Puser\Lib\Address::class)->init($this->place->uid)->getInfo($address_id);
- } else {
- $this->user['address'] = Dever::load(\Puser\Lib\Address::class)->init($this->place->uid)->getDefault();
- }
- }
- }
- # 获取订单信息
- private function getOrder()
- {
- $this->order['uid'] = $this->place->uid;
- $this->order['cash'] = Dever::number($this->order['cash']);
- # 支付金额
- $this->order['pay_cash'] = $this->order['cash'];
- # 使用的钱包金额、优惠券金额、礼品卡
- $this->order['user_coupon_id'] = $this->order['wallet_cash'] = $this->order['coupon_cash'] = $this->order['coupon_value'] = $this->order['user_gift_id'] = $this->order['gift_cash'] = 0;
- # 使用的优惠券名称和礼品卡名称
- $this->order['coupon_name'] = $this->order['gift_name'] = '';
- # 优惠券
- $this->getCoupon();
- # 礼品卡
- $this->getGift();
- # 钱包
- $this->getWallet();
- # 获取金额
- $this->getCash();
- # 获取金额文本
- list(
- $this->order['cash_text'],
- $this->order['pay_cash_text'],
- $this->order['wallet_cash_text'],
- $this->order['coupon_cash_text'],
- $this->order['gift_cash_text'],
- ) = Dever::load(\Pscore\Lib\Info::class)->getText(
- [
- $this->order['cash'],
- $this->order['pay_cash'],
- $this->order['wallet_cash'],
- $this->order['coupon_cash'],
- $this->order['gift_cash'],
- ], $this->set['score']);
- if (isset($this->user['address']) && $this->user['address']) {
- $this->order['address_id'] = $this->user['address']['id'];
- }
- $this->order['score_id'] = $this->set['score']['id'];
- $this->order['score_exp'] = $this->set['score']['exp'];
- $this->order['score_tip'] = Dever::load(\Pscore\Lib\Info::class)->getExpTip($this->set['score']);
- }
- # 优惠券
- private function getCoupon()
- {
- if (!$this->user) {
- return;
- }
- $this->order['pay_cash_coupon'] = 0;
- $this->coupon = [];
- $coupon_id = Dever::input('coupon_id');
- if ($this->user['coupon'] && $coupon_id != -1) {
- $this->coupon = $this->user['coupon'][0];
- if ($this->order['pay_cash'] > 0 && $this->coupon['cash'] > 0) {
- $this->order['user_coupon_id'] = $this->coupon['id'];
- $this->order['coupon_id'] = $this->coupon['coupon_id'];
- $this->order['coupon_value'] = $this->coupon['pay_num'];
- $this->order['coupon_cash'] = Dever::number($this->coupon['cash']);
- # 记录优惠券付款之前的支付金额
- $this->order['pay_cash_coupon'] = $this->order['pay_cash'];
- $this->order['pay_cash'] = Dever::math('sub', $this->order['pay_cash'], $this->order['coupon_cash']);
- $this->order['coupon_name'] = $this->coupon['name'];
- }
- }
- }
- # 礼品卡
- private function getGift()
- {
- if (!$this->user) {
- return;
- }
- $this->order['pay_cash_gift'] = 0;
- $this->gift = [];
- $gift_id = Dever::input('gift_id');
- if ($this->user['gift'] && $gift_id > 0) {
- $this->gift = $this->user['gift'][0];
- if ($this->order['pay_cash'] > 0 && $this->gift['cash'] > 0) {
- if ($this->gift['cash'] > $this->order['pay_cash']) {
- $this->gift['cash'] = $this->order['pay_cash'];
- }
- $this->order['user_gift_id'] = $this->gift['id'];
- $this->order['gift_id'] = $this->gift['gift_id'];
- $this->order['gift_cash'] = Dever::number($this->gift['cash']);
- # 记录礼品卡付款之前的支付金额
- $this->order['pay_cash_gift'] = $this->order['pay_cash'];
- $this->order['pay_cash'] = Dever::math('sub', $this->order['pay_cash'], $this->order['gift_cash']);
- $this->order['gift_name'] = $this->gift['name'];
- }
- }
- }
- # 钱包
- private function getWallet()
- {
- if (!$this->user) {
- return;
- }
- $this->order['pay_cash_wallet'] = 0;
- $wallet = Dever::input('wallet');
- if ($wallet > 0 && $this->user['wallet']['yue'] >= $wallet) {
- if ($wallet > $this->order['pay_cash']) {
- $wallet = $this->order['pay_cash'];
- }
- $this->order['wallet_cash'] = Dever::number($wallet);
- # 记录钱包付款之前的支付金额
- $this->order['pay_cash_wallet'] = $this->order['pay_cash'];
- $this->order['pay_cash'] = Dever::math('sub', $this->order['pay_cash'], $this->order['wallet_cash']);
- }
- }
- # 返利
- public function getCash()
- {
- foreach ($this->detail as &$v) {
- # 计算每个明细优惠券分摊金额
- # 分摊优惠 = 优惠券金额 × (小计金额 / 订单总金额)
- # 先除以后乘以
- if ($this->order['pay_cash_coupon'] > 0) {
- $v['coupon_cash'] = Dever::math('divMul', $v['pay_cash'], $this->order['pay_cash_coupon'], $this->order['coupon_cash']);
- $v['pay_cash'] -= $v['coupon_cash'];
- }
- # 计算每个明细礼品卡分摊金额
- # 分摊优惠 = 礼品卡金额 × (小计金额 / 订单总金额)
- # 先除以后乘以
- if ($this->order['pay_cash_gift'] > 0) {
- $v['gift_cash'] = Dever::math('divMul', $v['pay_cash'], $this->order['pay_cash_gift'], $this->order['gift_cash']);
- $v['pay_cash'] -= $v['gift_cash'];
- }
- # 计算每个明细钱包分摊金额
- # 分摊优惠 = 钱包金额 × (小计金额 / 订单总金额)
- if ($this->order['pay_cash_wallet'] > 0) {
- $v['wallet_cash'] = Dever::math('divMul', $v['pay_cash'], $this->order['pay_cash_wallet'], $this->order['wallet_cash']);
- $v['pay_cash'] -= $v['wallet_cash'];
- }
- if ($v['pay_cash'] > 0) {
- $v['pay_money_cash'] = Dever::math('mul', $v['pay_cash'], $this->set['score']['exp']);
- $this->set['rebate'] = Dever::load(\Pbenefit\Lib\Item::class)->load('rebate')->get($this->order['uid'], $v['scope'], $v['pay_money_cash'], $this->set['rebate']['rule']);
- }
- }
- $this->order['pay_cash'] = Dever::number($this->order['pay_cash']);
- $this->order['pay_money_cash'] = Dever::math('mul', $this->order['pay_cash'], $this->set['score']['exp']);
- $this->order['money_cash'] = Dever::math('mul', $this->order['cash'], $this->set['score']['exp']);
- }
- # 下单
- public function act_commit(){}
- public function act($detail = false)
- {
- if ($detail) {
- $this->get($detail);
- }
- if (!$this->order) {
- Dever::error('订单不存在');
- }
- if ($this->order['num'] <= 0) {
- Dever::error('下单失败,数量为0');
- }
- $table = 'porder/' . $this->type;
- $prefix = strtoupper(substr($this->type, 0, 2));
- $this->order['uid'] = $this->place->uid;
- # 获取上级
- if ($this->place->user['parent_uid'] && $this->place->user['parent_uid'] > 0) {
- $this->order['parent_uid'] = $this->place->user['parent_uid'];
- }
- if (is_array($this->order['name'])) {
- $this->order['name'] = implode('、', $this->order['name']);
- }
- $this->order['order_num'] = Dever::load(\Api\Lib\Util::class)->createNumber($prefix, $table);
- $this->order['notify'] = 'Porder/Lib/Notify.handle|' . $this->place->sector['id'] . '|' . $this->place->info['id'] . '|' . $this->type . '|' . $this->order['order_num'];
- $this->order['buy_info'] = Dever::input('remark');
- $this->check();
- $result = [];
- if ($this->order['pay_money_cash'] > 0) {
- $result['pay'] = Dever::load(\Place\Lib\Account::class)->pay($this->order);
- if (isset($result['pay']['link']) && $result['pay']['link']) {
- return $result;
- }
- //$result['pay'] = true;
- } else {
- $result['pay'] = true;
- }
- if ($result['pay']) {
- $this->order['env_platform'] = Dever::input('platform', 'is_string', '账户', 'weixin');
- $this->order['env_type'] = Dever::input('env', 'is_numeric', '运行环境', 3);
- $result['order_id'] = Dever::db($table)->insert($this->order);
- if ($result['order_id']) {
- $result['order_num'] = $this->order['order_num'];
- $this->success($result['order_id']);
- if ($result['pay'] === true) {
- Dever::load(\Porder\Lib\Notify::class)->notify_end($this->place->sector['id'], $this->place->info['id'], $this->type, $result['order_num'], 1);
- } elseif ($this->type == 'source') {
- # 记录下来
- Dever::db('porder/soruce_pay')->insert(['order_id' => $result['order_id'], 'pay' => Dever::json_encode($result['pay'])]);
- }
- }
- }
- return $result;
- }
- public function success($order_id)
- {
- Dever::load('Porder\\Lib\\' . ucfirst($this->type) . '\\Log')->up(1, $this->order['uid'], $order_id, '下单成功');
- # 记录购买订单数
- Dever::db('puser/info')->update($this->order['uid'], ['num_order' => ['+', 1]]);
- $table = $this->type . '_detail';
- foreach ($this->detail as $v) {
- $promotion = $v['promotion'];
- unset($v['promotion']);
- /*
- $rebate = $v['rebate'];
- unset($v['rebate']);
- */
- # 记录明细
- $v['order_id'] = $order_id;
- if ($promotion) {
- $v['promotion_id'] = $promotion['id'];
- }
- $id = Dever::db('porder/' . $table)->insert($v);
- # 记录销量
- if (isset($v['source_id'])) {
- Dever::db('psource/info')->update($v['source_id'], ['num_sell' => ['+', 1]]);
- }
- if ($id) {
- # 记录促销
- if ($promotion) {
- $promotion['table'] = $table;
- $promotion['table_id'] = $id;
- $promotion['promotion_id'] = $promotion['id'];
- unset($promotion['id']);
- Dever::db('porder/promotion')->insert($promotion);
- }
- # 记录返利 以后增加明细返利记录
- /*
- if ($rebate && $rebate['rule']) {
- foreach ($rebate['rule'] as $v1) {
- $v1['table'] = $table;
- $v1['table_id'] = $id;
- $v1['rebate_rule_id'] = $v1['id'];
- unset($v1['id']);
- Dever::db('porder/rebate')->insert($v1);
- }
- }*/
- }
- }
- # 记录订单返利
- if ($this->set['rebate'] && $this->set['rebate']['rule']) {
- foreach ($this->set['rebate']['rule'] as $v1) {
- $v1['table'] = $this->type;
- $v1['table_id'] = $order_id;
- $v1['rebate_rule_id'] = $v1['id'];
- unset($v1['id']);
- Dever::db('porder/rebate')->insert($v1);
- }
- }
- # 设置活跃
- Dever::db('place/code')->update(['code' => $this->place->code], ['status' => 1]);
- }
- }
|