OrderRepository.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /**
  89. * 用户确认收货
  90. * @param $order
  91. * @param $uid
  92. * @throws \think\Exception
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @throws \think\exception\DbException
  96. * @throws \think\db\exception\DbException
  97. * @throws \Exception
  98. */
  99. public static function storeProductOrderUserTakeDelivery($order, $uid)
  100. {
  101. $res1 = StoreOrder::gainUserIntegral($order);
  102. $res3 = StoreOrder::gainWhiteIntegral($order);
  103. $res4 = StoreOrder::gainBusinessIntegral($order);
  104. // $res2 = User::backOrderBrokerage($order);
  105. $res2 = User::sendBackOrderBrokerage($order);
  106. StoreOrder::orderTakeAfter($order);
  107. //满赠优惠券
  108. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  109. if (!($res1 && $res2 && $res3 && $res4)) exception('收货失败!');
  110. }
  111. /**
  112. * 修改状态 为已收货 admin模块
  113. * @param $order
  114. * @throws \Exception
  115. */
  116. public static function storeProductOrderTakeDeliveryAdmin($order)
  117. {
  118. $res1 = AdminStoreOrder::gainUserIntegral($order);
  119. // $res2 = User::backOrderBrokerage($order);
  120. $res2 = User::sendBackOrderBrokerage($order);
  121. AdminStoreOrder::orderTakeAfter($order);
  122. if (!($res1 && $res2)) exception('收货失败!');
  123. }
  124. /**
  125. * 修改状态 为已收货 定时任务使用
  126. * @param $order
  127. * @throws \Exception
  128. */
  129. public static function storeProductOrderTakeDeliveryTimer($order)
  130. {
  131. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  132. $res2 = User::sendBackOrderBrokerage($order);
  133. AdminStoreOrder::orderTakeAfter($order);
  134. if (!($res1 && $res2)) exception('收货失败!');
  135. }
  136. /**
  137. * 修改状态为 已退款 admin模块
  138. * @param $data
  139. * @param $oid
  140. * @return bool|mixed
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. * @throws \think\exception\DbException
  144. */
  145. public static function storeProductOrderRefundY($data, $oid)
  146. {
  147. $order = AdminStoreOrder::where('id', $oid)->find();
  148. if ($order['is_channel'] == 1)
  149. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  150. else
  151. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  152. }
  153. /**
  154. * TODO 后台余额退款
  155. * @param $product
  156. * @param $refund_data
  157. * @throws \Exception
  158. */
  159. public static function storeOrderYueRefund($product, $refund_data)
  160. {
  161. $res = AdminStoreOrder::integralBack($product['id']);
  162. if (!$res) exception('退积分失败!');
  163. }
  164. /**
  165. * 订单退积分
  166. * @param $product $product 商品信息
  167. * @param $back_integral $back_integral 退多少积分
  168. */
  169. public static function storeOrderIntegralBack($product, $back_integral)
  170. {
  171. }
  172. public static function aliPay($orderId, $field = 'order_id')
  173. {
  174. if (is_string($orderId))
  175. $orderInfo = StoreOrder::where($field, $orderId)->where('is_del', 0)->find();
  176. else
  177. $orderInfo = $orderId;
  178. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  179. if ($orderInfo['paid']) exception('支付已支付!');
  180. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  181. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  182. $site_name = sys_config('site_name');
  183. if (!$site_name || !$bodyContent) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  184. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  185. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  186. $aliPay = new AlipayService();
  187. $aliPay->setAppid($alipay['alipay_app_id']);
  188. $aliPay->setNotifyUrl($notifyUrl);
  189. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  190. $aliPay->setTotalFee($orderInfo['pay_price']);
  191. $aliPay->setOutTradeNo($orderInfo['order_id']);
  192. $aliPay->setOrderName(StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  193. $aliPay->setPassbackParams(['attach' => 'product']);
  194. $orderStr = $aliPay->getOrderStr();
  195. return $orderStr;
  196. }
  197. }