OrderPayServices.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. if ($payType !== 'weixinh5' && $payType !== 'pc') {
  46. if ($payType === 'weixin') {
  47. $userType = 'wechat';
  48. } else {
  49. $userType = $payType;
  50. }
  51. /** @var WechatUserServices $services */
  52. $services = app()->make(WechatUserServices::class);
  53. $openid = $services->uidToOpenid($orderInfo['uid'], $userType);
  54. if (!$openid) {
  55. throw new ValidateException('获取用户openid失败,无法支付');
  56. }
  57. } else {
  58. $openid = '';
  59. }
  60. $site_name = sys_config('site_name');
  61. if (isset($orderInfo['member_type'])) {
  62. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  63. $successAction = "member";
  64. } else {
  65. /** @var StoreOrderCartInfoServices $orderInfoServices */
  66. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  67. $body = $orderInfoServices->getCarIdByProductTitle($orderInfo['cart_id']);
  68. $body = Str::substrUTf8($site_name . '--' . $body, 30);
  69. $successAction = "product";
  70. }
  71. if (!$body) {
  72. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  73. }
  74. return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
  75. }
  76. /**
  77. * 支付宝支付
  78. * @param array $orderInfo
  79. * @param string $quitUrl
  80. * @return array|string
  81. */
  82. public function alipayOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
  83. {
  84. if ($orderInfo['paid']) {
  85. throw new ValidateException('订单已支付!');
  86. }
  87. if ($orderInfo['pay_price'] <= 0) {
  88. throw new ValidateException('该支付无需支付!');
  89. }
  90. $site_name = sys_config('site_name');
  91. if (isset($orderInfo['member_type'])) {
  92. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  93. $successAction = "member";
  94. } else {
  95. /** @var StoreOrderCartInfoServices $orderInfoServices */
  96. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  97. $body = $orderInfoServices->getCarIdByProductTitle($orderInfo['cart_id']);
  98. $body = Str::substrUTf8($site_name . '--' . $body, 30);
  99. $successAction = "product";
  100. }
  101. if (!$body) {
  102. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  103. }
  104. return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $isCode);
  105. }
  106. }