Payment.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $conf = config('paypal');
  54. $pp = new PayPal($conf);
  55. return $pp->unify($order['order_no'], $order['pay_price']);
  56. }
  57. //积分兑换
  58. if ($payType == OrderPayTypeEnum::POINTS) {
  59. $userInfo = User::getCurrentLoginUser();
  60. $points = $userInfo['points'];
  61. $payPoints = intval(bcmul(strval($order['pay_price']), '100', 0));//订单所需积分
  62. if (intval($points) < $payPoints) {
  63. return ['flag' => false, 'message' => '积分不够'];
  64. } else {
  65. $orderModel = new PaySuccess($order['order_no']);
  66. $transId = 'VP' . date('YmdHis') . $userInfo['user_id'];
  67. $describe = "用户消费:{$order['order_no']}";
  68. UserModel::setIncPoints($userInfo['user_id'], -$payPoints, $describe);
  69. $status = $orderModel->onPaySuccess(OrderPayTypeEnum::POINTS, ['transaction_id' => $transId]);
  70. if ($status) {
  71. return ['flag' => true, 'message' => 'success'];
  72. } else {
  73. Log::error('orderPayment:积分兑换error,userId:' . $userInfo['user_id'] . ',orderNo:' . $order['order_no']);
  74. return ['flag' => false, 'message' => 'error'];
  75. }
  76. }
  77. }
  78. //todo stripe
  79. return [];
  80. }
  81. /**
  82. * 构建微信支付
  83. * @param $orderId
  84. * @param $orderNo
  85. * @param $payPrice
  86. * @param int $orderType
  87. * @return array
  88. * @throws BaseException
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public static function wechat(
  94. $orderId,
  95. $orderNo,
  96. $payPrice,
  97. int $orderType = OrderTypeEnum::ORDER
  98. ): array
  99. {
  100. // 获取当前用户信息
  101. $userInfo = UserService::getCurrentLoginUser(true);
  102. // 获取第三方用户信息(微信)
  103. $oauth = UserService::getOauth($userInfo['user_id'], 'MP-WEIXIN');
  104. empty($oauth) && throwError('没有找到第三方用户信息oauth');
  105. // 统一下单API
  106. $WxPay = new WxPay(static::getWxConfig());
  107. return $WxPay->unifiedorder($orderNo, $oauth['oauth_id'], $payPrice, $orderType);
  108. }
  109. /**
  110. * 获取微信支付配置
  111. * @return array
  112. * @throws BaseException
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. private static function getWxConfig(): array
  118. {
  119. $storeId = getStoreId();
  120. return WxappSettingModel::getWxappConfig($storeId);
  121. }
  122. }