Payment.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $confs = config('paypal');
  50. if (is_debug()){
  51. $conf = $confs['sandbox'];
  52. }else{
  53. $conf = $confs['live'];
  54. }
  55. $pp = new PayPal($conf);
  56. return $pp->unify($order['order_no'], $order['pay_price']);
  57. }
  58. //todo stripe
  59. return [];
  60. }
  61. /**
  62. * 构建微信支付
  63. * @param $orderId
  64. * @param $orderNo
  65. * @param $payPrice
  66. * @param int $orderType
  67. * @return array
  68. * @throws BaseException
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public static function wechat(
  74. $orderId,
  75. $orderNo,
  76. $payPrice,
  77. int $orderType = OrderTypeEnum::ORDER
  78. ): array
  79. {
  80. // 获取当前用户信息
  81. $userInfo = UserService::getCurrentLoginUser(true);
  82. // 获取第三方用户信息(微信)
  83. $oauth = UserService::getOauth($userInfo['user_id'], 'MP-WEIXIN');
  84. empty($oauth) && throwError('没有找到第三方用户信息oauth');
  85. // 统一下单API
  86. $WxPay = new WxPay(static::getWxConfig());
  87. return $WxPay->unifiedorder($orderNo, $oauth['oauth_id'], $payPrice, $orderType);
  88. }
  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. private static function getWxConfig(): array
  98. {
  99. $storeId = getStoreId();
  100. return WxappSettingModel::getWxappConfig($storeId);
  101. }
  102. }