OrderRepository.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\user\User;
  5. use app\models\user\UserBill;
  6. use app\models\user\WechatUser;
  7. use app\admin\model\order\StoreOrder as AdminStoreOrder;
  8. use crmeb\services\JiaLieService;
  9. use crmeb\services\MiniProgramService;
  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 $orderId
  91. * @param string $field
  92. */
  93. public static function barcodePay($orderId,$auth_code, $field = 'order_id')
  94. {
  95. if (is_string($orderId))
  96. $orderInfo = StoreOrder::where($field, $orderId)->find();
  97. else
  98. $orderInfo = $orderId;
  99. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  100. if ($orderInfo['paid']) exception('支付已支付!');
  101. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  102. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  103. $site_name = sys_config('site_name');
  104. $device_info = "myc".$orderInfo['store_id'];
  105. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  106. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB',compact('auth_code','device_info'));
  107. }
  108. /**
  109. * 嘉联支付
  110. * @param $orderId
  111. * @param $auth_code
  112. * @param string $field
  113. */
  114. public static function jialiePay($orderId,$auth_code, $field = 'order_id|re_order_id')
  115. {
  116. if (is_string($orderId))
  117. $orderInfo = StoreOrder::where($field, $orderId)->find();
  118. else
  119. $orderInfo = $orderId;
  120. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  121. if ($orderInfo['paid']) exception('支付已支付!');
  122. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  123. $bodyContent = '商品订单';
  124. $site_name = sys_config('site_name');
  125. $device_info = "myc".$orderInfo['store_id'];
  126. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  127. $data['pay_type'] = 'jialie';
  128. $data['auth_code'] = $auth_code;
  129. $data['body'] = $bodyContent;
  130. $data['total_fee'] = bcmul($orderInfo['pay_price'],100,0);
  131. $data['nonce_str'] = uniqid();
  132. $data['out_trade_no'] = !empty($orderInfo['re_order_id'])?$orderInfo['re_order_id']:$orderInfo['order_id'];
  133. $data['mch_create_ip'] = app('request')->ip();
  134. $data['attach'] = 'product';
  135. $rs = JiaLieService::initialize()::paymentOrder(JiaLieService::MICROPAY,$data);
  136. return $rs;
  137. }
  138. /**
  139. * 用户确认收货
  140. * @param $order
  141. * @param $uid
  142. * @throws \think\Exception
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. */
  147. public static function storeProductOrderUserTakeDelivery($order, $uid)
  148. {
  149. $res1 = StoreOrder::gainUserIntegral($order);
  150. $res2 = User::backOrderBrokerage($order);
  151. StoreOrder::orderTakeAfter($order);
  152. //满赠优惠券
  153. WechatUser::userTakeOrderGiveCoupon($uid, $order['pay_price']);
  154. if (!($res1 && $res2)) exception('收货失败!');
  155. }
  156. /**
  157. * 修改状态 为已收货 admin模块
  158. * @param $order
  159. * @throws \Exception
  160. */
  161. public static function storeProductOrderTakeDeliveryAdmin($order)
  162. {
  163. $res1 = AdminStoreOrder::gainUserIntegral($order);
  164. $res2 = User::backOrderBrokerage($order);
  165. AdminStoreOrder::orderTakeAfter($order);
  166. //满赠优惠券
  167. WechatUser::userTakeOrderGiveCoupon($order['uid'], $order['pay_price']);
  168. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  169. if (!($res1 && $res2)) exception('收货失败!');
  170. }
  171. /**
  172. * 修改状态 为已收货 定时任务使用
  173. * @param $order
  174. * @throws \Exception
  175. */
  176. public static function storeProductOrderTakeDeliveryTimer($order)
  177. {
  178. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  179. $res2 = User::backOrderBrokerage($order, false);
  180. AdminStoreOrder::orderTakeAfter($order);
  181. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  182. if (!($res1 && $res2)) exception('收货失败!');
  183. }
  184. /**
  185. * 修改状态为 已退款 admin模块
  186. * @param $data
  187. * @param $oid
  188. * @return bool|mixed
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. * @throws \think\exception\DbException
  192. */
  193. public static function storeProductOrderRefundY($data, $oid)
  194. {
  195. $order = AdminStoreOrder::where('id', $oid)->find();
  196. if ($order['is_channel'] == 1)
  197. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  198. else
  199. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  200. }
  201. /**
  202. * TODO 后台余额退款
  203. * @param $product
  204. * @param $refund_data
  205. * @throws \Exception
  206. */
  207. public static function storeOrderYueRefund($product, $refund_data)
  208. {
  209. $res = AdminStoreOrder::integralBack($product['id']);
  210. if (!$res) exception('退积分失败!');
  211. }
  212. /**
  213. * 订单退积分
  214. * @param $product $product 商品信息
  215. * @param $back_integral $back_integral 退多少积分
  216. */
  217. public static function storeOrderIntegralBack($product, $back_integral)
  218. {
  219. }
  220. }