<commit_before>package cn.momia.service.course.web.ctrl;

import cn.momia.api.course.dto.PaymentDto;
import cn.momia.api.user.UserServiceApi;
import cn.momia.api.user.dto.UserDto;
import cn.momia.common.api.exception.MomiaFailedException;
import cn.momia.common.api.http.MomiaHttpResponse;
import cn.momia.common.client.ClientType;
import cn.momia.common.deal.gateway.CallbackParam;
import cn.momia.common.deal.gateway.PayType;
import cn.momia.common.deal.gateway.PaymentGateway;
import cn.momia.common.deal.gateway.PrepayParam;
import cn.momia.common.deal.gateway.PrepayResult;
import cn.momia.common.deal.gateway.factory.CallbackParamFactory;
import cn.momia.common.deal.gateway.factory.PaymentGatewayFactory;
import cn.momia.common.webapp.config.Configuration;
import cn.momia.common.webapp.ctrl.BaseController;
import cn.momia.common.webapp.util.RequestUtil;
import cn.momia.service.course.subject.Subject;
import cn.momia.service.course.subject.SubjectService;
import cn.momia.service.course.subject.coupon.CouponService;
import cn.momia.service.course.subject.coupon.UserCoupon;
import cn.momia.service.course.subject.order.Order;
import cn.momia.service.course.subject.order.OrderService;
import cn.momia.service.course.subject.order.Payment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@RestController
@RequestMapping(value = "/payment")
public class PaymentController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PaymentController.class);

    @Autowired private SubjectService subjectService;
    @Autowired private OrderService orderService;
    @Autowired private CouponService couponService;
    @Autowired private UserServiceApi userServiceApi;

    @RequestMapping(value = "/prepay/alipay", method = RequestMethod.POST)
    public MomiaHttpResponse prepayAlipay(HttpServletRequest request) {
        return prepay(request, PayType.ALIPAY);
    }

    private MomiaHttpResponse prepay(HttpServletRequest request, int payType) {
        UserDto user = userServiceApi.get(request.getParameter("utoken"));
        long orderId = Long.valueOf(request.getParameter("oid"));
        long userCouponId = Long.valueOf(request.getParameter("coupon"));

        Order order = orderService.get(orderId);
        Subject subject = subjectService.get(order.getSubjectId());
        if (!order.exists() || order.getUserId() != user.getId() || !subject.exists()) return MomiaHttpResponse.FAILED("订单数据有问题，无效的订单");

        if (!orderService.prepay(orderId)) return MomiaHttpResponse.FAILED;

        PrepayParam prepayParam = extractPrepayParam(request, order, subject, payType);
        PaymentGateway gateway = PaymentGatewayFactory.create(payType);

        if (userCouponId > 0) {
            UserCoupon userCoupon = couponService.get(userCouponId);
            if (!userCoupon.exists() || userCoupon.isExpired() || userCoupon.isUsed()) return MomiaHttpResponse.FAILED("无效的红包/优惠券");
            if (!couponService.preUseCoupon(order.getId(), userCouponId)) return MomiaHttpResponse.FAILED;
            prepayParam.setTotalFee(couponService.calcTotalFee(prepayParam.getTotalFee(), userCoupon));
        }

        PrepayResult prepayResult = gateway.prepay(prepayParam);

        if (!prepayResult.isSuccessful()) return MomiaHttpResponse.FAILED;
        return MomiaHttpResponse.SUCCESS(prepayResult);
    }

    private PrepayParam extractPrepayParam(HttpServletRequest request, Order order, Subject subject, int payType) {
        PrepayParam prepayParam = new PrepayParam();

        prepayParam.setClientType(extractClientType(request, payType));
        prepayParam.setOrderId(order.getId());
        prepayParam.setProductId(subject.getId());
        prepayParam.setProductTitle(subject.getTitle());
        prepayParam.setProductUrl(Configuration.getString("Wap.ProductUrl") + order.getSubjectId());
        prepayParam.setPaymentResultUrl(Configuration.getString("Wap.PaymentResultUrl") + order.getId());

        switch (payType) {
            case PayType.ALIPAY:
                prepayParam.setTotalFee(order.getTotalFee());
                break;
            case PayType.WEIXIN:
                prepayParam.setTotalFee(new BigDecimal(order.getTotalFee().multiply(new BigDecimal(100)).intValue()));
                break;
            default: throw new MomiaFailedException("无效的支付类型: " + payType);
        }

        prepayParam.addAll(extractParams(request));
        prepayParam.add("userIp", RequestUtil.getRemoteIp(request));

        return prepayParam;
    }<commit_msg>fix bug<commit_after>package cn.momia.service.course.web.ctrl;

import cn.momia.api.course.dto.PaymentDto;
import cn.momia.api.user.UserServiceApi;
import cn.momia.api.user.dto.UserDto;
import cn.momia.common.api.exception.MomiaFailedException;
import cn.momia.common.api.http.MomiaHttpResponse;
import cn.momia.common.client.ClientType;
import cn.momia.common.deal.gateway.CallbackParam;
import cn.momia.common.deal.gateway.PayType;
import cn.momia.common.deal.gateway.PaymentGateway;
import cn.momia.common.deal.gateway.PrepayParam;
import cn.momia.common.deal.gateway.PrepayResult;
import cn.momia.common.deal.gateway.factory.CallbackParamFactory;
import cn.momia.common.deal.gateway.factory.PaymentGatewayFactory;
import cn.momia.common.webapp.config.Configuration;
import cn.momia.common.webapp.ctrl.BaseController;
import cn.momia.common.webapp.util.RequestUtil;
import cn.momia.service.course.subject.Subject;
import cn.momia.service.course.subject.SubjectService;
import cn.momia.service.course.subject.coupon.CouponService;
import cn.momia.service.course.subject.coupon.UserCoupon;
import cn.momia.service.course.subject.order.Order;
import cn.momia.service.course.subject.order.OrderService;
import cn.momia.service.course.subject.order.Payment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@RestController
@RequestMapping(value = "/payment")
public class PaymentController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PaymentController.class);

    @Autowired private SubjectService subjectService;
    @Autowired private OrderService orderService;
    @Autowired private CouponService couponService;
    @Autowired private UserServiceApi userServiceApi;

    @RequestMapping(value = "/prepay/alipay", method = RequestMethod.POST)
    public MomiaHttpResponse prepayAlipay(HttpServletRequest request) {
        return prepay(request, PayType.ALIPAY);
    }

    private MomiaHttpResponse prepay(HttpServletRequest request, int payType) {
        UserDto user = userServiceApi.get(request.getParameter("utoken"));
        long orderId = Long.valueOf(request.getParameter("oid"));
        long userCouponId = Long.valueOf(request.getParameter("coupon"));

        Order order = orderService.get(orderId);
        Subject subject = subjectService.get(order.getSubjectId());
        if (!order.exists() || order.getUserId() != user.getId() || !subject.exists()) return MomiaHttpResponse.FAILED("订单数据有问题，无效的订单");

        if (!orderService.prepay(orderId)) return MomiaHttpResponse.FAILED;

        BigDecimal totalFee = order.getTotalFee();
        if (userCouponId > 0) {
            UserCoupon userCoupon = couponService.get(userCouponId);
            if (!userCoupon.exists() || userCoupon.isExpired() || userCoupon.isUsed()) return MomiaHttpResponse.FAILED("无效的红包/优惠券");
            if (!couponService.preUseCoupon(order.getId(), userCouponId)) return MomiaHttpResponse.FAILED;
            totalFee = couponService.calcTotalFee(totalFee, userCoupon);
        }

        PrepayParam prepayParam = extractPrepayParam(request, order, totalFee, subject, payType);
        PaymentGateway gateway = PaymentGatewayFactory.create(payType);
        PrepayResult prepayResult = gateway.prepay(prepayParam);

        if (!prepayResult.isSuccessful()) return MomiaHttpResponse.FAILED;
        return MomiaHttpResponse.SUCCESS(prepayResult);
    }

    private PrepayParam extractPrepayParam(HttpServletRequest request, Order order, BigDecimal totalFee, Subject subject, int payType) {
        PrepayParam prepayParam = new PrepayParam();

        prepayParam.setClientType(extractClientType(request, payType));
        prepayParam.setOrderId(order.getId());
        prepayParam.setProductId(subject.getId());
        prepayParam.setProductTitle(subject.getTitle());
        prepayParam.setProductUrl(Configuration.getString("Wap.ProductUrl") + order.getSubjectId());
        prepayParam.setPaymentResultUrl(Configuration.getString("Wap.PaymentResultUrl") + order.getId());

        switch (payType) {
            case PayType.ALIPAY:
                prepayParam.setTotalFee(totalFee);
                break;
            case PayType.WEIXIN:
                prepayParam.setTotalFee(totalFee.multiply(new BigDecimal(100)));
                break;
            default: throw new MomiaFailedException("无效的支付类型: " + payType);
        }

        prepayParam.addAll(extractParams(request));
        prepayParam.add("userIp", RequestUtil.getRemoteIp(request));

        return prepayParam;
    }
{'fraction_non_alphanumeric': 0.10730570473198905, 'fraction_numerical': 0.0012159286655182896, 'mean_word_length': 4.390496996176952, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '684456', 'n_tokens_mistral': 3009, 'n_tokens_neox': 2992, 'n_words': 449}