OrderRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StoreOrderCartInfo;
  5. use app\models\user\User;
  6. use app\models\user\WechatUser;
  7. use app\admin\model\order\StoreOrder as AdminStoreOrder;
  8. use crmeb\services\AlipayService;
  9. use crmeb\services\MiniProgramService;
  10. use crmeb\services\SystemConfigService;
  11. use crmeb\services\WechatService;
  12. /**
  13. * Class OrderRepository
  14. * @package crmeb\repositories
  15. */
  16. class OrderRepository
  17. {
  18. /**
  19. * TODO 小程序JS支付
  20. * @param $orderId
  21. * @param string $field
  22. * @return array|string
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. */
  27. public static function jsPay($orderId, $field = 'order_id')
  28. {
  29. if (is_string($orderId))
  30. $orderInfo = StoreOrder::where($field, $orderId)->find();
  31. else
  32. $orderInfo = $orderId;
  33. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  34. if ($orderInfo['paid']) exception('支付已支付!');
  35. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  36. $openid = WechatUser::getOpenId($orderInfo['uid']);
  37. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  38. $site_name = sys_config('site_name');
  39. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  40. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  41. }
  42. /**
  43. * 微信公众号JS支付
  44. * @param $orderId
  45. * @param string $field
  46. * @return array|string
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public static function wxPay($orderId, $field = 'order_id')
  52. {
  53. if (is_string($orderId))
  54. $orderInfo = StoreOrder::where($field, $orderId)->find();
  55. else
  56. $orderInfo = $orderId;
  57. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  58. if ($orderInfo['paid']) exception('支付已支付!');
  59. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  60. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  61. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  62. $site_name = sys_config('site_name');
  63. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  64. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  65. }
  66. /**
  67. * 微信h5支付
  68. * @param $orderId
  69. * @param string $field
  70. * @return array|string
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @throws \think\exception\DbException
  74. */
  75. public static function h5Pay($orderId, $field = 'order_id')
  76. {
  77. if (is_string($orderId))
  78. $orderInfo = StoreOrder::where($field, $orderId)->find();
  79. else
  80. $orderInfo = $orderId;
  81. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  82. if ($orderInfo['paid']) exception('支付已支付!');
  83. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  84. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  85. $site_name = sys_config('site_name');
  86. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  87. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  88. }
  89. /**
  90. * 用户确认收货
  91. * @param $order
  92. * @param $uid
  93. * @throws \think\Exception
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. * @throws \think\db\exception\DbException
  98. * @throws \Exception
  99. */
  100. public static function storeProductOrderUserTakeDelivery($order, $uid)
  101. {
  102. $res1 = StoreOrder::gainUserIntegral($order);
  103. // $res2 = User::backOrderBrokerage($order);
  104. $res2 = User::sendBackOrderBrokerage($order);
  105. StoreOrderCartInfo::where('oid', $order['id'])->where('paid', 1)->update(['take' => 1]);
  106. StoreOrder::orderTakeAfter($order);
  107. //满赠优惠券
  108. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  109. if (!($res1 && $res2)) 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. StoreOrderCartInfo::where('oid', $order['id'])->where('paid', 1)->update(['take' => 1]);
  122. AdminStoreOrder::orderTakeAfter($order);
  123. if (!($res1 && $res2)) exception('收货失败!');
  124. }
  125. /**
  126. * 修改状态 为已收货 定时任务使用
  127. * @param $order
  128. * @throws \Exception
  129. */
  130. public static function storeProductOrderTakeDeliveryTimer($order)
  131. {
  132. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  133. $res2 = User::sendBackOrderBrokerage($order);
  134. StoreOrderCartInfo::where('oid', $order['id'])->where('paid', 1)->update(['take' => 1]);
  135. AdminStoreOrder::orderTakeAfter($order);
  136. if (!($res1 && $res2)) exception('收货失败!');
  137. }
  138. /**
  139. * 修改状态为 已退款 admin模块
  140. * @param $data
  141. * @param $oid
  142. * @return bool|mixed
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. */
  147. public static function storeProductOrderRefundY($data, $oid)
  148. {
  149. $order = AdminStoreOrder::where('id', $oid)->find();
  150. if ($order['is_channel'] == 1)
  151. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  152. else
  153. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  154. }
  155. /**
  156. * TODO 后台余额退款
  157. * @param $product
  158. * @param $refund_data
  159. * @throws \Exception
  160. */
  161. public static function storeOrderYueRefund($product, $refund_data)
  162. {
  163. $res = AdminStoreOrder::integralBack($product['id']);
  164. if (!$res) exception('退积分失败!');
  165. }
  166. /**
  167. * 订单退积分
  168. * @param $product $product 商品信息
  169. * @param $back_integral $back_integral 退多少积分
  170. */
  171. public static function storeOrderIntegralBack($product, $back_integral)
  172. {
  173. }
  174. /**
  175. * 微信app支付
  176. * @param $order
  177. * @param string $field
  178. * @return array|string
  179. * @throws Exception
  180. */
  181. public static function appPay($order, string $field = 'order_id')
  182. {
  183. if (is_string($order))
  184. $orderInfo = StoreOrder::where($field, $order)->find();
  185. else
  186. $orderInfo = $order;
  187. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  188. if ($orderInfo['paid']) exception('支付已支付!');
  189. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  190. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  191. $site_name = sys_config('site_name');
  192. $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret']);
  193. $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open']);
  194. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  195. $config = array(
  196. 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id
  197. 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret
  198. 'mchid' => $payment['pay_weixin_mchid'], //商户id
  199. 'key' => $payment['pay_weixin_key'], //填写你设定的key
  200. 'sslcert_path' => realpath('.' . $payment['pay_weixin_client_cert']),
  201. 'sslkey_path' => realpath('.' . $payment['pay_weixin_client_key']),
  202. 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径
  203. );
  204. $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config);
  205. $pay_data = [
  206. 'body' => StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), //内容
  207. 'attach' => 'product', //商家数据包
  208. 'out_trade_no' => $orderInfo['order_id'], //商户订单号
  209. 'total_fee' => bcmul($orderInfo['pay_price'], 100, 0), //支付价格(单位:分)
  210. 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址
  211. ];
  212. $url = $wechatpay->handle($pay_data);
  213. return $url;
  214. }
  215. public static function aliPay($orderId, $field = 'order_id')
  216. {
  217. if (is_string($orderId))
  218. $orderInfo = StoreOrder::where($field, $orderId)->where('is_del', 0)->find();
  219. else
  220. $orderInfo = $orderId;
  221. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  222. if ($orderInfo['paid']) exception('支付已支付!');
  223. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  224. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  225. $site_name = sys_config('site_name');
  226. if (!$site_name || !$bodyContent) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  227. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  228. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  229. $aliPay = new AlipayService();
  230. $aliPay->setAppid($alipay['alipay_app_id']);
  231. $aliPay->setNotifyUrl($notifyUrl);
  232. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  233. $aliPay->setTotalFee($orderInfo['pay_price']);
  234. $aliPay->setOutTradeNo($orderInfo['order_id']);
  235. $aliPay->setOrderName(StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  236. $aliPay->setPassbackParams(['attach' => 'product']);
  237. $orderStr = $aliPay->getOrderStr();
  238. return $orderStr;
  239. }
  240. }