OrderPayServices.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\pay;
  12. use app\services\order\OtherOrderServices;
  13. use app\services\order\StoreOrderCartInfoServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\wechat\WechatUserServices;
  16. use crmeb\exceptions\ApiException;
  17. use crmeb\utils\Str;
  18. /**
  19. * 订单发起支付
  20. * Class OrderPayServices
  21. * @package app\services\pay
  22. */
  23. class OrderPayServices
  24. {
  25. /**
  26. * 支付
  27. * @var PayServices
  28. */
  29. protected $payServices;
  30. public function __construct(PayServices $services)
  31. {
  32. $this->payServices = $services;
  33. }
  34. /**
  35. * 订单发起支付
  36. * @param array $orderInfo
  37. * @return array|string
  38. */
  39. public function orderPay(array $orderInfo, string $payType)
  40. {
  41. if ($orderInfo['paid']) {
  42. throw new ApiException(410174);
  43. }
  44. if ($orderInfo['pay_price'] <= 0) {
  45. throw new ApiException(410274);
  46. }
  47. $openid = '';
  48. if (!in_array($payType, ['weixinh5', 'pc']) && !request()->isApp()) {
  49. if ($payType === 'weixin') {
  50. $userType = 'wechat';
  51. } else {
  52. $userType = $payType;
  53. }
  54. /** @var WechatUserServices $services */
  55. $services = app()->make(WechatUserServices::class);
  56. $openid = $services->uidToOpenid($orderInfo['pay_uid'] ?? $orderInfo['uid'], $userType);
  57. if (!$openid) {
  58. throw new ApiException(410275);
  59. }
  60. }
  61. $site_name = sys_config('site_name');
  62. if (isset($orderInfo['member_type'])) {
  63. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 20);
  64. $successAction = "member";
  65. /** @var OtherOrderServices $otherOrderServices */
  66. $otherOrderServices = app()->make(OtherOrderServices::class);
  67. $otherOrderServices->update($orderInfo['id'], ['pay_type' => 'alipay']);
  68. } else {
  69. /** @var StoreOrderCartInfoServices $orderInfoServices */
  70. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  71. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id'], $orderInfo['cart_id']);
  72. $body = Str::substrUTf8($site_name . '--' . $body, 20);
  73. $successAction = "product";
  74. /** @var StoreOrderServices $orderServices */
  75. $orderServices = app()->make(StoreOrderServices::class);
  76. $orderServices->update($orderInfo['id'], ['pay_type' => 'weixin']);
  77. }
  78. if (!$body) {
  79. throw new ApiException(410276);
  80. }
  81. return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
  82. }
  83. /**
  84. * 支付宝支付
  85. * @param array $orderInfo
  86. * @param string $quitUrl
  87. * @return array|string
  88. */
  89. public function alipayOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
  90. {
  91. if ($orderInfo['paid']) {
  92. throw new ApiException(410174);
  93. }
  94. if ($orderInfo['pay_price'] <= 0) {
  95. throw new ApiException(410274);
  96. }
  97. $site_name = sys_config('site_name');
  98. if (isset($orderInfo['member_type'])) {
  99. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  100. $successAction = "member";
  101. /** @var OtherOrderServices $otherOrderServices */
  102. $otherOrderServices = app()->make(OtherOrderServices::class);
  103. $otherOrderServices->update($orderInfo['id'], ['pay_type' => 'alipay']);
  104. } else {
  105. /** @var StoreOrderCartInfoServices $orderInfoServices */
  106. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  107. $body = $orderInfoServices->getCarIdByProductTitle($orderInfo['id'], $orderInfo['cart_id']);
  108. $body = Str::substrUTf8($site_name . '--' . $body, 30);
  109. $successAction = "product";
  110. /** @var StoreOrderServices $orderServices */
  111. $orderServices = app()->make(StoreOrderServices::class);
  112. $orderServices->update($orderInfo['id'], ['pay_type' => 'alipay']);
  113. }
  114. if (!$body) {
  115. throw new ApiException(410276);
  116. }
  117. return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $isCode);
  118. }
  119. }