OrderPayServices.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * @param string $payType
  38. * @return array|string
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function orderPay(array $orderInfo, string $payType)
  44. {
  45. if ($orderInfo['paid']) {
  46. throw new ApiException(410174);
  47. }
  48. if ($orderInfo['pay_price'] <= 0) {
  49. throw new ApiException(410274);
  50. }
  51. $openid = '';
  52. if (!in_array($payType, ['weixinh5', 'pc']) && !request()->isApp()) {
  53. if ($payType === 'weixin') {
  54. $userType = 'wechat';
  55. } else {
  56. $userType = $payType;
  57. }
  58. /** @var WechatUserServices $services */
  59. $services = app()->make(WechatUserServices::class);
  60. $openid = $services->uidToOpenid($orderInfo['pay_uid'] ?? $orderInfo['uid'], $userType);
  61. if (!$openid) {
  62. throw new ApiException(410275);
  63. }
  64. }
  65. $site_name = sys_config('site_name');
  66. if (isset($orderInfo['member_type'])) {
  67. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 20);
  68. $successAction = "member";
  69. /** @var OtherOrderServices $otherOrderServices */
  70. $otherOrderServices = app()->make(OtherOrderServices::class);
  71. $otherOrderServices->update($orderInfo['id'], ['pay_type' => 'alipay']);
  72. } else {
  73. /** @var StoreOrderCartInfoServices $orderInfoServices */
  74. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  75. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  76. $body = Str::substrUTf8($site_name . '--' . $body, 20);
  77. $successAction = "product";
  78. /** @var StoreOrderServices $orderServices */
  79. $orderServices = app()->make(StoreOrderServices::class);
  80. $orderServices->update($orderInfo['id'], ['pay_type' => 'weixin']);
  81. }
  82. if (!$body) {
  83. throw new ApiException(410276);
  84. }
  85. return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
  86. }
  87. /**
  88. * 支付宝支付
  89. * @param array $orderInfo
  90. * @param string $quitUrl
  91. * @return array|string
  92. */
  93. public function alipayOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
  94. {
  95. if ($orderInfo['paid']) {
  96. throw new ApiException(410174);
  97. }
  98. if ($orderInfo['pay_price'] <= 0) {
  99. throw new ApiException(410274);
  100. }
  101. $site_name = sys_config('site_name');
  102. if (isset($orderInfo['member_type'])) {
  103. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  104. $successAction = "member";
  105. /** @var OtherOrderServices $otherOrderServices */
  106. $otherOrderServices = app()->make(OtherOrderServices::class);
  107. $otherOrderServices->update($orderInfo['id'], ['pay_type' => 'alipay']);
  108. } else {
  109. /** @var StoreOrderCartInfoServices $orderInfoServices */
  110. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  111. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  112. $body = Str::substrUTf8($site_name . '--' . $body, 30);
  113. $successAction = "product";
  114. /** @var StoreOrderServices $orderServices */
  115. $orderServices = app()->make(StoreOrderServices::class);
  116. $orderServices->update($orderInfo['id'], ['pay_type' => 'alipay']);
  117. }
  118. if (!$body) {
  119. throw new ApiException(410276);
  120. }
  121. return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $isCode);
  122. }
  123. }