OrderPayServices.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\StoreOrderCartInfoServices;
  13. use app\services\wechat\WechatUserServices;
  14. use crmeb\utils\Str;
  15. use think\exception\ValidateException;
  16. /**
  17. * 订单发起支付
  18. * Class OrderPayServices
  19. * @package app\services\pay
  20. */
  21. class OrderPayServices
  22. {
  23. /**
  24. * 支付
  25. * @var PayServices
  26. */
  27. protected $payServices;
  28. public function __construct(PayServices $services)
  29. {
  30. $this->payServices = $services;
  31. }
  32. /**
  33. * 订单发起支付
  34. * @param array $orderInfo
  35. * @return array|string
  36. */
  37. public function orderPay(array $orderInfo, string $payType)
  38. {
  39. if ($orderInfo['paid']) {
  40. throw new ValidateException('订单已支付!');
  41. }
  42. if ($orderInfo['pay_price'] <= 0) {
  43. throw new ValidateException('该支付无需支付!');
  44. }
  45. $openid = '';
  46. if (!in_array($payType, ['weixinh5', 'pc']) && !request()->isApp()) {
  47. if ($payType === 'weixin') {
  48. $userType = 'wechat';
  49. } else {
  50. $userType = $payType;
  51. }
  52. /** @var WechatUserServices $services */
  53. $services = app()->make(WechatUserServices::class);
  54. $openid = $services->uidToOpenid($orderInfo['uid'], $userType);
  55. if (!$openid) {
  56. throw new ValidateException('获取用户openid失败,无法支付');
  57. }
  58. }
  59. $site_name = sys_config('site_name');
  60. if (isset($orderInfo['member_type'])) {
  61. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  62. $successAction = "member";
  63. } else {
  64. /** @var StoreOrderCartInfoServices $orderInfoServices */
  65. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  66. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id'], $orderInfo['cart_id']);
  67. $body = Str::substrUTf8($site_name . '--' . $body, 30);
  68. $successAction = "product";
  69. }
  70. if (!$body) {
  71. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  72. }
  73. return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
  74. }
  75. /**
  76. * 支付宝支付
  77. * @param array $orderInfo
  78. * @param string $quitUrl
  79. * @return array|string
  80. */
  81. public function alipayOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
  82. {
  83. if ($orderInfo['paid']) {
  84. throw new ValidateException('订单已支付!');
  85. }
  86. if ($orderInfo['pay_price'] <= 0) {
  87. throw new ValidateException('该支付无需支付!');
  88. }
  89. $site_name = sys_config('site_name');
  90. if (isset($orderInfo['member_type'])) {
  91. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  92. $successAction = "member";
  93. } else {
  94. /** @var StoreOrderCartInfoServices $orderInfoServices */
  95. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  96. $body = $orderInfoServices->getCarIdByProductTitle($orderInfo['id'], $orderInfo['cart_id']);
  97. $body = Str::substrUTf8($site_name . '--' . $body, 30);
  98. $successAction = "product";
  99. }
  100. if (!$body) {
  101. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  102. }
  103. return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $isCode);
  104. }
  105. }