@Nullable public static Address fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } String city = optString(jsonObject, FIELD_CITY); String country = optString(jsonObject, FIELD_COUNTRY); String line1 = optString(jsonObject, FIELD_LINE_1); String line2 = optString(jsonObject, FIELD_LINE_2); String postalCode = optString(jsonObject, FIELD_POSTAL_CODE); String state = optString(jsonObject, FIELD_STATE); return new Address(city, country, line1, line2, postalCode, state); }
@Nullable public static SourceRedirect fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } String returnUrl = optString(jsonObject, FIELD_RETURN_URL); @Status String status = asStatus(optString(jsonObject, FIELD_STATUS)); String url = optString(jsonObject, FIELD_URL); return new SourceRedirect(returnUrl, status, url); }
@Nullable public static BankAccount fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } return new BankAccount( StripeJsonUtils.optString(jsonObject, FIELD_ACCOUNT_HOLDER_NAME), asBankAccountType( StripeJsonUtils.optString(jsonObject, FIELD_ACCOUNT_HOLDER_TYPE)), StripeJsonUtils.optString(jsonObject, FIELD_BANK_NAME), StripeJsonUtils.optCountryCode(jsonObject, FIELD_COUNTRY), StripeJsonUtils.optCurrency(jsonObject, FIELD_CURRENCY), StripeJsonUtils.optString(jsonObject, FIELD_FINGERPRINT), StripeJsonUtils.optString(jsonObject, FIELD_LAST4), StripeJsonUtils.optString(jsonObject, FIELD_ROUTING_NUMBER)); } }
@Nullable public static ShippingInformation fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } ShippingInformation shippingInformation = new ShippingInformation(); shippingInformation.mName = optString(jsonObject, FIELD_NAME); shippingInformation.mPhone = optString(jsonObject, FIELD_PHONE); shippingInformation.mAddress = Address.fromJson(jsonObject.optJSONObject(FIELD_ADDRESS)); return shippingInformation; }
@Nullable public static SourceCodeVerification fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } return new SourceCodeVerification( jsonObject.optInt(FIELD_ATTEMPTS_REMAINING, INVALID_ATTEMPTS_REMAINING), asStatus(optString(jsonObject, FIELD_STATUS))); }
address = Address.fromJson(addressObject); email = optString(jsonObject, FIELD_EMAIL); name = optString(jsonObject, FIELD_NAME); phone = optString(jsonObject, FIELD_PHONE); verifiedAddress = Address.fromJson(vAddressObject); verifiedEmail = optString(jsonObject, FIELD_VERIFIED_EMAIL); verifiedName = optString(jsonObject, FIELD_VERIFIED_NAME); verifiedPhone = optString(jsonObject, FIELD_VERIFIED_PHONE);
@Nullable public static SourceSepaDebitData fromJson(JSONObject jsonObject) { if (jsonObject == null) { return null; } SourceSepaDebitData sepaData = new SourceSepaDebitData(); sepaData.setBankCode(optString(jsonObject, FIELD_BANK_CODE)) .setBranchCode(optString(jsonObject, FIELD_BRANCH_CODE)) .setCountry(optString(jsonObject, FIELD_COUNTRY)) .setFingerPrint(optString(jsonObject, FIELD_FINGERPRINT)) .setLast4(optString(jsonObject, FIELD_LAST4)) .setMandateReference(optString(jsonObject, FIELD_MANDATE_REFERENCE)) .setMandateUrl(optString(jsonObject, FIELD_MANDATE_URL)); Map<String, Object> nonStandardFields = jsonObjectToMapWithoutKeys(jsonObject, sepaData.mStandardFields); if (nonStandardFields != null) { sepaData.setAdditionalFields(nonStandardFields); } return sepaData; }
return null; String id = optString(jsonObject, FIELD_ID); String objectType = optString(jsonObject, FIELD_OBJECT); JSONArray allowedSourceTypesJSONArray = jsonObject.optJSONArray(FIELD_ALLOWED_SOURCE_TYPES); List<String> allowedSourceTypes = new ArrayList<>(); String captureMethod = optString(jsonObject, FIELD_CAPTURE_METHOD); String clientSecret = optString(jsonObject, FIELD_CLIENT_SECRET); String confirmationMethod = optString(jsonObject, FIELD_CONFIRMATION_METHOD); Long created = optLong(jsonObject, FIELD_CREATED); String currency = optCurrency(jsonObject, FIELD_CURRENCY); String description = optString(jsonObject, FIELD_DESCRIPTION); Boolean livemode = optBoolean(jsonObject, FIELD_LIVEMODE); String receiptEmail = optString(jsonObject, FIELD_RECEIPT_EMAIL); String status = optString(jsonObject, FIELD_STATUS); Map<String, Object> nextSourceAction = optMap(jsonObject, FIELD_NEXT_SOURCE_ACTION); String source = optString(jsonObject, FIELD_SOURCE);
@Nullable public static Customer fromJson(JSONObject jsonObject) { String objectType = optString(jsonObject, FIELD_OBJECT); if (!VALUE_CUSTOMER.equals(objectType)) { return null; customer.mId = optString(jsonObject, FIELD_ID); customer.mDefaultSource = optString(jsonObject, FIELD_DEFAULT_SOURCE); customer.mShippingInformation = ShippingInformation.fromJson(jsonObject.optJSONObject(FIELD_SHIPPING)); JSONObject sources = jsonObject.optJSONObject(FIELD_SOURCES); if (sources != null && VALUE_LIST.equals(optString(sources, FIELD_OBJECT))) { customer.mHasMore = optBoolean(sources, FIELD_HAS_MORE); customer.mTotalCount = optInteger(sources, FIELD_TOTAL_COUNT); customer.mUrl = optString(sources, FIELD_URL); List<CustomerSource> sourceDataList = new ArrayList<>(); JSONArray dataArray = sources.optJSONArray(FIELD_DATA);
@Nullable public static CustomerSource fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } String objectString = optString(jsonObject, "object"); StripePaymentSource sourceObject = null; if (Card.VALUE_CARD.equals(objectString)) { sourceObject = Card.fromJson(jsonObject); } else if (Source.VALUE_SOURCE.equals(objectString)) { sourceObject = Source.fromJson(jsonObject); } if (sourceObject == null) { return null; } else { return new CustomerSource(sourceObject); } }
@Nullable public static SourceReceiver fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } String address = StripeJsonUtils.optString(jsonObject, FIELD_ADDRESS); return new SourceReceiver(address, jsonObject.optLong(FIELD_AMOUNT_CHARGED), jsonObject.optLong(FIELD_AMOUNT_RECEIVED), jsonObject.optLong(FIELD_AMOUNT_RETURNED)); } }
@Nullable static SourceCardData fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } SourceCardData cardData = new SourceCardData(); cardData.setAddressLine1Check(optString(jsonObject, FIELD_ADDRESS_LINE1_CHECK)) .setAddressZipCheck(optString(jsonObject, FIELD_ADDRESS_ZIP_CHECK)) .setBrand(Card.asCardBrand(optString(jsonObject, FIELD_BRAND))) .setCountry(optString(jsonObject, FIELD_COUNTRY)) .setCvcCheck(optString(jsonObject, FIELD_CVC_CHECK)) .setDynamicLast4(optString(jsonObject, FIELD_DYNAMIC_LAST4)) .setExpiryMonth(optInteger(jsonObject, FIELD_EXP_MONTH)) .setExpiryYear(optInteger(jsonObject, FIELD_EXP_YEAR)) .setFunding(Card.asFundingType(optString(jsonObject, FIELD_FUNDING))) .setLast4(optString(jsonObject, FIELD_LAST4)) .setThreeDSecureStatus(asThreeDSecureStatus(optString(jsonObject, FIELD_THREE_D_SECURE))) .setTokenizationMethod(optString(jsonObject, FIELD_TOKENIZATION_METHOD)); Map<String, Object> nonStandardFields = jsonObjectToMapWithoutKeys(jsonObject, cardData.mStandardFields); if (nonStandardFields != null) { cardData.setAdditionalFields(nonStandardFields); } return cardData; }
builder.addressCity(optString(jsonObject, FIELD_ADDRESS_CITY)); builder.addressLine1(optString(jsonObject, FIELD_ADDRESS_LINE1)); builder.addressLine1Check(optString(jsonObject, FIELD_ADDRESS_LINE1_CHECK)); builder.addressLine2(optString(jsonObject, FIELD_ADDRESS_LINE2)); builder.addressCountry(optString(jsonObject, FIELD_ADDRESS_COUNTRY)); builder.addressState(optString(jsonObject, FIELD_ADDRESS_STATE)); builder.addressZip(optString(jsonObject, FIELD_ADDRESS_ZIP)); builder.addressZipCheck(optString(jsonObject, FIELD_ADDRESS_ZIP_CHECK)); builder.brand(asCardBrand(optString(jsonObject, FIELD_BRAND))); builder.country(optCountryCode(jsonObject, FIELD_COUNTRY)); builder.customer(optString(jsonObject, FIELD_CUSTOMER)); builder.currency(optCurrency(jsonObject, FIELD_CURRENCY)); builder.cvcCheck(optString(jsonObject, FIELD_CVC_CHECK)); builder.funding(asFundingType(optString(jsonObject, FIELD_FUNDING))); builder.fingerprint(optString(jsonObject, FIELD_FINGERPRINT)); builder.id(optString(jsonObject, FIELD_ID)); builder.last4(optString(jsonObject, FIELD_LAST4)); builder.name(optString(jsonObject, FIELD_NAME)); builder.tokenizationMethod(optString(jsonObject, FIELD_TOKENIZATION_METHOD)); builder.metadata(optHash(jsonObject, FIELD_METADATA));
return null; final String tokenId = StripeJsonUtils.optString(jsonObject, FIELD_ID); final Long createdTimeStamp = StripeJsonUtils.optLong(jsonObject, FIELD_CREATED); final Boolean liveMode = StripeJsonUtils.optBoolean(jsonObject, FIELD_LIVEMODE); @TokenType final String tokenType = asTokenType(StripeJsonUtils.optString(jsonObject, FIELD_TYPE)); final Boolean used = StripeJsonUtils.optBoolean(jsonObject, FIELD_USED);
String id = optString(jsonObject, FIELD_ID); Long amount = optLong(jsonObject, FIELD_AMOUNT); String clientSecret = optString(jsonObject, FIELD_CLIENT_SECRET); SourceCodeVerification codeVerification = optStripeJsonModel( jsonObject, SourceCodeVerification.class); Long created = optLong(jsonObject, FIELD_CREATED); String currency = optString(jsonObject, FIELD_CURRENCY); @SourceFlow String flow = asSourceFlow(optString(jsonObject, FIELD_FLOW)); Boolean liveMode = jsonObject.optBoolean(FIELD_LIVEMODE); Map<String, String> metadata = FIELD_REDIRECT, SourceRedirect.class); @SourceStatus String status = asSourceStatus(optString(jsonObject, FIELD_STATUS)); String typeRaw = optString(jsonObject, FIELD_TYPE); if (typeRaw == null) { : null; @Usage String usage = asUsage(optString(jsonObject, FIELD_USAGE));
@Test public void optString_whenFieldPresent_findsAndReturnsField() { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("key", "value"); assertEquals("value", StripeJsonUtils.optString(jsonObject, "key")); } catch (JSONException jex) { fail("No exception expected"); } }
@Test public void optString_whenFieldContainsRawNull_returnsNull() { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("key", "null"); assertNull(StripeJsonUtils.optString(jsonObject, "key")); } catch (JSONException jex) { fail("No exception expected"); } }
@Test public void optString_whenFieldNotPresent_returnsNull() { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("key", "value"); Object ob = StripeJsonUtils.optString(jsonObject, "nokeyshere"); assertNull(ob); } catch (JSONException jex) { fail("No exception expected"); } }