You are here:
Sample Code Snippets for Apex Classes
Use these Apex classes to define the parameters required for WhatsApp Pay integration.
Here are the sample Apex classes that you configure.
- Payment Total
- Payment Line Items
- Order Context
- Payment Initiations
Payment Total
This class calculates and provides the total amount that the customer pays. It represents the final transaction cost after the system applies all calculations.
public with sharing class GetPaymentTotal {
@InvocableMethod
public static List<Outputs> getPaymentTotal() {
Double amount = 0.25;
List<Outputs> result = new List<Outputs>();
RichMessaging.PaymentLineItem total = new RichMessaging.PaymentLineItem('total amount', amount);
total.amountValue = 1.5;
Outputs o1 = new Outputs();
o1.paymentLineItem = total;
result.add(o1);
return result;
}
public class Outputs {
@InvocableVariable
public RichMessaging.PaymentLineItem paymentLineItem;
}
}
Payment Line Items
This class generates an itemized receipt for the customer. It lists the individual products or services, such as a pizza or pasta, and their prices so the buyer can review their cart directly within the WhatsApp chat.
public with sharing class GetPaymentLineItems {
@InvocableMethod
public static List<List<RichMessaging.PaymentLineItem>> getLineItems() {
Double amount = 0.25;
Double amount1 = 1.25;
List<List<RichMessaging.PaymentLineItem>> result = new List<List<RichMessaging.PaymentLineItem>>();
RichMessaging.PaymentLineItem pizza = new RichMessaging.PaymentLineItem('pizza', amount);
RichMessaging.PaymentLineItem pasta = new RichMessaging.PaymentLineItem('pasta', amount1);
pizza.statusValue = RichMessaging.PaymentItemStatus.FinalCost;
pasta.statusValue = RichMessaging.PaymentItemStatus.FinalCost;
RichMessaging.OrderItemCommerceAttributes commerceAttrs = new RichMessaging.OrderItemCommerceAttributes();
commerceAttrs.retailerIdValue = 'retailed_id';
commerceAttrs.categoryValue = RichMessaging.OrderItemCategory.Product;
pizza.commerceValue = commerceAttrs;
pasta.commerceValue = commerceAttrs;
List<RichMessaging.PaymentLineItem> options = new List<RichMessaging.PaymentLineItem>{
pizza, pasta
};
result.add(options);
return result;
}
}
Order Context
This class defines the background details and logistics of the order. It acts as the “terms and conditions” and “shipping label,” and provides necessary metadata such as billing and shipping addresses, the payment link's expiration time, and the type of goods being sold.
public with sharing class GetOrderContext {
@InvocableMethod
public static List<OrderContextOutputs> getOrderContext() {
RichMessaging.OrderContext orderContext = new RichMessaging.OrderContext();
RichMessaging.OrderExpiration orderExpiration = new RichMessaging.OrderExpiration();
orderExpiration.description = 'Expiration Description';
orderExpiration.timestamp = 2342534536L;
orderContext.expiration = orderExpiration;
orderContext.catalogId = 'CAT-001';
orderContext.typeValue = RichMessaging.OrderType.DigitalGoods;
orderContext.checkoutBehaviorValue = RichMessaging.PaymentCheckoutBehavior.QuickPay;
orderContext.referenceId = 'REF-001';
List<RichMessaging.OrderBeneficiary> beneficiaries = new List<RichMessaging.OrderBeneficiary>();
RichMessaging.OrderBeneficiary beneficiaryA = new RichMessaging.OrderBeneficiary();
beneficiaryA.name = 'beneficiary Name A';
beneficiaryA.phone = 'beneficiary Phone A';
List<String> addressLinesA = new List<String>();
addressLinesA.add('Addresss Line 1');
addressLinesA.add('Addresss Line 2');
RichMessaging.Address addressA = new RichMessaging.Address();
addressA.addressLines = addressLinesA;
addressA.subLocality = 'subLocality A';
addressA.locality = 'locality A';
addressA.postalCode = '111111';
addressA.subAdministrativeArea = 'subAdministrativeArea A';
addressA.administrativeArea = 'administrativeArea A';
addressA.country = 'India';
addressA.countryCode = 'IN';
beneficiaryA.address = addressA;
RichMessaging.OrderBeneficiary beneficiaryB = new RichMessaging.OrderBeneficiary();
beneficiaryB.name = 'beneficiary Name B';
beneficiaryB.phone = 'beneficiary Phone B';
List<String> addressLinesB = new List<String>();
addressLinesB.add('Addresss Line 1');
addressLinesB.add('Addresss Line 2');
RichMessaging.Address addressB = new RichMessaging.Address();
addressB.addressLines = addressLinesB;
addressB.subLocality = 'subLocality B';
addressB.locality = 'locality B';
addressB.postalCode = '222222';
addressB.subAdministrativeArea = 'subAdministrativeArea B';
addressB.administrativeArea = 'administrativeArea B';
addressB.country = 'India';
addressB.countryCode = 'IN';
beneficiaryB.address = addressB;
beneficiaries.add(beneficiaryA);
beneficiaries.add(beneficiaryB);
orderContext.beneficiaries = beneficiaries;
OrderContextOutputs output = new OrderContextOutputs();
output.orderContext = orderContext;
List<OrderContextOutputs> result = new List<OrderContextOutputs>();
result.add(output);
return result;
}
public class OrderContextOutputs {
@InvocableVariable
public RichMessaging.OrderContext orderContext;
}
}
Payment Initiations
This class configures how the customer can pay. It specifies the approved payment methods, such as PIX or Boleto, and provides the underlying routing codes and account keys required to process the transaction.
public class GetPaymentInitiations {
@InvocableMethod
public static List<List<RichMessaging.PaymentInitiation>> getPaymentInitiation() {
List<List<RichMessaging.PaymentInitiation>> result = new List<List<RichMessaging.PaymentInitiation>>();
RichMessaging.PaymentInitiation pixPaymentInitiation = new RichMessaging.PaymentInitiation();
pixPaymentInitiation.modeValue = RichMessaging.PaymentInitiationMode.ExternalPaymentApp;
RichMessaging.PaymentInitiationPix pixValue = new RichMessaging.PaymentInitiationPix();
pixValue.keyValue = '12345678901';
pixValue.keyTypeValue = RichMessaging.PaymentPixKeyType.Cpf;
pixValue.codeValue = '12345678901';
pixPaymentInitiation.pix = pixValue;
RichMessaging.PaymentInitiation boletoPaymentInitiation = new RichMessaging.PaymentInitiation();
boletoPaymentInitiation.modeValue = RichMessaging.PaymentInitiationMode.ExternalPaymentApp;
RichMessaging.PaymentInitiationBoleto boletoValue = new RichMessaging.PaymentInitiationBoleto();
boletoValue.digitableLine = '03399026944140000002628346101018898510000008848';
boletoPaymentInitiation.boleto = boletoValue;
List<RichMessaging.PaymentInitiation> options = new List<RichMessaging.PaymentInitiation>{
pixPaymentInitiation, boletoPaymentInitiation
};
result.add(options);
return result;
}
}

