RechargeServices.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\pay;
  13. use app\model\user\UserRecharge;
  14. use app\services\wechat\WechatUserServices;
  15. use crmeb\exceptions\ApiException;
  16. use crmeb\services\pay\extend\allinpay\AllinPay;
  17. /**
  18. *
  19. * Class RechargeServices
  20. * @package app\services\pay
  21. */
  22. class RechargeServices
  23. {
  24. protected $pay;
  25. /**
  26. * RechargeServices constructor.
  27. * @param PayServices $pay
  28. */
  29. public function __construct(PayServices $pay)
  30. {
  31. $this->pay = $pay;
  32. }
  33. public function recharge(UserRecharge $recharge)
  34. {
  35. if (!$recharge) {
  36. throw new ApiException(410173);
  37. }
  38. if ($recharge['paid'] == 1) {
  39. throw new ApiException(410174);
  40. }
  41. $payType = '';
  42. switch ($recharge['recharge_type']) {
  43. case 'weixin':
  44. case 'weixinh5':
  45. case 'routine':
  46. $payType = PayServices::WEIXIN_PAY;
  47. break;
  48. case PayServices::ALIAPY_PAY:
  49. $payType = PayServices::ALIAPY_PAY;
  50. break;
  51. }
  52. $payType = app()->make(OrderPayServices::class)->getPayType($payType);
  53. if (!$payType) {
  54. throw new ApiException(410278);
  55. }
  56. if ($recharge['recharge_type'] == PayServices::WEIXIN_PAY && !request()->isH5() && !request()->isApp()) {
  57. /** @var WechatUserServices $wechatUser */
  58. $wechatUser = app()->make(WechatUserServices::class);
  59. if (request()->isApp()) {
  60. $userType = 'app';
  61. } else if (request()->isRoutine()) {
  62. $userType = 'routine';
  63. } else if (request()->isWechat()) {
  64. $userType = 'wechat';
  65. } else {
  66. throw new ApiException(410275);
  67. }
  68. $openid = $wechatUser->uidToOpenid((int)$recharge['uid'], $userType);
  69. if (!$openid) {
  70. throw new ApiException(410275);
  71. }
  72. } else {
  73. $openid = '';
  74. }
  75. $res = $this->pay->pay($payType, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值', ['openid' => $openid]);
  76. if ($payType == PayServices::WEIXIN_PAY) {
  77. if (request()->isH5()) {
  78. $payStstus = 'wechat_h5_pay';
  79. } else {
  80. $payStstus = 'wechat_pay';
  81. }
  82. } else if ($payType == PayServices::ALIAPY_PAY) {
  83. $payStstus = 'alipay_pay';
  84. } else if ($payType == PayServices::ALLIN_PAY) {
  85. $payStstus = 'allinpay_pay';
  86. }
  87. return ['pay_url' => AllinPay::UNITODER_H5UNIONPAY, 'jsConfig' => $res, 'pay_key' => md5($recharge['order_id']), 'order_id' => $recharge['order_id'], 'pay_type' => strtoupper($payStstus)];
  88. }
  89. }