Payment.php 3.4 KB

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