Payment.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service;
  13. use app\api\service\User as UserService;
  14. use app\api\model\wxapp\Setting as WxappSettingModel;
  15. use app\common\enum\OrderType as OrderTypeEnum;
  16. use app\common\enum\order\PayType as OrderPayTypeEnum;
  17. use app\common\service\BaseService;
  18. use app\common\library\wechat\WxPay;
  19. use app\common\exception\BaseException;
  20. /**
  21. * 订单支付服务类
  22. * Class Payment
  23. * @package app\api\service
  24. */
  25. class Payment extends BaseService
  26. {
  27. /**
  28. * 构建订单支付参数
  29. * @param $order
  30. * @param $payType
  31. * @return array
  32. * @throws BaseException
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public static function orderPayment($order, $payType): array
  38. {
  39. if ($payType == OrderPayTypeEnum::WECHAT) {
  40. return self::wechat(
  41. $order['order_id'],
  42. $order['order_no'],
  43. $order['pay_price'],
  44. OrderTypeEnum::ORDER
  45. );
  46. }
  47. return [];
  48. }
  49. /**
  50. * 构建微信支付
  51. * @param $orderId
  52. * @param $orderNo
  53. * @param $payPrice
  54. * @param int $orderType
  55. * @return array
  56. * @throws BaseException
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public static function wechat(
  62. $orderId,
  63. $orderNo,
  64. $payPrice,
  65. int $orderType = OrderTypeEnum::ORDER
  66. ): array
  67. {
  68. // 获取当前用户信息
  69. $userInfo = UserService::getCurrentLoginUser(true);
  70. // 获取第三方用户信息(微信)
  71. $oauth = UserService::getOauth($userInfo['user_id'], 'MP-WEIXIN');
  72. empty($oauth) && throwError('没有找到第三方用户信息oauth');
  73. // 统一下单API
  74. $WxPay = new WxPay(static::getWxConfig());
  75. return $WxPay->unifiedorder($orderNo, $oauth['oauth_id'], $payPrice, $orderType);
  76. }
  77. /**
  78. * 获取微信支付配置
  79. * @return array
  80. * @throws BaseException
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. private static function getWxConfig(): array
  86. {
  87. $storeId = getStoreId();
  88. return WxappSettingModel::getWxappConfig($storeId);
  89. }
  90. /**
  91. * @return array
  92. * @throws BaseException
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public static function getPaySetting()
  98. {
  99. $setting = static::getWxConfig();
  100. $setting['pay_type'] = [
  101. ['name'=>'微信支付','value'=>20]
  102. ];
  103. unset($setting['app_id']);
  104. unset($setting['app_secret']);
  105. unset($setting['mchid']);
  106. unset($setting['apikey']);
  107. unset($setting['cert_pem']);
  108. unset($setting['key_pem']);
  109. return $setting;
  110. }
  111. }