package com.bonus.canteen.core.auth.mobile.alipay.service.impl;
import com.alipay.api.response.AlipayUserAgreementQueryResponse;
import com.bonus.canteen.core.allocation.api.AllocMetadataApi;
import com.bonus.canteen.core.auth.mobile.alipay.dto.AliPayUserAgreementQueryDTO;
import com.bonus.canteen.core.auth.mobile.alipay.service.AliPayService;
import com.bonus.canteen.core.common.thirdutils.alipay.AliPayUtil;
import com.bonus.canteen.core.common.thirdutils.alipay.model.AlipayReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class AliPayServiceImpl implements AliPayService {
   @Resource
   private AliPayUtil aliPayUtil;
   @Resource
   @Lazy
   private AllocMetadataApi allocMetadataApi;

   @Override
   public String paymentCodeDecod(String authCode) {
      AlipayReq alipayReq = this.getAlipayReq();
      return this.aliPayUtil.paymentCodeDecod(authCode, alipayReq);
   }

   private AlipayReq getAlipayReq() {
      return this.allocMetadataApi.checkAndCreateAlipayReq();
   }

   public String userAgreementQueryselect(AliPayUserAgreementQueryDTO aliPayUserAgreementQueryDTO) {
      AlipayReq alipayReq = this.getAlipayReq();
      AlipayUserAgreementQueryResponse response = this.aliPayUtil.userAgreementQuery(aliPayUserAgreementQueryDTO, alipayReq);
      return "NORMAL".equals(response.getStatus()) ? response.getAgreementNo() : null;
   }

}
