OrderRepository.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\user\User;
  5. use app\models\user\WechatUser;
  6. use app\admin\model\order\StoreOrder as AdminStoreOrder;
  7. use crmeb\services\AlipayService;
  8. use crmeb\services\MiniProgramService;
  9. use crmeb\services\SystemConfigService;
  10. use crmeb\services\WechatService;
  11. /**
  12. * Class OrderRepository
  13. * @package crmeb\repositories
  14. */
  15. class OrderRepository
  16. {
  17. /**
  18. * TODO 小程序JS支付
  19. * @param $orderId
  20. * @param string $field
  21. * @return array|string
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public static function jsPay($orderId, $field = 'order_id')
  27. {
  28. if (is_string($orderId))
  29. $orderInfo = StoreOrder::where($field, $orderId)->find();
  30. else
  31. $orderInfo = $orderId;
  32. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  33. if ($orderInfo['paid']) exception('支付已支付!');
  34. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  35. $openid = WechatUser::getOpenId($orderInfo['uid']);
  36. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  37. $site_name = sys_config('site_name');
  38. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  39. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  40. }
  41. /**
  42. * 微信公众号JS支付
  43. * @param $orderId
  44. * @param string $field
  45. * @return array|string
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. public static function wxPay($orderId, $field = 'order_id')
  51. {
  52. if (is_string($orderId))
  53. $orderInfo = StoreOrder::where($field, $orderId)->find();
  54. else
  55. $orderInfo = $orderId;
  56. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  57. if ($orderInfo['paid']) exception('支付已支付!');
  58. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  59. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  60. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  61. $site_name = sys_config('site_name');
  62. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  63. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  64. }
  65. /**
  66. * 微信h5支付
  67. * @param $orderId
  68. * @param string $field
  69. * @return array|string
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. */
  74. public static function h5Pay($orderId, $field = 'order_id')
  75. {
  76. if (is_string($orderId))
  77. $orderInfo = StoreOrder::where($field, $orderId)->find();
  78. else
  79. $orderInfo = $orderId;
  80. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  81. if ($orderInfo['paid']) exception('支付已支付!');
  82. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  83. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  84. $site_name = sys_config('site_name');
  85. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  86. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  87. }
  88. public static function aliPay($orderId, $field = 'order_id')
  89. {
  90. if (is_string($orderId))
  91. $orderInfo = StoreOrder::where($field, $orderId)->where('is_del', 0)->find();
  92. else
  93. $orderInfo = $orderId;
  94. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  95. if ($orderInfo['paid']) exception('支付已支付!');
  96. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  97. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  98. $site_name = sys_config('site_name');
  99. if (!$site_name || !$bodyContent) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  100. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  101. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  102. $aliPay = new AlipayService();
  103. $aliPay->setAppid($alipay['alipay_app_id']);
  104. $aliPay->setNotifyUrl($notifyUrl);
  105. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  106. $aliPay->setTotalFee($orderInfo['pay_price']);
  107. $aliPay->setOutTradeNo($orderInfo['order_id']);
  108. $aliPay->setOrderName(StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  109. $aliPay->setPassbackParams(['attach' => 'product']);
  110. $orderStr = $aliPay->getOrderStr();
  111. return $orderStr;
  112. }
  113. /**
  114. * 用户确认收货
  115. * @param $order
  116. * @param $uid
  117. * @throws \think\Exception
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. * @throws \think\exception\DbException
  121. * @throws \think\db\exception\DbException
  122. * @throws \Exception
  123. */
  124. public static function storeProductOrderUserTakeDelivery($order, $uid)
  125. {
  126. $res1 = StoreOrder::gainUserIntegral($order);
  127. // $res2 = User::backOrderBrokerage($order);
  128. $res2 = User::sendBackOrderBrokerage($order);
  129. StoreOrder::orderTakeAfter($order);
  130. //满赠优惠券
  131. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  132. if (!($res1 && $res2)) exception('收货失败!');
  133. }
  134. /**
  135. * 修改状态 为已收货 admin模块
  136. * @param $order
  137. * @throws \Exception
  138. */
  139. public static function storeProductOrderTakeDeliveryAdmin($order)
  140. {
  141. $res1 = AdminStoreOrder::gainUserIntegral($order);
  142. // $res2 = User::backOrderBrokerage($order);
  143. $res2 = User::sendBackOrderBrokerage($order);
  144. AdminStoreOrder::orderTakeAfter($order);
  145. if (!($res1 && $res2)) exception('收货失败!');
  146. }
  147. /**
  148. * 修改状态 为已收货 定时任务使用
  149. * @param $order
  150. * @throws \Exception
  151. */
  152. public static function storeProductOrderTakeDeliveryTimer($order)
  153. {
  154. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  155. $res2 = User::sendBackOrderBrokerage($order);
  156. AdminStoreOrder::orderTakeAfter($order);
  157. if (!($res1 && $res2)) exception('收货失败!');
  158. }
  159. /**
  160. * 修改状态为 已退款 admin模块
  161. * @param $data
  162. * @param $oid
  163. * @return bool|mixed
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. * @throws \think\exception\DbException
  167. */
  168. public static function storeProductOrderRefundY($data, $oid)
  169. {
  170. $order = AdminStoreOrder::where('id', $oid)->find();
  171. if ($order['is_channel'] == 1)
  172. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  173. else
  174. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  175. }
  176. /**
  177. * TODO 后台余额退款
  178. * @param $product
  179. * @param $refund_data
  180. * @throws \Exception
  181. */
  182. public static function storeOrderYueRefund($product, $refund_data)
  183. {
  184. $res = AdminStoreOrder::integralBack($product['id']);
  185. if (!$res) exception('退积分失败!');
  186. }
  187. /**
  188. * 订单退积分
  189. * @param $product $product 商品信息
  190. * @param $back_integral $back_integral 退多少积分
  191. */
  192. public static function storeOrderIntegralBack($product, $back_integral)
  193. {
  194. }
  195. }