123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\index\service;
- use app\index\model\User as UserModel;
- use app\index\service\order\PaySuccess;
- use app\index\service\User as UserService;
- use app\index\model\wxapp\Setting as WxappSettingModel;
- use app\common\enum\OrderType as OrderTypeEnum;
- use app\common\enum\order\PayType as OrderPayTypeEnum;
- use app\common\library\paypal\PayPal;
- use app\common\service\BaseService;
- use app\common\library\wechat\WxPay;
- use app\common\exception\BaseException;
- use think\facade\Log;
- /**
- * 订单支付服务类
- * Class Payment
- * @package app\api\service
- */
- class Payment extends BaseService
- {
- /**
- * 构建订单支付参数
- * @param $order
- * @param $payType
- * @return array
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function orderPayment($order, $payType): array
- {
- if ($payType == OrderPayTypeEnum::WECHAT) {
- return self::wechat(
- $order['order_id'],
- $order['order_no'],
- $order['pay_price'],
- OrderTypeEnum::ORDER
- );
- }
- //paypal支付
- if ($payType == OrderPayTypeEnum::PAYPAL) {
- $confs = config('paypal');
- if (is_debug()) {
- $conf = $confs['sandbox'];
- } else {
- $conf = $confs['live'];
- }
- $pp = new PayPal($conf);
- return $pp->unify($order['order_no'], $order['pay_price']);
- }
- //积分兑换
- if ($payType == OrderPayTypeEnum::POINTS) {
- $userInfo = User::getCurrentLoginUser();
- $points = $userInfo['points'];
- $payPoints = intval(bcmul(strval($order['pay_price']), strval(\app\common\model\User::POINTS_FOR_ONE_DOLLAR), 0));//订单所需积分
- if (intval($points) < $payPoints) {
- return ['flag' => false, 'message' => '积分不够'];
- } else {
- $orderModel = new PaySuccess($order['order_no']);
- $transId = 'VP' . date('YmdHis') . $userInfo['user_id'];
- $describe = "User consumption:{$order['order_no']}";
- UserModel::setIncPoints($userInfo['user_id'], -$payPoints, $describe);
- $status = $orderModel->onPaySuccess(OrderPayTypeEnum::POINTS, ['transaction_id' => $transId]);
- if ($status) {
- return ['flag' => true, 'message' => 'success'];
- } else {
- Log::error('orderPayment:积分兑换error,userId:' . $userInfo['user_id'] . ',orderNo:' . $order['order_no']);
- return ['flag' => false, 'message' => 'error'];
- }
- }
- }
- //todo stripe
- return [];
- }
- /**
- * 构建微信支付
- * @param $orderId
- * @param $orderNo
- * @param $payPrice
- * @param int $orderType
- * @return array
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function wechat(
- $orderId,
- $orderNo,
- $payPrice,
- int $orderType = OrderTypeEnum::ORDER
- ): array
- {
- // 获取当前用户信息
- $userInfo = UserService::getCurrentLoginUser(true);
- // 获取第三方用户信息(微信)
- $oauth = UserService::getOauth($userInfo['user_id'], 'MP-WEIXIN');
- empty($oauth) && throwError('没有找到第三方用户信息oauth');
- // 统一下单API
- $WxPay = new WxPay(static::getWxConfig());
- return $WxPay->unifiedorder($orderNo, $oauth['oauth_id'], $payPrice, $orderType);
- }
- /**
- * 获取微信支付配置
- * @return array
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private static function getWxConfig(): array
- {
- $storeId = getStoreId();
- return WxappSettingModel::getWxappConfig($storeId);
- }
- }
|