OrderRepository.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\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. * 微信app支付
  90. * @param $order
  91. * @param string $field
  92. * @return array|string
  93. * @throws Exception
  94. */
  95. public static function appPay($order, string $field = 'order_id')
  96. {
  97. if (is_string($order))
  98. $orderInfo = StoreOrder::where($field, $order)->find();
  99. else
  100. $orderInfo = $order;
  101. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  102. if ($orderInfo['paid']) exception('支付已支付!');
  103. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  104. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  105. $site_name = sys_config('site_name');
  106. $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret'], true);
  107. $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open'], true);
  108. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  109. $config = array(
  110. 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id
  111. 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret
  112. 'mchid' => $payment['pay_weixin_mchid'], //商户id
  113. 'key' => $payment['pay_weixin_key'], //填写你设定的key
  114. 'sslcert_path' => realpath('.' . $payment['pay_weixin_client_cert']),
  115. 'sslkey_path' => realpath('.' . $payment['pay_weixin_client_key']),
  116. 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径
  117. );
  118. $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config);
  119. $pay_data = [
  120. 'body' => StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), //内容
  121. 'attach' => 'product', //商家数据包
  122. 'out_trade_no' => $orderInfo['order_id'], //商户订单号
  123. 'total_fee' => bcmul($orderInfo['pay_price'], 100, 0), //支付价格(单位:分)
  124. 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址
  125. ];
  126. $url = $wechatpay->handle($pay_data);
  127. return $url;
  128. }
  129. /**
  130. * 用户确认收货
  131. * @param $order
  132. * @param $uid
  133. * @throws \think\Exception
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. * @throws \think\exception\DbException
  137. */
  138. public static function storeProductOrderUserTakeDelivery($order, $uid)
  139. {
  140. $res1 = StoreOrder::gainUserIntegral($order);
  141. $res2 = User::backOrderBrokerage($order);
  142. StoreOrder::orderTakeAfter($order);
  143. //满赠优惠券
  144. WechatUser::userTakeOrderGiveCoupon($uid, $order['pay_price']);
  145. if (!($res1 && $res2)) exception('收货失败!');
  146. }
  147. /**
  148. * 修改状态 为已收货 admin模块
  149. * @param $order
  150. * @throws \Exception
  151. */
  152. public static function storeProductOrderTakeDeliveryAdmin($order)
  153. {
  154. $res1 = AdminStoreOrder::gainUserIntegral($order);
  155. $res2 = User::backOrderBrokerage($order);
  156. AdminStoreOrder::orderTakeAfter($order);
  157. //满赠优惠券
  158. WechatUser::userTakeOrderGiveCoupon($order['uid'], $order['pay_price']);
  159. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  160. if (!($res1 && $res2)) exception('收货失败!');
  161. }
  162. /**
  163. * 修改状态 为已收货 定时任务使用
  164. * @param $order
  165. * @throws \Exception
  166. */
  167. public static function storeProductOrderTakeDeliveryTimer($order)
  168. {
  169. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  170. $res2 = User::backOrderBrokerage($order, false);
  171. AdminStoreOrder::orderTakeAfter($order);
  172. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  173. if (!($res1 && $res2)) exception('收货失败!');
  174. }
  175. /**
  176. * 修改状态为 已退款 admin模块
  177. * @param $data
  178. * @param $oid
  179. * @return bool|mixed
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\ModelNotFoundException
  182. * @throws \think\exception\DbException
  183. */
  184. public static function storeProductOrderRefundY($data, $oid)
  185. {
  186. $order = AdminStoreOrder::where('id', $oid)->find();
  187. if ($order['is_channel'] == 1)
  188. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  189. else
  190. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  191. }
  192. /**
  193. * TODO 后台余额退款
  194. * @param $product
  195. * @param $refund_data
  196. * @throws \Exception
  197. */
  198. public static function storeOrderYueRefund($product, $refund_data)
  199. {
  200. $res = AdminStoreOrder::integralBack($product['id']);
  201. if (!$res) exception('退积分失败!');
  202. }
  203. /**
  204. * 订单退积分
  205. * @param $product $product 商品信息
  206. * @param $back_integral $back_integral 退多少积分
  207. */
  208. public static function storeOrderIntegralBack($product, $back_integral)
  209. {
  210. }
  211. }