OrderRepository.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\user\User;
  5. use app\models\user\UserRecharge;
  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. // $res3 = StoreOrder::gainWhiteIntegral($order);
  104. // $res4 = StoreOrder::gainBusinessIntegral($order);
  105. // $res2 = User::backOrderBrokerage($order);
  106. $res2 = User::sendBackOrderBrokerage($order);
  107. StoreOrder::orderTakeAfter($order);
  108. //满赠优惠券
  109. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  110. if (!($res2)) exception('收货失败!');
  111. }
  112. /**
  113. * 修改状态 为已收货 admin模块
  114. * @param $order
  115. * @throws \Exception
  116. */
  117. public static function storeProductOrderTakeDeliveryAdmin($order)
  118. {
  119. // $res1 = AdminStoreOrder::gainUserIntegral($order);
  120. // $res2 = User::backOrderBrokerage($order);
  121. $res2 = User::sendBackOrderBrokerage($order);
  122. AdminStoreOrder::orderTakeAfter($order);
  123. if (!($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. AdminStoreOrder::orderTakeAfter($order);
  135. if (!($res2)) exception('收货失败!');
  136. }
  137. /**
  138. * 修改状态为 已退款 admin模块
  139. * @param $data
  140. * @param $oid
  141. * @return bool|mixed
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. * @throws \think\exception\DbException
  145. */
  146. public static function storeProductOrderRefundY($data, $oid)
  147. {
  148. $order = AdminStoreOrder::where('id', $oid)->find();
  149. if ($order['is_channel'] == 1)
  150. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  151. else
  152. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  153. }
  154. /**
  155. * TODO 后台余额退款
  156. * @param $product
  157. * @param $refund_data
  158. * @throws \Exception
  159. */
  160. public static function storeOrderYueRefund($product, $refund_data)
  161. {
  162. $res = AdminStoreOrder::integralBack($product['id']);
  163. if (!$res) exception('退积分失败!');
  164. }
  165. /**
  166. * 订单退积分
  167. * @param $product $product 商品信息
  168. * @param $back_integral $back_integral 退多少积分
  169. */
  170. public static function storeOrderIntegralBack($product, $back_integral)
  171. {
  172. }
  173. public static function aliPay($orderId, $field = 'order_id')
  174. {
  175. if (is_string($orderId))
  176. $orderInfo = StoreOrder::where($field, $orderId)->where('is_del', 0)->find();
  177. else
  178. $orderInfo = $orderId;
  179. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  180. if ($orderInfo['paid']) exception('支付已支付!');
  181. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  182. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  183. $site_name = sys_config('site_name');
  184. if (!$site_name || !$bodyContent) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  185. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  186. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  187. $aliPay = new AlipayService();
  188. $aliPay->setAppid($alipay['alipay_app_id']);
  189. $aliPay->setNotifyUrl($notifyUrl);
  190. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  191. $aliPay->setTotalFee($orderInfo['pay_price']);
  192. $aliPay->setOutTradeNo($orderInfo['order_id']);
  193. $aliPay->setOrderName(StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  194. $aliPay->setPassbackParams(['attach' => 'product']);
  195. $orderStr = $aliPay->getOrderStr();
  196. return $orderStr;
  197. }
  198. /**
  199. * 微信app支付
  200. * @param $order
  201. * @param string $field
  202. * @return array|string
  203. * @throws Exception
  204. */
  205. public static function appPay($order, string $field = 'order_id')
  206. {
  207. if (is_string($order))
  208. $orderInfo = StoreOrder::where($field, $order)->find();
  209. else
  210. $orderInfo = $order;
  211. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  212. if ($orderInfo['paid']) exception('支付已支付!');
  213. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  214. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  215. $site_name = sys_config('site_name');
  216. $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret'], true);
  217. $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open'], true);
  218. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  219. $config = array(
  220. 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id
  221. 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret
  222. 'mchid' => $payment['pay_weixin_mchid'], //商户id
  223. 'key' => $payment['pay_weixin_key'], //填写你设定的key
  224. 'sslcert_path' => realpath('.' . $payment['pay_weixin_client_cert']),
  225. 'sslkey_path' => realpath('.' . $payment['pay_weixin_client_key']),
  226. 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径
  227. );
  228. $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config);
  229. $pay_data = [
  230. 'body' => StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), //内容
  231. 'attach' => 'product', //商家数据包
  232. 'out_trade_no' => $orderInfo['order_id'], //商户订单号
  233. 'total_fee' => bcmul($orderInfo['pay_price'], 100, 0), //支付价格(单位:分)
  234. 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址
  235. ];
  236. $url = $wechatpay->handle($pay_data);
  237. return $url;
  238. }
  239. /**
  240. * 微信app支付
  241. * @param $order
  242. * @param string $field
  243. * @return array|string
  244. * @throws Exception
  245. */
  246. public static function appPayZf($order, string $field = 'order_id')
  247. {
  248. if (is_string($order))
  249. $orderInfo = UserRecharge::where($field, $order)->find();
  250. else
  251. $orderInfo = $order;
  252. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  253. if ($orderInfo['paid']) exception('支付已支付!');
  254. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  255. $site_name = sys_config('site_name');
  256. $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret'], true);
  257. $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open'], true);
  258. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  259. $config = array(
  260. 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id
  261. 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret
  262. 'mchid' => $payment['pay_weixin_mchid'], //商户id
  263. 'key' => $payment['pay_weixin_key'], //填写你设定的key
  264. 'sslcert_path' => realpath('.' . $payment['pay_weixin_client_cert']),
  265. 'sslkey_path' => realpath('.' . $payment['pay_weixin_client_key']),
  266. 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径
  267. );
  268. $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config);
  269. $pay_data = [
  270. 'body' => StoreOrder::getSubstrUTf8($site_name . ' - 充值' , 30), //内容
  271. 'attach' => 'product', //商家数据包
  272. 'out_trade_no' => $orderInfo['order_id'], //商户订单号
  273. 'total_fee' => bcmul($orderInfo['pay_price'], 100, 0), //支付价格(单位:分)
  274. 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址
  275. ];
  276. $url = $wechatpay->handle($pay_data);
  277. return $url;
  278. }
  279. }