| | |
| | | package com.stylefeng.guns.modular.api; |
| | | |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.JsonArray; |
| | | import com.google.gson.JsonElement; |
| | | import com.google.gson.JsonObject; |
| | | import com.intuit.ipp.core.IEntity; |
| | | import com.intuit.ipp.data.*; |
| | | import com.intuit.ipp.exception.FMSException; |
| | | import com.intuit.ipp.services.DataService; |
| | | import com.intuit.ipp.services.QueryResult; |
| | | import com.intuit.oauth2.data.BearerTokenResponse; |
| | | import com.intuit.oauth2.exception.OAuthException; |
| | | import com.stylefeng.guns.core.base.controller.BaseController; |
| | | import com.stylefeng.guns.core.base.tips.ErrorTip; |
| | | import com.stylefeng.guns.core.common.constant.factory.ConstantFactory; |
| | | import com.stylefeng.guns.core.common.exception.BizExceptionEnum; |
| | | import com.stylefeng.guns.core.exception.GunsException; |
| | | import com.stylefeng.guns.core.shiro.ShiroUser; |
| | | import com.stylefeng.guns.core.util.Convert; |
| | | import com.stylefeng.guns.core.util.JwtTokenUtil; |
| | |
| | | import com.stylefeng.guns.modular.system.model.User; |
| | | import com.stylefeng.guns.modular.system.model.UserInfo; |
| | | import com.stylefeng.guns.modular.system.utils.EmailUtil; |
| | | import com.stylefeng.guns.modular.system.utils.InvoicesDataUploadUtil; |
| | | import com.stylefeng.guns.modular.system.utils.RedisUtil; |
| | | import com.stylefeng.guns.modular.system.utils.tips.SuccessTip; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiImplicitParam; |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.RandomStringUtils; |
| | | import org.json.JSONObject; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | @RestController |
| | | @Api(tags = "登录") |
| | | @RequestMapping("/gunsApi") |
| | | @Slf4j |
| | | public class ApiController extends BaseController { |
| | | |
| | | @Autowired |
| | |
| | | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | private final String INVOICE_DATA = "CARINVOICE:"; |
| | | |
| | | private static final String ACCOUNT_QUERY = "select * from Account where AccountType='%s' maxresults 1"; |
| | | |
| | | /** |
| | | * api登录接口,通过账号密码获取token |
| | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | @ApiOperation(value = "redirectURL接口",notes="redirectURL接口") |
| | | @GetMapping(value = "/oauth2redirectOfCar") |
| | | @ResponseBody |
| | | @Transactional |
| | | public String callbackIntuit(@RequestParam("code") String authCode, |
| | | @RequestParam("state") String state, |
| | | @RequestParam(value = "realmId", required = false) String realmId, HttpSession session) throws OAuthException { |
| | | String accessToken = ""; |
| | | if (com.stylefeng.guns.core.util.ToolUtil.isEmpty(realmId)) { |
| | | return new JSONObject().put("response","No realm ID. QBO calls only work if the accounting scope was passed!").toString(); |
| | | } |
| | | try { |
| | | session.setAttribute("realmId", realmId); |
| | | session.setAttribute("auth_code", authCode); |
| | | |
| | | // OAuth2PlatformClient client = factory.getOAuth2PlatformClient(); |
| | | // String redirectUri = factory.getRedirectUrl(); |
| | | |
| | | BearerTokenResponse bearerTokenResponse = InvoicesDataUploadUtil.callBackFromOAuth(authCode); |
| | | accessToken = bearerTokenResponse.getAccessToken(); |
| | | session.setAttribute("access_token", bearerTokenResponse.getAccessToken()); |
| | | session.setAttribute("refresh_token", bearerTokenResponse.getRefreshToken()); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | |
| | | String value = redisUtil.getValue(INVOICE_DATA); |
| | | log.info("value:{}",value); |
| | | if (ToolUtil.isEmpty(value)){ |
| | | return "ERROR"; |
| | | } |
| | | Gson gson = new Gson(); |
| | | JsonArray jsonArray = gson.fromJson(value, JsonArray.class); |
| | | for (JsonElement jsonElement : jsonArray) { |
| | | JsonObject asJsonObject = jsonElement.getAsJsonObject(); |
| | | String name = asJsonObject.get("name").getAsString(); |
| | | String amount = asJsonObject.get("amount").getAsString(); |
| | | String unitPrice = asJsonObject.get("unitPrice").getAsString(); |
| | | DataService service = InvoicesDataUploadUtil.getDataService(realmId, accessToken); |
| | | Customer customer = getCustomerWithAllFields(name,"testconceptsample@mailinator.com"); |
| | | Customer savedCustomer = service.add(customer); |
| | | Item item = getItemFields(service, unitPrice == null || unitPrice == "ABC Corporations" ? "0" : unitPrice ); |
| | | Item savedItem = service.add(item); |
| | | Invoice invoice = getInvoiceFields(savedCustomer, savedItem, amount == null || amount == "" ? "0" : amount); |
| | | Invoice savedInvoice = service.add(invoice); |
| | | service.sendEmail(savedInvoice, customer.getPrimaryEmailAddr().getAddress()); |
| | | Payment payment = getPaymentFields(savedCustomer, savedInvoice); |
| | | Payment savedPayment = service.add(payment); |
| | | createResponse(savedPayment); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new GunsException(BizExceptionEnum.CALLBACK_ERROR); |
| | | } |
| | | return "SUCCESS"; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | private Customer getCustomerWithAllFields(String companyName,String mailAddr) { |
| | | Customer customer = new Customer(); |
| | | customer.setDisplayName(RandomStringUtils.randomAlphanumeric(6)); |
| | | customer.setCompanyName(companyName); |
| | | |
| | | EmailAddress emailAddr = new EmailAddress(); |
| | | emailAddr.setAddress(mailAddr); |
| | | customer.setPrimaryEmailAddr(emailAddr); |
| | | |
| | | return customer; |
| | | } |
| | | |
| | | private Item getItemFields(DataService service,String unitPrice) throws FMSException { |
| | | |
| | | Item item = new Item(); |
| | | item.setName("Item" + RandomStringUtils.randomAlphanumeric(5)); |
| | | item.setTaxable(false); |
| | | item.setUnitPrice(unitPrice == null || unitPrice == "" ? BigDecimal.ZERO :new BigDecimal(unitPrice)); |
| | | item.setType(ItemTypeEnum.SERVICE); |
| | | |
| | | Account incomeAccount = getIncomeBankAccount(service); |
| | | item.setIncomeAccountRef(createRef(incomeAccount)); |
| | | |
| | | return item; |
| | | } |
| | | |
| | | private Account getIncomeBankAccount(DataService service) throws FMSException { |
| | | QueryResult queryResult = service.executeQuery(String.format(ACCOUNT_QUERY, AccountTypeEnum.INCOME.value())); |
| | | List<? extends IEntity> entities = queryResult.getEntities(); |
| | | if(!entities.isEmpty()) { |
| | | return (Account)entities.get(0); |
| | | } |
| | | return createIncomeBankAccount(service); |
| | | } |
| | | |
| | | private ReferenceType createRef(IntuitEntity entity) { |
| | | ReferenceType referenceType = new ReferenceType(); |
| | | referenceType.setValue(entity.getId()); |
| | | return referenceType; |
| | | } |
| | | private Account createIncomeBankAccount(DataService service) throws FMSException { |
| | | Account account = new Account(); |
| | | account.setName("Incom" + RandomStringUtils.randomAlphabetic(5)); |
| | | account.setAccountType(AccountTypeEnum.INCOME); |
| | | |
| | | return service.add(account); |
| | | } |
| | | |
| | | private Invoice getInvoiceFields(Customer customer, Item item,String amount) { |
| | | |
| | | Invoice invoice = new Invoice(); |
| | | invoice.setCustomerRef(createRef(customer)); |
| | | BigDecimal balance = invoice.getBalance(); |
| | | log.info("balance:{}",balance); |
| | | |
| | | List<Line> invLine = new ArrayList<Line>(); |
| | | Line line = new Line(); |
| | | line.setAmount(amount == null || amount == "" ? BigDecimal.ZERO : new BigDecimal(amount)); |
| | | line.setDetailType(LineDetailTypeEnum.SALES_ITEM_LINE_DETAIL); |
| | | |
| | | SalesItemLineDetail silDetails = new SalesItemLineDetail(); |
| | | silDetails.setItemRef(createRef(item)); |
| | | |
| | | line.setSalesItemLineDetail(silDetails); |
| | | invLine.add(line); |
| | | invoice.setLine(invLine); |
| | | |
| | | return invoice; |
| | | } |
| | | |
| | | private Payment getPaymentFields(Customer customer, Invoice invoice) { |
| | | |
| | | Payment payment = new Payment(); |
| | | payment.setCustomerRef(createRef(customer)); |
| | | |
| | | payment.setTotalAmt(invoice.getTotalAmt()); |
| | | |
| | | List<LinkedTxn> linkedTxnList = new ArrayList<LinkedTxn>(); |
| | | LinkedTxn linkedTxn = new LinkedTxn(); |
| | | linkedTxn.setTxnId(invoice.getId()); |
| | | linkedTxn.setTxnType(TxnTypeEnum.INVOICE.value()); |
| | | linkedTxnList.add(linkedTxn); |
| | | |
| | | Line line1 = new Line(); |
| | | line1.setAmount(invoice.getTotalAmt()); |
| | | line1.setLinkedTxn(linkedTxnList); |
| | | |
| | | List<Line> lineList = new ArrayList<Line>(); |
| | | lineList.add(line1); |
| | | payment.setLine(lineList); |
| | | |
| | | return payment; |
| | | } |
| | | |
| | | |
| | | private String createResponse(Object entity) { |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | String jsonInString; |
| | | try { |
| | | jsonInString = mapper.writeValueAsString(entity); |
| | | } catch (Exception e) { |
| | | return createErrorResponse(e); |
| | | } |
| | | return jsonInString; |
| | | } |
| | | |
| | | private String createErrorResponse(Exception e) { |
| | | log.error("Exception while calling QBO ", e); |
| | | return new JSONObject().put("response","Failed").toString(); |
| | | } |
| | | |
| | | } |