|
| 1 | +package com.ke.bella.openapi.protocol.ocr.bankcard; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | + |
| 6 | +import com.ke.bella.openapi.protocol.ocr.KeOcrProperty; |
| 7 | +import com.ke.bella.openapi.protocol.ocr.OcrRequest; |
| 8 | +import com.ke.bella.openapi.protocol.ocr.provider.ke.KeOcrHelper; |
| 9 | +import com.ke.bella.openapi.protocol.ocr.provider.ke.KeRequest; |
| 10 | +import com.ke.bella.openapi.protocol.ocr.provider.ke.KeResponse; |
| 11 | +import com.ke.bella.openapi.utils.HttpUtils; |
| 12 | +import com.ke.bella.openapi.utils.JacksonUtils; |
| 13 | + |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | +import okhttp3.MediaType; |
| 16 | +import okhttp3.Request; |
| 17 | +import okhttp3.RequestBody; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +@Component("keBankcard") |
| 21 | +public class KeAdaptor implements BankcardAdaptor<KeOcrProperty> { |
| 22 | + private static final String KEY_CARD_NUMBER = "KA_HAO"; |
| 23 | + private static final String KEY_BANK_NAME = "YIN_HANG_MING_CHENG"; |
| 24 | + private static final String KEY_VALID_DATE = "YOU_XIAO_QI_JIE_ZHI_RI_QI"; |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private KeOcrHelper keOcrHelper; |
| 28 | + |
| 29 | + @Override |
| 30 | + public String getDescription() { |
| 31 | + return "Ke OCR银行卡识别协议"; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Class<KeOcrProperty> getPropertyClass() { |
| 36 | + return KeOcrProperty.class; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public OcrBankcardResponse bankcard(OcrRequest request, String url, KeOcrProperty property) { |
| 41 | + KeRequest keRequest = keOcrHelper.requestConvert(request); |
| 42 | + Request httpRequest = new Request.Builder() |
| 43 | + .url(url) |
| 44 | + .post(RequestBody.create(MediaType.parse("application/json"), JacksonUtils.toByte(keRequest))) |
| 45 | + .build(); |
| 46 | + clearLargeData(request, keRequest); |
| 47 | + KeResponse keResponse = HttpUtils.httpRequest(httpRequest, KeResponse.class); |
| 48 | + return responseConvert(keResponse, keRequest); |
| 49 | + } |
| 50 | + |
| 51 | + private OcrBankcardResponse responseConvert(KeResponse keResponse, KeRequest keRequest) { |
| 52 | + if(keOcrHelper.hasError(keResponse)) { |
| 53 | + return OcrBankcardResponse.builder() |
| 54 | + .error(keOcrHelper.buildError(keResponse)) |
| 55 | + .build(); |
| 56 | + } |
| 57 | + String requestId = keResponse.getRequestId(); |
| 58 | + OcrBankcardResponse.BankcardData data = OcrBankcardResponse.BankcardData.builder() |
| 59 | + .card_number(keOcrHelper.findValueByKey(keResponse, KEY_CARD_NUMBER)) |
| 60 | + .bank_name(keOcrHelper.findValueByKey(keResponse, KEY_BANK_NAME)) |
| 61 | + .valid_date(keOcrHelper.findValueByKey(keResponse, KEY_VALID_DATE)) |
| 62 | + .build(); |
| 63 | + return OcrBankcardResponse.builder() |
| 64 | + .request_id(requestId) |
| 65 | + .data(data) |
| 66 | + .build(); |
| 67 | + } |
| 68 | +} |
0 commit comments