Pay.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php namespace Porder\Lib;
  2. use Dever;
  3. use Place;
  4. class Pay
  5. {
  6. protected $type = '';
  7. protected $order = [];
  8. protected $detail = [];
  9. protected $user = [];
  10. protected $coupon = [];
  11. protected $gift = [];
  12. protected $set = [];
  13. protected $place;
  14. public function __construct(Place $place)
  15. {
  16. $this->place = $place;
  17. if (!$this->place->uid) {
  18. Dever::error('请先登录');
  19. }
  20. }
  21. # 获取支付需要的信息
  22. public function get($detail, $set, $user = true)
  23. {
  24. if (empty($set['score'])) {
  25. Dever::error('积分规则不能为空');
  26. }
  27. $this->set = $set;
  28. $this->set['rebate'] = ['rule' => []];
  29. $this->order['name'] = '';
  30. $this->order['cash'] = $this->order['num'] = 0;
  31. $this->order['sales_type'] = $this->order['sales_id'] = 0;
  32. if ($this->place->info['sales_type'] && $this->place->info['sales_id']) {
  33. $this->order['sales_type'] = $this->place->info['sales_type'];
  34. $this->order['sales_id'] = $this->place->info['sales_id'];
  35. }
  36. foreach ($detail as $k => $v) {
  37. $this->getDetail($v);
  38. }
  39. $user && $this->getUser();
  40. $this->getOrder();
  41. return ['order' => $this->order, 'detail' => $this->detail, 'user' => $this->user, 'rebate' => $this->set['rebate']];
  42. }
  43. # 获取用户信息
  44. private function getUser()
  45. {
  46. $address_id = Dever::input('address_id');
  47. $coupon_id = Dever::input('coupon_id');
  48. $coupon_cash = Dever::input('coupon_cash');
  49. $gift_id = Dever::input('gift_id');
  50. $gift_cash = Dever::input('gift_cash');
  51. # 获取当前余额
  52. $this->user['wallet'] = Dever::load(\Pscore\Lib\User::class)->getInfo($this->place->uid, $this->set['score']);
  53. # 获取当前可用的优惠券
  54. $this->user['coupon'] = Dever::load(\Puser\Lib\Coupon::class)->getUseList($this->place->uid, $this->detail, $this->set['score'], $coupon_id, $coupon_cash);
  55. # 获取当前可用的礼品卡
  56. $this->user['gift'] = Dever::load(\Puser\Lib\Gift::class)->getUseList($this->place->uid, $this->detail, $this->set['score'], $gift_id, $gift_cash);
  57. # 获取地址
  58. if (isset($this->order['deliver_type']) && $this->order['deliver_type'] == 1 && $this->order['is_address'] == 1) {
  59. if ($address_id) {
  60. $this->user['address'] = Dever::load(\Puser\Lib\Address::class)->init($this->place->uid)->getInfo($address_id);
  61. } else {
  62. $this->user['address'] = Dever::load(\Puser\Lib\Address::class)->init($this->place->uid)->getDefault();
  63. }
  64. }
  65. }
  66. # 获取订单信息
  67. private function getOrder()
  68. {
  69. $this->order['uid'] = $this->place->uid;
  70. $this->order['cash'] = Dever::number($this->order['cash']);
  71. # 支付金额
  72. $this->order['pay_cash'] = $this->order['cash'];
  73. # 使用的钱包金额、优惠券金额、礼品卡
  74. $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;
  75. # 使用的优惠券名称和礼品卡名称
  76. $this->order['coupon_name'] = $this->order['gift_name'] = '';
  77. # 优惠券
  78. $this->getCoupon();
  79. # 礼品卡
  80. $this->getGift();
  81. # 钱包
  82. $this->getWallet();
  83. # 获取金额
  84. $this->getCash();
  85. # 获取金额文本
  86. list(
  87. $this->order['cash_text'],
  88. $this->order['pay_cash_text'],
  89. $this->order['wallet_cash_text'],
  90. $this->order['coupon_cash_text'],
  91. $this->order['gift_cash_text'],
  92. ) = Dever::load(\Pscore\Lib\Info::class)->getText(
  93. [
  94. $this->order['cash'],
  95. $this->order['pay_cash'],
  96. $this->order['wallet_cash'],
  97. $this->order['coupon_cash'],
  98. $this->order['gift_cash'],
  99. ], $this->set['score']);
  100. if (isset($this->user['address']) && $this->user['address']) {
  101. $this->order['address_id'] = $this->user['address']['id'];
  102. }
  103. $this->order['score_id'] = $this->set['score']['id'];
  104. $this->order['score_exp'] = $this->set['score']['exp'];
  105. $this->order['score_tip'] = Dever::load(\Pscore\Lib\Info::class)->getExpTip($this->set['score']);
  106. }
  107. # 优惠券
  108. private function getCoupon()
  109. {
  110. if (!$this->user) {
  111. return;
  112. }
  113. $this->order['pay_cash_coupon'] = 0;
  114. $this->coupon = [];
  115. $coupon_id = Dever::input('coupon_id');
  116. if ($this->user['coupon'] && $coupon_id != -1) {
  117. $this->coupon = $this->user['coupon'][0];
  118. if ($this->order['pay_cash'] > 0 && $this->coupon['cash'] > 0) {
  119. $this->order['user_coupon_id'] = $this->coupon['id'];
  120. $this->order['coupon_id'] = $this->coupon['coupon_id'];
  121. $this->order['coupon_value'] = $this->coupon['pay_num'];
  122. $this->order['coupon_cash'] = Dever::number($this->coupon['cash']);
  123. # 记录优惠券付款之前的支付金额
  124. $this->order['pay_cash_coupon'] = $this->order['pay_cash'];
  125. $this->order['pay_cash'] = Dever::math('sub', $this->order['pay_cash'], $this->order['coupon_cash']);
  126. $this->order['coupon_name'] = $this->coupon['name'];
  127. }
  128. }
  129. }
  130. # 礼品卡
  131. private function getGift()
  132. {
  133. if (!$this->user) {
  134. return;
  135. }
  136. $this->order['pay_cash_gift'] = 0;
  137. $this->gift = [];
  138. $gift_id = Dever::input('gift_id');
  139. if ($this->user['gift'] && $gift_id > 0) {
  140. $this->gift = $this->user['gift'][0];
  141. if ($this->order['pay_cash'] > 0 && $this->gift['cash'] > 0) {
  142. if ($this->gift['cash'] > $this->order['pay_cash']) {
  143. $this->gift['cash'] = $this->order['pay_cash'];
  144. }
  145. $this->order['user_gift_id'] = $this->gift['id'];
  146. $this->order['gift_id'] = $this->gift['gift_id'];
  147. $this->order['gift_cash'] = Dever::number($this->gift['cash']);
  148. # 记录礼品卡付款之前的支付金额
  149. $this->order['pay_cash_gift'] = $this->order['pay_cash'];
  150. $this->order['pay_cash'] = Dever::math('sub', $this->order['pay_cash'], $this->order['gift_cash']);
  151. $this->order['gift_name'] = $this->gift['name'];
  152. }
  153. }
  154. }
  155. # 钱包
  156. private function getWallet()
  157. {
  158. if (!$this->user) {
  159. return;
  160. }
  161. $this->order['pay_cash_wallet'] = 0;
  162. $wallet = Dever::input('wallet');
  163. if ($wallet > 0 && $this->user['wallet']['yue'] >= $wallet) {
  164. if ($wallet > $this->order['pay_cash']) {
  165. $wallet = $this->order['pay_cash'];
  166. }
  167. $this->order['wallet_cash'] = Dever::number($wallet);
  168. # 记录钱包付款之前的支付金额
  169. $this->order['pay_cash_wallet'] = $this->order['pay_cash'];
  170. $this->order['pay_cash'] = Dever::math('sub', $this->order['pay_cash'], $this->order['wallet_cash']);
  171. }
  172. }
  173. # 返利
  174. public function getCash()
  175. {
  176. foreach ($this->detail as &$v) {
  177. # 计算每个明细优惠券分摊金额
  178. # 分摊优惠 = 优惠券金额 × (小计金额 / 订单总金额)
  179. # 先除以后乘以
  180. if ($this->order['pay_cash_coupon'] > 0) {
  181. $v['coupon_cash'] = Dever::math('divMul', $v['pay_cash'], $this->order['pay_cash_coupon'], $this->order['coupon_cash']);
  182. $v['pay_cash'] -= $v['coupon_cash'];
  183. }
  184. # 计算每个明细礼品卡分摊金额
  185. # 分摊优惠 = 礼品卡金额 × (小计金额 / 订单总金额)
  186. # 先除以后乘以
  187. if ($this->order['pay_cash_gift'] > 0) {
  188. $v['gift_cash'] = Dever::math('divMul', $v['pay_cash'], $this->order['pay_cash_gift'], $this->order['gift_cash']);
  189. $v['pay_cash'] -= $v['gift_cash'];
  190. }
  191. # 计算每个明细钱包分摊金额
  192. # 分摊优惠 = 钱包金额 × (小计金额 / 订单总金额)
  193. if ($this->order['pay_cash_wallet'] > 0) {
  194. $v['wallet_cash'] = Dever::math('divMul', $v['pay_cash'], $this->order['pay_cash_wallet'], $this->order['wallet_cash']);
  195. $v['pay_cash'] -= $v['wallet_cash'];
  196. }
  197. if ($v['pay_cash'] > 0) {
  198. $v['pay_money_cash'] = Dever::math('mul', $v['pay_cash'], $this->set['score']['exp']);
  199. $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']);
  200. }
  201. }
  202. $this->order['pay_cash'] = Dever::number($this->order['pay_cash']);
  203. $this->order['pay_money_cash'] = Dever::math('mul', $this->order['pay_cash'], $this->set['score']['exp']);
  204. $this->order['money_cash'] = Dever::math('mul', $this->order['cash'], $this->set['score']['exp']);
  205. }
  206. # 下单
  207. public function act_commit(){}
  208. public function act($detail = false)
  209. {
  210. if ($detail) {
  211. $this->get($detail);
  212. }
  213. if (!$this->order) {
  214. Dever::error('订单不存在');
  215. }
  216. if ($this->order['num'] <= 0) {
  217. Dever::error('下单失败,数量为0');
  218. }
  219. $table = 'porder/' . $this->type;
  220. $prefix = strtoupper(substr($this->type, 0, 2));
  221. $this->order['uid'] = $this->place->uid;
  222. # 获取上级
  223. if ($this->place->user['parent_uid'] && $this->place->user['parent_uid'] > 0) {
  224. $this->order['parent_uid'] = $this->place->user['parent_uid'];
  225. }
  226. if (is_array($this->order['name'])) {
  227. $this->order['name'] = implode('、', $this->order['name']);
  228. }
  229. $this->order['order_num'] = Dever::load(\Api\Lib\Util::class)->createNumber($prefix, $table);
  230. $this->order['notify'] = 'Porder/Lib/Notify.handle|' . $this->place->sector['id'] . '|' . $this->place->info['id'] . '|' . $this->type . '|' . $this->order['order_num'];
  231. $this->order['buy_info'] = Dever::input('remark');
  232. $this->check();
  233. $result = [];
  234. if ($this->order['pay_money_cash'] > 0) {
  235. $result['pay'] = Dever::load(\Place\Lib\Account::class)->pay($this->order);
  236. if (isset($result['pay']['link']) && $result['pay']['link']) {
  237. return $result;
  238. }
  239. //$result['pay'] = true;
  240. } else {
  241. $result['pay'] = true;
  242. }
  243. if ($result['pay']) {
  244. $this->order['env_platform'] = Dever::input('platform', 'is_string', '账户', 'weixin');
  245. $this->order['env_type'] = Dever::input('env', 'is_numeric', '运行环境', 3);
  246. $result['order_id'] = Dever::db($table)->insert($this->order);
  247. if ($result['order_id']) {
  248. $result['order_num'] = $this->order['order_num'];
  249. $this->success($result['order_id']);
  250. if ($result['pay'] === true) {
  251. Dever::load(\Porder\Lib\Notify::class)->notify_end($this->place->sector['id'], $this->place->info['id'], $this->type, $result['order_num'], 1);
  252. } elseif ($this->type == 'source') {
  253. # 记录下来
  254. Dever::db('porder/soruce_pay')->insert(['order_id' => $result['order_id'], 'pay' => Dever::json_encode($result['pay'])]);
  255. }
  256. }
  257. }
  258. return $result;
  259. }
  260. public function success($order_id)
  261. {
  262. Dever::load('Porder\\Lib\\' . ucfirst($this->type) . '\\Log')->up(1, $this->order['uid'], $order_id, '下单成功');
  263. # 记录购买订单数
  264. Dever::db('puser/info')->update($this->order['uid'], ['num_order' => ['+', 1]]);
  265. $table = $this->type . '_detail';
  266. foreach ($this->detail as $v) {
  267. $promotion = $v['promotion'];
  268. unset($v['promotion']);
  269. /*
  270. $rebate = $v['rebate'];
  271. unset($v['rebate']);
  272. */
  273. # 记录明细
  274. $v['order_id'] = $order_id;
  275. if ($promotion) {
  276. $v['promotion_id'] = $promotion['id'];
  277. }
  278. $id = Dever::db('porder/' . $table)->insert($v);
  279. # 记录销量
  280. if (isset($v['source_id'])) {
  281. Dever::db('psource/info')->update($v['source_id'], ['num_sell' => ['+', 1]]);
  282. }
  283. if ($id) {
  284. # 记录促销
  285. if ($promotion) {
  286. $promotion['table'] = $table;
  287. $promotion['table_id'] = $id;
  288. $promotion['promotion_id'] = $promotion['id'];
  289. unset($promotion['id']);
  290. Dever::db('porder/promotion')->insert($promotion);
  291. }
  292. # 记录返利 以后增加明细返利记录
  293. /*
  294. if ($rebate && $rebate['rule']) {
  295. foreach ($rebate['rule'] as $v1) {
  296. $v1['table'] = $table;
  297. $v1['table_id'] = $id;
  298. $v1['rebate_rule_id'] = $v1['id'];
  299. unset($v1['id']);
  300. Dever::db('porder/rebate')->insert($v1);
  301. }
  302. }*/
  303. }
  304. }
  305. # 记录订单返利
  306. if ($this->set['rebate'] && $this->set['rebate']['rule']) {
  307. foreach ($this->set['rebate']['rule'] as $v1) {
  308. $v1['table'] = $this->type;
  309. $v1['table_id'] = $order_id;
  310. $v1['rebate_rule_id'] = $v1['id'];
  311. unset($v1['id']);
  312. Dever::db('porder/rebate')->insert($v1);
  313. }
  314. }
  315. # 设置活跃
  316. Dever::db('place/code')->update(['code' => $this->place->code], ['status' => 1]);
  317. }
  318. }