package ink.wgink.module.wechat.pay.service.v3.order.impl;

import ink.wgink.exceptions.ParamsException;
import ink.wgink.module.wechat.pay.enums.PayAuthorizationTypeEnum;
import ink.wgink.module.wechat.pay.pojo.v3.order.OrderSearch;
import ink.wgink.module.wechat.pay.remote.v3.IOrderPayRemoteService;
import ink.wgink.module.wechat.pay.service.BasePayService;
import ink.wgink.module.wechat.pay.service.v3.order.IOrderSearchService;
import ink.wgink.properties.wechat.pay.v3.PayProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @ClassName: OrderSearchServiceImpl
 * @Description: 订单查询
 * @Author: wanggeng
 * @Date: 2021/8/27 10:51 上午
 * @Version: 1.0
 */
@Service
public class OrderSearchServiceImpl extends BasePayService implements IOrderSearchService {

    @Autowired
    private PayProperties payProperties;
    @Autowired
    private IOrderPayRemoteService orderPayRemoteService;


    @Override
    public OrderSearch getOrder(String transactionId) throws Exception {
        if (StringUtils.isBlank(transactionId)) {
            throw new ParamsException("微信订单号参数不能为空");
        }
        String urlSuffix = "/v3/pay/transactions/id/" + transactionId + "?mchid=" + payProperties.getMchid();
        String serialNumber = getSerialNumber();
        String authorization = getAuthorization(RequestMethod.GET, urlSuffix, serialNumber, payProperties, null, PayAuthorizationTypeEnum.WECHATPAY2_SHA256_RSA2048);
        return orderPayRemoteService.getOrder(getPayBaseUrl(), transactionId, authorization, serialNumber, payProperties.getMchid());
    }

    @Override
    public OrderSearch getBusinessOrder(String businessOrderId) throws Exception {
        if (StringUtils.isBlank(businessOrderId)) {
            throw new ParamsException("商家订单号参数不能为空");
        }
        String urlSuffix = "/v3/pay/transactions/out-trade-no/" + businessOrderId + "?mchid=" + payProperties.getMchid();
        String serialNumber = getSerialNumber();
        String authorization = getAuthorization(RequestMethod.GET, urlSuffix, serialNumber, payProperties, null, PayAuthorizationTypeEnum.WECHATPAY2_SHA256_RSA2048);
        return orderPayRemoteService.getBusinessOrder(getPayBaseUrl(), businessOrderId, authorization, serialNumber, payProperties.getMchid());
    }

}
