Payment.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\index\service;
  13. use app\index\model\User as UserModel;
  14. use app\index\service\order\PaySuccess;
  15. use app\index\service\User as UserService;
  16. use app\index\model\wxapp\Setting as WxappSettingModel;
  17. use app\common\enum\OrderType as OrderTypeEnum;
  18. use app\common\enum\order\PayType as OrderPayTypeEnum;
  19. use app\common\library\paypal\PayPal;
  20. use app\common\service\BaseService;
  21. use app\common\library\wechat\WxPay;
  22. use app\common\exception\BaseException;
  23. use think\facade\Log;
  24. /**
  25. * 订单支付服务类
  26. * Class Payment
  27. * @package app\api\service
  28. */
  29. class Payment extends BaseService
  30. {
  31. /**
  32. * 构建订单支付参数
  33. * @param $order
  34. * @param $payType
  35. * @return array
  36. * @throws BaseException
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public static function orderPayment($order, $payType): array
  42. {
  43. if ($payType == OrderPayTypeEnum::WECHAT) {
  44. return self::wechat(
  45. $order['order_id'],
  46. $order['order_no'],
  47. $order['pay_price'],
  48. OrderTypeEnum::ORDER
  49. );
  50. }
  51. //paypal支付
  52. if ($payType == OrderPayTypeEnum::PAYPAL) {
  53. $confs = config('paypal');
  54. if (is_debug()) {
  55. $conf = $confs['sandbox'];
  56. } else {
  57. $conf = $confs['live'];
  58. }
  59. $pp = new PayPal($conf);
  60. return $pp->unify($order['order_no'], $order['pay_price']);
  61. }
  62. //积分兑换
  63. if ($payType == OrderPayTypeEnum::POINTS) {
  64. $userInfo = User::getCurrentLoginUser();
  65. $points = $userInfo['points'];
  66. $payPoints = intval(bcmul(strval($order['pay_price']), strval(\app\common\model\User::POINTS_FOR_ONE_DOLLAR), 0));//订单所需积分
  67. if (intval($points) < $payPoints) {
  68. return ['flag' => false, 'message' => '积分不够'];
  69. } else {
  70. $orderModel = new PaySuccess($order['order_no']);
  71. $transId = 'VP' . date('YmdHis') . $userInfo['user_id'];
  72. $describe = "User consumption:{$order['order_no']}";
  73. UserModel::setIncPoints($userInfo['user_id'], -$payPoints, $describe);
  74. $status = $orderModel->onPaySuccess(OrderPayTypeEnum::POINTS, ['transaction_id' => $transId]);
  75. if ($status) {
  76. return ['flag' => true, 'message' => 'success'];
  77. } else {
  78. Log::error('orderPayment:积分兑换error,userId:' . $userInfo['user_id'] . ',orderNo:' . $order['order_no']);
  79. return ['flag' => false, 'message' => 'error'];
  80. }
  81. }
  82. }
  83. //todo stripe
  84. return [];
  85. }
  86. /**
  87. * 构建微信支付
  88. * @param $orderId
  89. * @param $orderNo
  90. * @param $payPrice
  91. * @param int $orderType
  92. * @return array
  93. * @throws BaseException
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public static function wechat(
  99. $orderId,
  100. $orderNo,
  101. $payPrice,
  102. int $orderType = OrderTypeEnum::ORDER
  103. ): array
  104. {
  105. // 获取当前用户信息
  106. $userInfo = UserService::getCurrentLoginUser(true);
  107. // 获取第三方用户信息(微信)
  108. $oauth = UserService::getOauth($userInfo['user_id'], 'MP-WEIXIN');
  109. empty($oauth) && throwError('没有找到第三方用户信息oauth');
  110. // 统一下单API
  111. $WxPay = new WxPay(static::getWxConfig());
  112. return $WxPay->unifiedorder($orderNo, $oauth['oauth_id'], $payPrice, $orderType);
  113. }
  114. /**
  115. * 获取微信支付配置
  116. * @return array
  117. * @throws BaseException
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. private static function getWxConfig(): array
  123. {
  124. $storeId = getStoreId();
  125. return WxappSettingModel::getWxappConfig($storeId);
  126. }
  127. }