Use the consumer key generated in the Merchant Portal to set the client id.
MasterCardApiConfig.setClientId("cLb0tKkEJhGTITp_6ltDIibO5Wgbx4rIldeXM_jRd4b0476c!414f4859446c4a366c726a327474695545332b353049303d");
Prior to completing this step, the developer must have uploaded or generated a PEM encoded Certificate Request File as part of the onboarding process.
MasterCardApiConfig.setPrivateKey(getPrivateKey());
...
private static PrivateKey getPrivateKey() {
String fileName = "example-file.p12"; // path to your p12 file
String password = "password"; // your keystore password
PrivateKey privateKey = null;
try {
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(AbstractTest.class.getResourceAsStream(fileName), password.toCharArray());
Enumeration<String> enumeration = ks.aliases();
String keyAlias = enumeration.nextElement();
privateKey = (PrivateKey) ks.getKey(keyAlias, password.toCharArray());
} catch (Exception e) {
throw new MCApiRuntimeException(e);
}
return privateKey;
}
MasterCardApiConfig.SANDBOX = true;
Call the Request Token Service twice. The first call generates a pairing request token, which will be used to gain the long access token needed to pair the consumer's wallet and merchant account.
The second call generates a checkout request token that will be used to invoke the MasterPass Lightbox. Pass a callback_url that the browser will redirect to when the Lightbox closes.
The Request Token Service calls must be made before the consumer clicks the Buy with MasterPass button.
String callback_url = "https://www.somemerchant.com/checkoutconfirm.htm";
RequestTokenResponse pairingResponse = RequestTokenService.create(Auth.auth(callback_url));
String pairing_request_token = pairingResponse.getOauthToken();
RequestTokenResponse checkoutResponse = RequestTokenService.create(Auth.auth(callback_url));
String checkout_request_token = checkoutResponse.getOauthToken();
The Shopping Cart Service enables shopping cart data to be displayed to users as they proceed through the MasterPass login and checkout. This service requires the checkout request token from Step 1.
Each ShoppingCartItem must have a description, quantity, and value. Value is represented as an integer (for example, USD 100.00 is 10000), and is calculated as the cost of a single item multiplied by the quantity of that item. A ShoppingCartItem may also have an optional imageURL.
The Shopping Cart Service must be called before the consumer clicks the Buy with MasterPass button.
//Create an instance of ShoppingCartRequest
ShoppingCartRequest shoppingCartRequest = new ShoppingCartRequest()
.shoppingCart(new ShoppingCart()
.subtotal(11900)
.currencyCode("USD")
.shoppingCartItem(new ShoppingCartItem()
.value(1900)
.description("This is one item")
.quantity(1))
.shoppingCartItem(new ShoppingCartItem()
.imageURL("https://somemerchant.com/someimage")
.value(10000)
.description("Five items")
.quantity(5)))
.oAuthToken(checkout_request_token);
//Call the ShoppingCartService with required params
ShoppingCartResponse shoppingCartResponse = ShoppingCartService.create(shoppingCartRequest);
The Merchant Initialization Service is used to secure Lightbox connections between the merchant and MasterPass. The originUrl identifies the origin of the page that will initialize the Lightbox. This service requires the checkout request token from Step 1.
The Merchant Initialization Service must be called before the consumer clicks the Buy with MasterPass button.
//Create an instance of MerchantInitializationRequest
MerchantInitializationRequest merchantInitializationRequest = new MerchantInitializationRequest()
.originUrl("https://somemerchant.com")
.oAuthToken(checkout_request_token);
//Call the MerchantInitializationApi with required params
MerchantInitializationResponse merchantInitializationResponse = MerchantInitializationService.create(merchantInitializationRequest);
On the Checkout Page of the merchant's website, add the Buy with MasterPass button to your HTML (see MasterPass Branding to customize size and locale).
<img src="https://www.mastercard.com/mc_us/wallet/img/en/US/mcpp_wllt_btn_chk_147x034px.png"/>
Import jQuery, and add the MasterPass Integration Script for either the sandbox or the production environment.
<!-- url to jquery 1.10.2 -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/
jquery/1.10.2/jquery.min.js"></script>
<!-- url to sandbox MasterPass Lightbox -->
<script type="text/javascript" src=" https://sandbox.masterpass.com/
lightbox/Switch/integration/MasterPass.client.js "></script>
<!-- url to production MasterPass Lightbox -->
<!-- <script type="text/javascript" src=" https://masterpass.com/lightbox/
Switch/integration/MasterPass.client.js "></script> -->
Invoke the MasterPass lightbox when the user clicks the Buy with MasterPass button.
<script type="text/javascript" language="Javascript">
MasterPass.client.checkout({
"requestPairing":true, //required
"requestToken":"insert_checkout_request_token_here", //required
"pairingRequestToken":"insert_pairing_request_token_here", //required
"callbackUrl":"http://www.somemerchant.com/checkoutcomplete.htm", //required
"merchantCheckoutId":"insert_checkout_id_here", //required
"requestedDataTypes":["REWARD_PROGRAM", "ADDRESS", "PROFILE", "CARD"], //required
"allowedCardTypes":["master","amex","diners","discover","maestro","visa"], //optional
"version":"v6", //optional
"successCallback": function(mpLightboxResponse){ ... }, //optional
"failureCallback": function(mpLightboxResponse){ ... }, //optional
"cancelCallback": function(mpLightboxResponse){ ... } //optional
});
</script>
After the consumer completes checkout, MasterPass returns data to the merchant. By default, MasterPass redirects the browser to the callback URL, returning the data via URL parameters. Optionally, if callback functions were provided in Step 4, MasterPass returns control to the page that initiated the Lightbox, and the data is passed to the callback function.
Under certain conditions, the MasterPass UI may be forced to run in Full Screen display instead of launching the Lightbox. When this occurs, the browser will redirect to the callback URL when the MasterPass flow is complete, even if callback functions were supplied.
On success, MasterPass returns the following parameters:
http://www.somemerchant.com/checkoutcomplete.htm
?mpstatus=success
&checkout_resource_url=https%3A%2F%2Fstage.api.mastercard.com%2Fmasterpass%2Fv6%2Fcheckout%2F
10706241%3Fwallet%3Dphw
&oauth_verifier=fbe45bcad30299c93765b1fb4b45bab208f84458
&oauth_token=d9382e34e0721a68a9952110cecdf89517e45498
&pairing_verifier=6c50838e31b7441e6eafa2229385452889255b13
&pairing_token=35b2a0cf87f8160fcb5d24996a12edb7cce4c530
http://www.somemerchant.com/checkoutcomplete.htm
?mpstatus=cancel
http://www.somemerchant.com/checkoutcomplete.htm
?mpstatus=failure
If the checkout was completed successfully, the next step is to make two Access Token Service calls. The first call uses the checkout request token and the checkout verifier token from Step 5 to retrieve a checkout access token. This checkout access token will be used in the next step to retrieve checkout data from the current transaction.
The second call uses the pairing request token and the pairing verifier token from Step 5 to retrieve a long access token. The long access token will be used to retrieve pre-checkout data for this consumer in future transactions.
public void accessTokenService_returnsToken() throws Exception {
AccessTokenResponse checkoutAccessTokenResponse = AccessTokenService.create(Auth.auth(oauth_checkout_request_token),Auth.auth(oauth_checkout_verifier_token));
String checkout_accessToken = checkoutAccessTokenResponse.getOauthToken();
AccessTokenResponse longAccessTokenResponse = AccessTokenService.create(Auth.auth(oauth_pairing_request_token),Auth.auth(oauth_pairing_verifier_token));
String long_accessToken = longAccessTokenResponse.getOauthToken();
}
Use the checkout resource URL received in Step 5 and the checkout access token from Step 6 to retrieve the consumer’s payment, shipping, reward and 3-D Secure information from MasterPass.
//TEMPORARY This is going to change to use checkout resource url
Checkout checkout = CheckoutApi.show(checkoutid, Auth.auth(accessToken));
Card card = checkout.getCard();
Address billingAddress = card.getBillingAddress();
Contact contact = checkout.getContact();
AuthenticationOptions authOptions = checkout.getAuthenticationOptions(); //result of 3DS verification
String preCheckoutTransactionId = checkout.getPreCheckoutTransactionId();
RewardProgram rewardProgram = checkout.getRewardProgram();
ShippingAddress shippingAddress = checkout.getShippingAddress();
String transactionId = checkout.getTransactionId();
String walletId = checkout.getWalletID();
Using the data retrieved in Step 7, process the payment through the merchant's payment gateway.
After the payment has been processed in Step 8, make a service call to communicate the result of the transaction to MasterPass.
//Create an instance of MerchantTransactions
MerchantTransactions request = new MerchantTransactions()
.merchantTransactions(new MerchantTransactions()
.transactionId(4549794) //from Step 7
.purchaseDate("2014-08-01T14:52:57.539-05:00")
.expressCheckoutIndicator(false)
.approvalCode("888888") // authorization code returned by payment gateway
.transactionStatus("Success")
.orderAmount(1229)
.preCheckoutTransactionId("a4a6x55-rgb1c5-hyaqkemj-1-hybxhplo-947") //from Step 7
.currency("USD")
.consumerKey("0zMKpm0nFtUv8lLXT97jDRo2bp4vNF8MFYyt3R5R87e3f3f4!414b48675861677159682b563745776b593652377939673d")); //generated during onboarding process
//Call the PostbackService with required params
MerchantTransactions response = PostbackService.create(request);
Call the Request Token Service to generate the pairing request token that will be used to invoke the MasterPass Lightbox. Pass a callback_url that the browser will redirect to when the Lightbox closes.
The Request Token Service must be called before the consumer clicks the Connect with MasterPass button.
String callback_url = "https://www.somemerchant.com/pairingcomplete.htm"
RequestTokenResponse requestTokenResponse = RequestTokenService.create(Auth.auth(callback_url));
String pairing_request_token = requestTokenResponse.getOauthToken();
The Merchant Initialization Service is used to secure Lightbox connections between the merchant site and MasterPass. The originUrl identifies the origin of the page that will initialize the Lightbox. This service requires the pairing request token from Step 1.
//Create an instance of MerchantInitializationRequest
MerchantInitializationRequest merchantInitializationRequest = new MerchantInitializationRequest()
.originUrl("https://somemerchant.com")
.oAuthToken(pairing_request_token);
//Call the MerchantInitializationApi with required params
MerchantInitializationResponse merchantInitializationResponse = MerchantInitializationService.create(merchantInitializationRequest);
Add the Connect with MasterPass button to the merchant site (for example, on the user account page) (see MasterPass Branding to customize size and locale).
<img src="https://www.mastercard.com/mc_us/wallet/img/en/US/mp_connect_with_button_126px.png"/>
Import jQuery, and add the MasterPass Integration Script for either the sandbox or the production environment.
<!-- url to jquery 1.10.2 -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/
jquery/1.10.2/jquery.min.js"></script>
<!-- url to sandbox MasterPass Lightbox -->
<script type="text/javascript" src=" https://sandbox.masterpass.com/
lightbox/Switch/integration/MasterPass.client.js "></script>
<!-- url to production MasterPass Lightbox -->
<!-- <script type="text/javascript" src=" https://masterpass.com/lightbox/
Switch/integration/MasterPass.client.js "></script> -->
Invoke the MasterPass lightbox when the user clicks the Connect with MasterPass button.
<script type="text/javascript" language="Javascript">
MasterPass.client.connect({
"requestPairing":true, //required
"pairingRequestToken":"insert_pairing_request_token_here", //required
"callbackUrl":"http://www.somemerchant.com/pairingcomplete.htm", //required
"merchantCheckoutId":"insert_checkout_id_here", //required
"requestedDataTypes":["REWARD_PROGRAM", "ADDRESS", "PROFILE", "CARD"], //required
"version":"v6" //optional
"successCallback": function(mpLightboxResponse){ ... }, //optional
"failureCallback": function(mpLightboxResponse){ ... }, //optional
"cancelCallback": function(mpLightboxResponse){ ... } //optional
});
</script>
After the lightbox closes, MasterPass returns data to the merchant. By default, MasterPass redirects the browser to the callback URL, returning the data via URL parameters. Optionally, if callback functions were provided in Step 4, MasterPass returns control to the page that initiated the Lightbox, and the data is passed to the callback function.
Under certain conditions, the MasterPass UI may be forced to run in Full Screen display instead of launching the Lightbox. When this occurs, the browser will redirect to the callback URL when the MasterPass flow is complete, even if callback functions were supplied.
On Success, MasterPass returns the following parameters:
http://www.somemerchant.com/pairingcomplete.htm
?mpstatus=success
&pairing_verifier=6c50838e31b7441e6eafa2229385452889255b13
&pairing_token=35b2a0cf87f8160fcb5d24996a12edb7cce4c530
http://www.somemerchant.com/pairingcomplete.htm
?mpstatus=cancel
http://www.somemerchant.com/pairingcomplete.htm
?mpstatus=failure
function handleCallbackSuccess (data) {
document.getElementById('pairingToken').value=data.pairing_token;
document.getElementById('pairingVerifer').value=data.pairing_verifier;
}
If the pairing was completed successfully, call the Access Token Service to retrieve a long access token. You will need the pairing request token and the pairing verifier token received in Step 4.
The long access token will be used to retrieve pre-checkout data for this consumer in the future. Store the long access token with this user's merchant account data so that it can be retrieved anytime the user logs into the merchant site.
AccessTokenResponse accessTokenResponse = AccessTokenService.create(Auth.auth("oauth_pairing_request_token"),Auth.auth("oauth_pairing_verifier_token"));
String long_accessToken = accessTokenResponse.getOauthToken();
A consumer's long access token should have been stored with their account data for the merchant's site. When the consumer logs into their merchant site account, get their long access token.
Use the consumer's long access token to retrieve pre-checkout data. The data types returned will be the types that were requested via the requestedDataTypes parameter when the Lightbox was invoked (Step 4 of Pairing During Checkout or Step 3 of Pairing Without Checkout).
String[] requestedDataTypes = {"REWARD_PROGRAM", "ADDRESS", "PROFILE", "CARD"};
PrecheckoutDataRequest preCheckoutDataRequest = new PrecheckoutDataRequest();
PairingDataTypes pairingDataTypes = new PairingDataTypes();
for(var i = 0; i < requestedDataTypes.length; i++){
pairingDataTypes.PairingDataType(new PairingDataType().type(TypeEnum.valueOf(requestedDataTypes[i])));
}
preCheckoutDataRequest.pairingDataTypes(pairingDataTypes);
PrecheckoutDataResponse response = PrecheckoutDataApi.create(Auth.auth(longAccessToken), preCheckoutDataRequest);
Display the wallet data retrieved in Step 2 to the consumer, and prompt the consumer to select from it. Save the selected cardId, shippingId, and (if applicable) rewardId to be used on the Place Order page of the merchant site.
Call the Request Token Service to generate the request token that will be used to invoke the MasterPass Lightbox. Pass a callback_url that the browser will redirect to when the Lightbox closes.
The Request Token Service must be called before creating the Buy with MasterPass button.
String callback_url = "https://www.somemerchant.com/checkoutconfirm.htm"
RequestTokenResponse requestTokenResponse = RequestTokenService.create(Auth.auth(callback_url));
String request_token = requestTokenResponse.getOauthToken();
The Shopping Cart Service enables shopping cart data to be displayed to users as they proceed through the MasterPass login and checkout. This service requires the request token from Step 4.
Each ShoppingCartItem must have a description, quantity, and value. Value is represented as an integer (for example, USD 100.00 is 10000), and is calculated as the cost of a single item multiplied by the quantity of that item. A ShoppingCartItem may also have an optional imageURL.
The Shopping Cart Service must be called before creating the Buy with MasterPass button.
//Create an instance of ShoppingCartRequest
ShoppingCartRequest shoppingCartRequest = new ShoppingCartRequest()
.shoppingCart(new ShoppingCart()
.subtotal(11900)
.currencyCode("USD")
.shoppingCartItem(new ShoppingCartItem()
.value(1900)
.description("This is one item")
.quantity(1))
.shoppingCartItem(new ShoppingCartItem()
.imageURL("https://somemerchant.com/someimage")
.value(10000)
.description("Five items")
.quantity(5)))
.oAuthToken(request_token);
//Call the ShoppingCartService with required params
ShoppingCartResponse shoppingCartResponse = ShoppingCartService.create(shoppingCartRequest);
The Merchant Initialization Service is used to secure Lightbox connections between the merchant site and MasterPass. The originUrl identifies the origin of the page that will invoke the Lightbox. This service requires the request token from Step 4.
The Merchant Initialization Service must be called before creating the Buy with MasterPass button.
//Create an instance of MerchantInitializationRequest
MerchantInitializationRequest merchantInitializationRequest = new MerchantInitializationRequest()
.originUrl("https://somemerchant.com")
.oAuthToken(request_token);
//Call the MerchantInitializationApi with required params
MerchantInitializationResponse merchantInitializationResponse = MerchantInitializationService.create(merchantInitializationRequest);
Import jQuery, and add the MasterPass Integration Script for either the sandbox or the production environment
<!-- url to jquery 1.10.2 -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/
jquery/1.10.2/jquery.min.js"></script>
<!-- url to sandbox MasterPass Lightbox -->
<script type="text/javascript" src=" https://sandbox.masterpass.com/
lightbox/Switch/integration/MasterPass.client.js "></script>
<!-- url to production MasterPass Lightbox -->
<!-- <script type="text/javascript" src=" https://masterpass.com/lightbox/
Switch/integration/MasterPass.client.js "></script> -->
On the Place Order page of the merchant's website, create the Buy with MasterPass button using the cardId, addressId, and rewardId from Step 3 and the request token from Step 4.
<script type="text/javascript" language="Javascript">
MasterPass.client.checkout({
"requestToken":"insert_request_token_here", //required
"callbackUrl":"http://www.somemerchant.com/checkoutcomplete.htm", //required
"merchantCheckoutId":"insert_checkout_id_here", //required
"cardId":"insert_selected_card_id_here", //required
"shippingId":"insert_selected_shipping_address_id_here", //required
"precheckoutTransactionId":"insert_prechechout_txn_id_here", //required
"walletName":"insert_wallet_name_here", //required
"consumerWalletId":"insert_consumer_walletid_here", //required
"loyaltyId":"insert_loyalty_id_here", //optional
"requireShippingDestination": "true" //optional
"version":"v6", //optional
"successCallback": function(mpLightboxResponse){ ... }, //optional
"failureCallback": function(mpLightboxResponse){ ... }, //optional
"cancelCallback": function(mpLightboxResponse){ ... } //optional
});
</script>
After the consumer completes checkout, MasterPass returns data to the merchant. By default, MasterPass redirects the browser to the callback URL, returning the data via URL parameters. Optionally, if callback functions were provided in Step 4, MasterPass returns control to the page that initiated the Lightbox, and the data is passed to the callback function.
Under certain conditions, the MasterPass UI may be forced to run in Full Screen display instead of launching the Lightbox. When this occurs, the browser will redirected to the callback URL when the MasterPass flow is complete, even if callback functions were supplied.
On Success, MasterPass returns the following parameters:
http://www.somemerchant.com/checkoutcomplete.htm
?mpstatus=success
&checkout_resource_url=https%3A%2F%2Fsandbox.api.mastercard.com%2Fmasterpass\%2Fv6%2Fcheckout%2F10189977%3Fwallet%3Dphw
&oauth_verifier=6c50838e31b7441e6eafa2229385452889255b13
&oauth_token=d6fa60984308aebb6183d44fb9688fb9dc8332dc
http://www.somemerchant.com/checkoutcomplete.htm
?mpstatus=cancel
http://www.somemerchant.com/checkoutcomplete.htm
?mpstatus=failure
function handleCallbackSuccess (data) {
callYourServerWith(data.oauth_token, data.oauth_verifier, data.checkout_resource_url);
}
If the checkout was completed successfully, call the Access Token Service to retrieve an access token. You will need the request token and the verifier token received in Step 8.
As there is no need to retrieve a long access token again, only this one call to the Access Token Service will be made.
public void accessTokenService_returnsToken() throws Exception {
AccessTokenResponse accessTokenResponse = AccessTokenService.create(Auth.auth(request_token), Auth.auth(verifier_token));
String accessToken = accessTokenResponse.getOauthToken();
}
Use the Checkout Resource URL received in Step 8 and the access token from Step 9 to retrieve the constomer’s payment, shipping, reward and 3-D Secure information from MasterPass.
//TEMPORARY This is going to change to use checkout resource url
Checkout checkout = CheckoutApi.show(checkoutid, Auth.auth(accessToken));
Card card = checkout.getCard();
Address billingAddress = card.getBillingAddress();
Contact contact = checkout.getContact();
AuthenticationOptions authOptions = checkout.getAuthenticationOptions();
String preCheckoutTransactionId = checkout.getPreCheckoutTransactionId();
RewardProgram rewardProgram = checkout.getRewardProgram();
ShippingAddress shippingAddress = checkout.getShippingAddress();
String transactionId = checkout.getTransactionId();
String walletId = checkout.getWalletID();
Using the data retrieved in Step 10, process the payment through the merchant's payment gateway.
After the payment has been processed in Step 11, make a service call to communicate the result of the transaction to MasterPass.
//Create an instance of MerchantTransactions
MerchantTransactions request = new MerchantTransactions()
.merchantTransactions(new MerchantTransaction()
.transactionId(4549794)
.purchaseDate("2014-08-01T14:52:57.539-05:00")
.expressCheckoutIndicator(false)
.approvalCode("888888") // from your payment gateway authorization
.transactionStatus("Success")
.orderAmount(1229) // total charged to card
.preCheckoutTransactionId("a4a6x55-rgb1c5-hyaqkemj-1-hybxhplo-947")
.currency("USD")
.consumerKey("0zMKpm0nFtUv8lLXT97jDRo2bp4vNF8MFYyt3R5R87e3f3f4!414b48675861677159682b563745776b593652377939673d"));
//Call the PostbackService with required params
MerchantTransactions response = PostbackService.create(request);