This control is used for displaying the billing address or shipping address on a custom payment sheet based on My info menu in the Samsung Pay app.
When allocating the control, controlId and SheetItemType are needed to distinguish billing address from shipping address. The merchant app can set the following properties with AddressControl.
As of API level 1.3, addressee’s phoneNumber is added for custom payment sheet. Merchant app can retrieve the phone number using getPhoneNumber() method of CustomSheetPaymentInfo.Address.
As of API level 1.5, addressee’s email address is added. Merchant app can retrieve the email address using getEmail() method. Also Merchant app can set display option for shipping address on the payment sheet with setDisplayOption() method. For more information, refer to the Samsung Pay SDK API reference and sample code in Samsung Pay SDK.
// For billing address private AddressControl makeBillingAddressControl() { AddressControl billingAddressControl = new AddressControl(BILLING_ADDRESS_ID, SheetItemType.BILLING_ADDRESS); billingAddressControl.setAddressTitle("Billing Address [control]"); //This callback is received when Controls are updated. billingAddressControl.setSheetUpdatedListener(new SheetUpdatedListener() { @Override public void onResult(String updatedControlId, CustomSheet customSheet) { Log.d(TAG,"onResult billingAddressControl updatedControlId: " + updatedControlId); // Validate billing address and set errorCode if needed. AddressControl addressControl = (AddressControl)customSheet.getSheetControl(updatedControlId); CustomSheetPaymentInfo.Address billAddress = addressControl.getAddress(); int errorCode = validateBillingAddress(billAddress); Log.d(TAG, "onResult updateSheetBilling errorCode: " + errorCode); addressControl.setErrorCode(errorCode); customSheet.updateControl(addressControl); AmountBoxControl amountBoxControl = (AmountBoxControl) customSheet.getSheetControl(AMOUNT_CONTROL_ID); amountBoxControl.updateValue(PRODUCT_ITEM_ID, 1000); amountBoxControl.updateValue(PRODUCT_TAX_ID, 50); amountBoxControl.updateValue(PRODUCT_SHIPPING_ID, 10); amountBoxControl.updateValue(PRODUCT_FUEL_ID, 0, "Pending"); amountBoxControl.setAmountTotal(1060, AmountConstants.FORMAT_TOTAL_PRICE_ONLY); customSheet.updateControl(amountBoxControl); // Call updateSheet with AmountBoxControl. This is mandatory. try { paymentManager.updateSheet(customSheet); } catch (IllegalStateException | NullPointerException e) { e.printStackTrace(); } } }); return billingAddressControl; } // For Shipping address private AddressControl makeShippingAddressControl() { AddressControl shippingAddressControl = new AddressControl(SHIPPING_ADDRESS_ID, SheetItemType.SHIPPING_ADDRESS); shippingAddressControl.setAddressTitle("Shipping Address [control]"); CustomSheetPaymentInfo.Address shippingAddress = new CustomSheetPaymentInfo.Address.Builder() .setAddressee("name") .setAddressLine1("addLine1") .setAddressLine2("addLine2") .setCity("city") .setState("state") .setCountryCode("USA") .setPostalCode("zip") .setPhoneNumber("123-1234-1234") .setEmail("12345@samsung.com") .build(); shippingAddressControl.setAddress(shippingAddress); /* * Set address display option on custom payment sheet. * If displayOption is not set, then default addressControl is displayed on custom payment sheet. * The possible values are combination of below constants: * {DISPLAY_OPTION_ADDRESSEE} * {DISPLAY_OPTION_ADDRESS} * {DISPLAY_OPTION_PHONE_NUMBER} * {DISPLAY_OPTION_EMAIL} */ int displayOption_val = AddressConstants.DISPLAY_OPTION_ADDRESSEE; // Addressee is mandatory displayOption_val += AddressConstants.DISPLAY_OPTION_ADDRESS; displayOption_val += AddressConstants.DISPLAY_OPTION_PHONE_NUMBER; displayOption_val += AddressConstants.DISPLAY_OPTION_EMAIL; shippingAddressControl.setDisplayOption(displayOption_val); return shippingAddressControl; }
private PlainTextControl makePlainTextControl() { PlainTextControl plainTextControl = new PlainTextControl("ExamplePlainTextControlId"); plainTextControl.setText("Plain Text [example]", "This is example of PlainTextControl"); return plainTextControl; }
private AmountBoxControl makeAmountControl() { AmountBoxControl amountBoxControl = new AmountBoxControl(AMOUNT_CONTROL_ID, "USD"); amountBoxControl.addItem(PRODUCT_ITEM_ID, "Items", 1000, ""); amountBoxControl.addItem(PRODUCT_TAX_ID, "Tax", 50, ""); amountBoxControl.addItem(PRODUCT_SHIPPING_ID, "Shipping", 10, ""); amountBoxControl.setAmountTotal(1060, AmountConstants.FORMAT_TOTAL_PRICE_ONLY); amountBoxControl.addItem(3, PRODUCT_FUEL_ID, "FUEL", 0, "Pending"); return amountBoxControl; }
// Example for adding new item while updating values AmountBoxControl amount = (AmountBoxControl) sheet.getSheetControll("id_amount"); amount.updateValue("itemId", 900); amount.updateValue("taxId", 50); amount.updateValue("shippingId", 10); amount.updateValue("fuelId", 0); // Add “Dicount” item amount.addItem(4, "discountId", "Discount", -60, ""); amount.setAmountTotal(1000, AmountConstants.FORMAT_TOTAL_PRICE_ONLY); sheet.updateControl(amount); // Call updateSheet with AmountBoxControl. This is mandatory. try { paymentManager.updateSheet(sheet); } catch (IllegalStateException | NullPointerException e) { e.printStackTrace(); }
This control is used for displaying Spinner options on a custom payment sheet.
When allocating the control, controlId, title, and SheetItemType are needed to distinguish between the types of spinner displayed. The merchant app can set the following properties with SpinnerControl.
// Construct SpinnerControl SpinnerControl spinnerControl = new SpinnerControl(SHIPPINGMETHOD_SPINNER_ID, "Shipping Method ", SheetItemType.SHIPPING_METHOD_SPINNER); // User can select one of the shipping method option on the payment sheet. spinnerControl.addItem("shipping_method1", getString(R.string.standard_shipping_free)); spinnerControl.addItem("shipping_method2", getString(R.string.twoday_shipping) ); spinnerControl.addItem("shipping_method3", getString(R.string.oneday_shipping)); spinnerControl.setSelectedItemId("shipping_method1"); // Set default option // This listener is for listening SheetControl events spinnerControl.setSheetUpdatedListener(new SheetUpdatedListener() { @Override public void onResult(String updatedControlId, CustomSheet customSheet) { AmountBoxControl amountBoxControl = (AmountBoxControl) customSheet.getSheetControl(AMOUNT_CONTROL_ID); SpinnerControl spinnerControl = (SpinnerControl) customSheet.getSheetControl(updatedControlId); switch (spinnerControl.getSelectedItemId()) { case "shipping_method1": amountBoxControl.updateValue(PRODUCT_SHIPPING_ID, 10); break; case "shipping_method2": amountBoxControl.updateValue(PRODUCT_SHIPPING_ID, 10 + 0.1); break; case "shipping_method3": amountBoxControl.updateValue(PRODUCT_SHIPPING_ID, 10 + 0.2); break; default: amountBoxControl.updateValue(PRODUCT_SHIPPING_ID, 10); break; } amountBoxControl.setAmountTotal(1000 + amountBoxControl.getValue(PRODUCT_SHIPPING_ID), AmountConstants.FORMAT_TOTAL_PRICE_ONLY); customSheet.updateControl(amountBoxControl); // Call updateSheet with AmountBoxControl. This is mandatory. try { paymentManager.updateSheet(customSheet); } catch (IllegalStateException | NullPointerException e) { e.printStackTrace(); } } }); // Construct SpinnerControl SpinnerControl spinnerControl = new SpinnerControl(INSTALLMENT_SPINNER_ID, "Installment", SheetItemType.INSTALLMENT_SPINNER); spinnerControl.addItem("installment1", "1 month without interest"); spinnerControl.addItem("installment2", "2 months with 2% monthly interest"); spinnerControl.addItem("installment3", "3 months with 2.2% monthly interest"); spinnerControl.setSelectedItemId("installment1"); // Set default option // This listener is for listening SheetControl events spinnerControl.setSheetUpdatedListener(new SheetUpdatedListener() { @Override public void onResult(String updatedControlId, CustomSheet customSheet) { AmountBoxControl amountBoxControl = (AmountBoxControl) customSheet.getSheetControl(AMOUNT_CONTROL_ID); SpinnerControl spinnerControl = (SpinnerControl) customSheet.getSheetControl(updatedControlId); double TotalInterest = 0; switch (spinnerControl.getSelectedItemId()) { case "installment1": amountBoxControl.updateValue(PRODUCT_TOTAL_INTEREST_ID, TotalInterest); break; case "installment2": // Calculate total interest again and updateValue amountBoxControl.updateValue(PRODUCT_TOTAL_INTEREST_ID, TotalInterest); break; case "installment3": // Calculate total interest again and updateValue amountBoxControl.updateValue(PRODUCT_TOTAL_INTEREST_ID, TotalInterest); break; default: amountBoxControl.updateValue(PRODUCT_TOTAL_INTEREST_ID, TotalInterest); break; } amountBoxControl.setAmountTotal(1000 + amountBoxControl.getValue(PRODUCT_TOTAL_INTEREST_ID), AmountConstants.FORMAT_TOTAL_PRICE_ONLY); customSheet.updateControl(amountBoxControl); // Call updateSheet with AmountBoxControl. This is mandatory. try { paymentManager.updateSheet(customSheet); } catch (IllegalStateException | NullPointerException e) { e.printStackTrace(); } } });
void updateSheet(final CustomSheet sheet, final int errorCode, final String customErrorMessage)
//In any abnormal cases, merchant can update sheet with CUSTOM_MESSSAGE error code. paymentManager.updateSheet(customSheet, PaymentManager.CUSTOM_MESSAGE, "Phone number is not valid. Please change your phone number!");