@Override public ShippingInformation createFromParcel(Parcel source) { return new ShippingInformation(source); }
@Override public void onReceive(Context context, Intent intent) { ShippingInformation shippingInformation = intent.getParcelableExtra(EXTRA_SHIPPING_INFO_DATA); Intent shippingInfoProcessedIntent = new Intent(EVENT_SHIPPING_INFO_PROCESSED); if (shippingInformation.getAddress() == null || !shippingInformation.getAddress().getCountry().equals(Locale.US.getCountry())) { shippingInfoProcessedIntent.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, false); } else { ArrayList<ShippingMethod> shippingMethods = createSampleShippingMethods(); shippingInfoProcessedIntent.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, true); shippingInfoProcessedIntent.putParcelableArrayListExtra(EXTRA_VALID_SHIPPING_METHODS, shippingMethods); shippingInfoProcessedIntent.putExtra(EXTRA_DEFAULT_SHIPPING_METHOD, shippingMethods.get(1)); } LocalBroadcastManager.getInstance(PaymentSessionActivity.this).sendBroadcast(shippingInfoProcessedIntent); } };
private Map<String, Object> createParams(long price, String sourceId, String customerId, ShippingInformation shippingInformation){ Map<String, Object> params = new HashMap<>(); params.put("amount", Long.toString(price)); params.put("source", sourceId); params.put("customer_id", customerId); params.put("shipping", shippingInformation.toMap()); return params; }
/** * @param shippingInformation shippingInformation to populated into the widget input fields. */ public void populateShippingInfo(@Nullable ShippingInformation shippingInformation) { if (shippingInformation == null) { return; } Address address = shippingInformation.getAddress(); if (address != null) { mCityEditText.setText(address.getCity()); if (address.getCountry() != null && !address.getCountry().isEmpty()) { mCountryAutoCompleteTextView.setCountrySelected(address.getCountry()); } mAddressEditText.setText(address.getLine1()); mAddressEditText2.setText(address.getLine2()); mPostalCodeEditText.setText(address.getPostalCode()); mStateEditText.setText(address.getState()); } mNameEditText.setText(shippingInformation.getName()); mPhoneNumberEditText.setText(shippingInformation.getPhone()); }
@Override public int hashCode() { int result = (int) (mCartTotal ^ (mCartTotal >>> 32)); result = 31 * result + (mIsPaymentReadyToCharge ? 1 : 0); result = 31 * result + mSelectedPaymentMethodId.hashCode(); result = 31 * result + (int) (mShippingTotal ^ (mShippingTotal >>> 32)); result = 31 * result + mPaymentResult.hashCode(); result = 31 * result + (mShippingInformation != null ? mShippingInformation.hashCode() : 0); result = 31 * result + (mShippingMethod != null ? mShippingMethod.hashCode() : 0); return result; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PaymentSessionData that = (PaymentSessionData) o; if (mCartTotal != that.mCartTotal) return false; if (mIsPaymentReadyToCharge != that.mIsPaymentReadyToCharge) return false; if (mShippingTotal != that.mShippingTotal) return false; if (!mSelectedPaymentMethodId.equals(that.mSelectedPaymentMethodId)) return false; if (!mPaymentResult.equals(that.mPaymentResult)) return false; if (mShippingInformation != null ? !mShippingInformation.equals(that.mShippingInformation) : that.mShippingInformation != null) { return false; } return mShippingMethod != null ? mShippingMethod.equals(that.mShippingMethod) : that.mShippingMethod == null; }
@NonNull @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); putStringIfNotNull(jsonObject, FIELD_NAME, mName); putStringIfNotNull(jsonObject, FIELD_PHONE, mPhone); putStripeJsonModelIfNotNull(jsonObject, FIELD_ADDRESS, mAddress); return jsonObject; }
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))) {
@Override public int hashCode() { int result = getHiddenShippingInfoFields().hashCode(); result = 31 * result + getOptionalShippingInfoFields().hashCode(); result = 31 * result + mShippingInformation.hashCode(); result = 31 * result + (isShippingInfoRequired() ? 1 : 0); result = 31 * result + (isShippingMethodRequired() ? 1 : 0); return result; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PaymentSessionConfig that = (PaymentSessionConfig) o; if (isShippingInfoRequired() != that.isShippingInfoRequired()) return false; if (isShippingMethodRequired() != that.isShippingMethodRequired()) return false; if (!getHiddenShippingInfoFields().equals(that.getHiddenShippingInfoFields())) return false; if (!getOptionalShippingInfoFields().equals(that.getOptionalShippingInfoFields())) { return false; } return getPrepopulatedShippingInfo().equals(that.getPrepopulatedShippingInfo()); }
@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; }
CardException { final Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("shipping", shippingInformation.toMap());
private ArrayList<ShippingMethod> getValidShippingMethods(ShippingInformation shippingInformation) { ArrayList<ShippingMethod> shippingMethods = new ArrayList<>(); shippingMethods.add(new ShippingMethod("UPS Ground", "ups-ground", "Arrives in 3-5 days", 0, "USD")); shippingMethods.add(new ShippingMethod("FedEx", "fedex", "Arrives tomorrow", 599, "USD")); if (shippingInformation.getAddress() != null && shippingInformation.getAddress().getPostalCode().equals("94110")) { shippingMethods.add(new ShippingMethod("1 Hour Courier", "courier", "Arrives in the next hour", 1099, "USD")); } return shippingMethods; }
public ShippingInformation getShippingInformation() { if (!validateAllFields()) { return null; } Address address = new Address.Builder().setCity(mCityEditText.getText().toString()) .setCountry(mCountryAutoCompleteTextView.getSelectedCountryCode()).setLine1 (mAddressEditText.getText().toString()).setLine2 (mAddressEditText2.getText().toString()).setPostalCode (mPostalCodeEditText.getText().toString()).setState(mStateEditText .getText().toString()).build(); ShippingInformation shippingInformation = new ShippingInformation(address, mNameEditText .getText().toString(), mPhoneNumberEditText.getText().toString()); return shippingInformation; }
@Test public void getShippingInfo_whenShippingInfoValid_returnsExpected() { mStateEditText.setText("CA"); mCityEditText.setText("San Francisco"); mAddressLine1EditText.setText("185 Berry St"); mAddressLine2EditText.setText("10th Floor"); mNameEditText.setText("Fake Name"); mPhoneEditText.setText("(123) 456 - 7890"); mPostalEditText.setText("12345"); mCountryAutoCompleteTextView.updateUIForCountryEntered(Locale.US.getDisplayCountry()); ShippingInformation inputShippingInfo = mShippingInfoWidget.getShippingInformation(); assertEquals(inputShippingInfo.toMap(), mShippingInfo.toMap()); }
@Override public void onReceive(Context context, Intent intent) { ShippingInformation shippingInformation = intent.getParcelableExtra(EXTRA_SHIPPING_INFO_DATA); Intent shippingInfoProcessedIntent = new Intent(EVENT_SHIPPING_INFO_PROCESSED); if (shippingInformation.getAddress() == null || !shippingInformation.getAddress().getCountry().equals(Locale.US.getCountry())) { shippingInfoProcessedIntent.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, false); } else { ArrayList<ShippingMethod> shippingMethods = getValidShippingMethods(shippingInformation); shippingInfoProcessedIntent.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, true); shippingInfoProcessedIntent.putParcelableArrayListExtra(EXTRA_VALID_SHIPPING_METHODS, shippingMethods); shippingInfoProcessedIntent.putExtra(EXTRA_DEFAULT_SHIPPING_METHOD, shippingMethods.get(0)); } LocalBroadcastManager.getInstance(PaymentActivity.this).sendBroadcast(shippingInfoProcessedIntent); } };
private ShippingInformation getExampleShippingInfo() { Address address = new Address.Builder() .setCity("San Francisco") .setCountry("US") .setLine1("123 Market St") .setLine2("#345") .setPostalCode("94107") .setState("CA") .build(); return new ShippingInformation(address, "Fake Name", "(555) 555-5555"); } }
@NonNull private ShippingInformation getExampleShippingInfo() { Address address = new Address.Builder() .setCity("San Francisco") .setCountry("US") .setLine1("123 Market St") .setLine2("#345") .setPostalCode("94107") .setState("CA") .build(); return new ShippingInformation(address, "Fake Name", "(555) 555-5555"); }
@Test public void writeToParcel_withoutNulls_readsFromParcelCorrectly() { PaymentSessionData data = new PaymentSessionData(); data.setCartTotal(100L); data.setShippingTotal(150L); data.setSelectedPaymentMethodId("abc123"); data.setPaymentReadyToCharge(false); data.setShippingInformation(new ShippingInformation()); data.setShippingMethod(new ShippingMethod("UPS", "SuperFast", 10000L, "usd")); Parcel parcel = Parcel.obtain(); data.writeToParcel(parcel, 0); parcel.setDataPosition(0); PaymentSessionData parceledData = PaymentSessionData.CREATOR.createFromParcel(parcel); assertEquals(data, parceledData); } }
.setPostalCode("12345") .build(); mShippingInfo = new ShippingInformation(mAddress, "Fake Name", "(123) 456 - 7890");