Messaging

Send messages to the iframe

Payment Information

Once the iframe is loaded, the parent window must post a PAYMENT_FORM_INFO message to the iframe including the authorisation token, payment information and configurations for PayPal button customisation using this function:

window.postMessage(message, targetOrigin, [transfer]);

Parameter list:

paymentInfo (Required)

NameTypeDescriptionRequired?
tokenstringAuthentication token✅
riskobjectInformation to enable risk assessment process✅
risk[currentService]stringThe service or accountId to which the order/payment relates. e.g. the MSN being recharged✅
risk[deviceFingerprint]stringA unique identifier of the device from which the payment request is being made✅
risk[ipAddress]stringThe IP address of the device from which the payment is being submitted✅
risk[parentAccount]stringReference to the parent account to which the current service belongs; can be same as currentService✅
risk [channel]stringThe medium from which the transaction is originated (the pre-built UI is used)
eg: web, app
✅
risk [simNumber]stringThe SIM number to which the payment relates; applicable only for telecom sim activation➖
risk [activationDate]dateThe date upon which the service was activated for the current user; The date must be today or a past date.➖
requestIdstringUnique identifier for request provided by merchant✅
servicestringThe service for which the payment is being completed, e.g. Shop, Recharge, Unlock✅
amountobjectThe value of funds to be transferred and the currency✅
amount [value]numberThe total value of the funds to be transferred; order total✅
amount [currency]stringThe currency in which the transaction is to occur✅
itemsobjectThe array of items the customer purchases from the merchant✅
items[name]stringThe item name or title✅
items [quantity]stringThe item quantity. Must be a whole number✅
items[description]stringThe detailed item description➖
items [unitPrice]stringThe unit price of the item✅
customerobjectThe details of customer requesting the order➖
customer[givenNames]stringThe given names of the customer✅
customer[surname]stringThe last name of the customer✅
customer[email]stringThe email address of customer✅
shippingobjectThe name and address of the person to whom to ship the items.➖
shipping[name]stringThe name of the person to whom to ship the items✅
shipping[line]stringThe address of the person to whom to ship the items, such as number and street✅
shipping[area]stringA city, town, or village✅
shipping[region]stringThe highest-level sub-division in a country, which is usually a province, state, or ISO-3166-2 subdivision✅
shipping[postcode]stringThe postal code, which is the ZIP code or equivalent. Typically required for countries with a postal code or an equivalent✅
shipping[countryCode]stringThe 2-character ISO 3166-1 code that identifies the country or region✅

config (Optional)

NameTypeDescriptionRequired?
paypalHeightnumberThe height of the PayPal button. min: 25, max: 55➖
Default:40
paypalColorstringThe theme colour of the PayPal button. one of gold, blue, white, black, sliver➖
Default: blue
paypalShapestringThe shape of the PayPal button. one of pill, rect➖
Default:pill

Sample

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
   $(document).ready(function () {
     createPaymentFrame();
   });
   
   function createPaymentFrame() {
     const paymentFrame = document.createElement("iframe");
		 // logic to construct the frame
     if (paymentFrame.addEventListener) {
       paymentFrame.addEventListener("load", sendMessage, false);
     } else if (paymentFrame.attachEvent) {
       paymentFrame.attachEvent("onload", sendMessage);
     }
     document.getElementById("payment").appendChild(paymentFrame);
   }
   
   function sendMessage() {  
     const paymentWindow = this.contentWindow;
     paymentWindow.postMessage(
      {
    type: "PAYMENT_FORM_INFO",
    value: {
        paymentInfo: {
              token: "< oauth token>",
              risk: {
                  currentService: "0469024567",
                  deviceFingerprint: "ABCDE12345",
                  ipAddress: "192.168.0.1",
                  parentAccount: "0469024567",
                  channel: "web",
                  merchantData: {"simNumber":"77666655555", "activationDate": "2022-05-01"}
                    },
              service: "shop",
              requestId:"test-paypal",
              amount: {
                            value: 25.00,
                            currency: "AUD"
                       },
              items: [  {
                            name: "iPhone",
                            quantity:"1",
                            description: "Online store order 1",
                            unitPrice: "12.00"
                        },
                        {
                            name: "Samsung",
                            quantity:"1",
                            description: "Online store order 2",
                            unitPrice: "13.00"
                        }
                     ],
              customer: {
                            givenNames: "Joe",
                            surname: "Consumer",
                            email: "[email protected]"
                        },
              shipping: {
                            name: "Joe Consumer",
                            line: "123 Fake Street",
                            area: "Melbourne",
                            region: "VIC",
                            postcode: "3000",
                            countryCode: "AU"
                        },

                           },
        config: {
          paypalShape: "rect",
          paypalHeight: 50,
          paypalColor: "silver",
                },
         },
     },
  "*"
);
   }
</script>
<body>
   <div id="payment" style="height: 400px" />
</body>

Receive messages from the iframe

A Parent window can listen for dispatched messsages from IFrame by executing this code

if (window.addEventListener) {
    window.addEventListener("message", (event) => console.log(event.data), false);
} else if (window.attachEvent) {
  // before IE 9 support
  window.attachEvent("onmessage", (event) => console.log(event.data));
} else {
  throw new Error("Could not attach message listener");
}

Response messages from the iframe

Message Format

Successful responses will be returned in the Compact format

Example: {type: SUCCESS, value: {id: 'aaa', receiptId: '312456', token: '4CU64277MN5554947', amount: 12, currency: 'AUD'}

Values present in successful response

NameTypePresent?Description
idstring✅Uniquely identifies each pre-authorise attempt. This is generated by Afterpay system
receiptIdstring✅identifier for the reservation made
tokenstring✅The unique identifier representing the customer's paypal order
amountnumber✅The value of the funds to be transferred
currencystring✅The currency in which the transaction is to occur

Failed response

Example: FAILED {code: '99', message: 'Other error occurred.', method: 'POST', endPoint: '/v1/reservations'}

Values present in failed response

NameTypePresent?Description
codenumeric✅A number that maps to an error
messagestring✅A human-readable message providing more details about the error
methodstring✅HTTP method POST
endpointstring✅endpoint information (v1/reservations)