OtherOrderController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v1\order;
  12. use app\services\order\OtherOrderServices;
  13. use app\services\pay\OrderPayServices;
  14. use app\services\pay\PayServices;
  15. use app\services\pay\YuePayServices;
  16. use app\services\user\member\MemberCardServices;
  17. use app\services\user\UserServices;
  18. use crmeb\services\CacheService;
  19. use crmeb\services\SystemConfigService;
  20. use app\Request;
  21. /**
  22. * Class OtherOrderController
  23. * @package app\controller\api\v1\order
  24. */
  25. class OtherOrderController
  26. {
  27. /**
  28. * @var OtherOrderServices
  29. */
  30. protected $services;
  31. /**
  32. * OtherOrderController constructor.
  33. * @param OtherOrderServices $services
  34. */
  35. public function __construct(OtherOrderServices $services)
  36. {
  37. $this->services = $services;
  38. }
  39. /**
  40. * 计算会员线下付款金额
  41. * @param Request $request
  42. * @return mixed
  43. */
  44. public function computed_offline_pay_price(Request $request)
  45. {
  46. [$pay_price] = $request->getMore([
  47. ['pay_price', 0]
  48. ], true);
  49. if (!$pay_price || !is_numeric($pay_price)) return app('json')->fail('请输入付款金额');
  50. $uid = $request->uid();
  51. /** @var UserServices $userService */
  52. $userService = app()->make(UserServices::class);
  53. $user_info = $userService->get($uid, ['is_money_level', 'is_ever_level', 'overdue_time']);
  54. //会员线下享受折扣
  55. if ($user_info->is_money_level > 0 && $user_info->overdue_time > time()) {
  56. //看是否开启线下享受折扣
  57. /** @var MemberCardServices $memberCardService */
  58. $memberCardService = app()->make(MemberCardServices::class);
  59. $offline_rule_number = $memberCardService->isOpenMemberCardCache('offline');
  60. if ($offline_rule_number) {
  61. $pay_price = bcmul($pay_price, bcdiv($offline_rule_number, '100', 2), 2);
  62. } else {
  63. $pay_price = 0;
  64. }
  65. } else {
  66. $pay_price = 0;
  67. }
  68. return app('json')->successful(['pay_price' => $pay_price]);
  69. }
  70. /**
  71. * @param Request $request
  72. * @return mixed
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function create(Request $request)
  78. {
  79. [$payType, $type, $from, $memberId, $price, $money, $quitUrl] = $request->postMore([
  80. ['pay_type', 'yue'],
  81. ['type', 0],
  82. ['from', 'weixin'],
  83. ['member_type', ''],
  84. ['price', 0.00],
  85. ['money', 0.00],
  86. ['quitUrl', ''],
  87. ], true);
  88. $payType = strtolower($payType);
  89. if (!(int)$memberId) {
  90. return app('json')->fail('缺少购买会员类型ID');
  91. }
  92. if (in_array($type, [1, 2])) {
  93. /** @var MemberCardServices $memberCardService */
  94. $memberCardService = app()->make(MemberCardServices::class);
  95. $isOpenMember = $memberCardService->isOpenMemberCardCache();
  96. if (!$isOpenMember) return app('json')->fail('付费会员功能暂未开启!');
  97. }
  98. $channelType = $request->user('user_type');
  99. $uid = (int)$request->uid();
  100. /** @var OtherOrderServices $OtherOrderServices */
  101. $OtherOrderServices = app()->make(OtherOrderServices::class);
  102. $order = $OtherOrderServices->createOrder($uid, $memberId, $price, $channelType, $payType, $type);
  103. if ($order === false) return app('json')->fail('支付数据生成失败');
  104. $order_id = $order['order_id'];
  105. $orderInfo = $order->toArray();
  106. $info = compact('order_id');
  107. if ($order_id) {
  108. switch ($payType) {
  109. case PayServices::WEIXIN_PAY:
  110. if ($orderInfo['paid']) return app('json')->fail('已支付!');
  111. //支付金额为0
  112. if (bcsub((string)$orderInfo['pay_price'], '0', 2) <= 0) {
  113. //创建订单jspay支付
  114. $payPriceStatus = $OtherOrderServices->zeroYuanPayment($orderInfo);
  115. if ($payPriceStatus)//0元支付成功
  116. return app('json')->status('success', '微信支付成功', $info);
  117. else
  118. return app('json')->status('pay_error');
  119. } else {
  120. /** @var OrderPayServices $payServices */
  121. $payServices = app()->make(OrderPayServices::class);
  122. $info['jsConfig'] = $payServices->orderPay($orderInfo, $from);
  123. if ($from == 'weixinh5') {
  124. return app('json')->status('wechat_h5_pay', '前往支付', $info);
  125. } else {
  126. return app('json')->status('wechat_pay', '前往支付', $info);
  127. }
  128. }
  129. break;
  130. case PayServices::YUE_PAY:
  131. /** @var YuePayServices $yueServices */
  132. $yueServices = app()->make(YuePayServices::class);
  133. $pay = $yueServices->yueOrderPay($orderInfo, $uid);
  134. if ($pay['status'] === true)
  135. return app('json')->status('success', '余额支付成功', $info);
  136. else {
  137. if (is_array($pay))
  138. return app('json')->status($pay['status'], $pay['msg'], $info);
  139. else
  140. return app('json')->status('pay_error', $pay);
  141. }
  142. break;
  143. case PayServices::ALIAPY_PAY:
  144. if (!$quitUrl && ($request->isH5() || $request->isWechat())) {
  145. return app('json')->status('pay_error', '请传入支付宝支付回调URL', $info);
  146. }
  147. //支付金额为0
  148. if (bcsub((string)$orderInfo['pay_price'], '0', 2) <= 0) {
  149. //创建订单jspay支付
  150. $payPriceStatus = $OtherOrderServices->zeroYuanPayment($orderInfo);
  151. if ($payPriceStatus)//0元支付成功
  152. return app('json')->status('success', '支付宝支付成功', $info);
  153. else
  154. return app('json')->status('pay_error');
  155. } else {
  156. /** @var OrderPayServices $payServices */
  157. $payServices = app()->make(OrderPayServices::class);
  158. $info['jsConfig'] = $payServices->alipayOrder($orderInfo, $quitUrl, $request->isRoutine());
  159. $payKey = md5($orderInfo['order_id']);
  160. CacheService::set($payKey, ['order_id' => $orderInfo['order_id'], 'other_pay_type' => true], 300);
  161. $info['pay_key'] = $payKey;
  162. return app('json')->status(PayServices::ALIAPY_PAY . '_pay', '前往支付', $info);
  163. }
  164. break;
  165. case PayServices::OFFLINE_PAY:
  166. return app('json')->status('success', '前往支付', $info);
  167. break;
  168. default:
  169. return app('json')->fail('未知支付方式');
  170. }
  171. } else return app('json')->fail('订单生成失败!');
  172. }
  173. /**
  174. * 线下支付方式
  175. * @return mixed
  176. */
  177. public function pay_type(Request $request)
  178. {
  179. $payType = SystemConfigService::more(['ali_pay_status', 'pay_weixin_open', 'site_name', 'balance_func_status', 'yue_pay_status']);
  180. $payType['now_money'] = $request->user('now_money');
  181. $payType['offline_pay_status'] = true;
  182. $payType['yue_pay_status'] = (int)($payType['balance_func_status'] ?? 0) && (int)($payType['yue_pay_status'] ?? 0) == 1 ? 1 : 0;//余额支付 1 开启 2 关闭
  183. unset($payType['balance_func_status'], $payType['yue_pay_status']);
  184. return app('json')->successful($payType);
  185. }
  186. }