You are here:
Sample Code Snippets for Apex Classes
Use these Apex classes to define the parameters required for WhatsApp Pay integration.
Classes without country suffixes are common across all WhatsApp Pay integrations. Here are the sample Apex classes that you configure.
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. For more information, see PaymentLineItem Class.
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. For more information, see PaymentLineItem Class.
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. For more information, see OrderContext Class.
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 Handler - India
This class handles WhatsApp Pay India payments and payment status. For more informaiton, see ProcessPaymentStatusHandler Interface.
global class MyProcessPaymentStatusHandler implements Richmessaging.ProcessPaymentStatusHandler {
global RichMessaging.ProcessPaymentStatusResult processPaymentStatus(RichMessaging.ProcessPaymentStatusRequest request) {
// logic to process payment status
return new RichMessaging.ProcessPaymentStatusResult(true);
}
}
Payment Initiations - India
This class configures how the customer can pay using WhatsApp Pay India. WhatsApp Pay India supports Payment gateways, UPI, and Payment Links payment types. For more information see, Meta WhatsApp Payment - India and PaymentInitiation Class.
public class PaymentPaymentInitiationsGateway {
@InvocableMethod
public static List <
List <
RichMessaging.PaymentInitiation > >
getPaymentInitiation() {
List <List <RichMessaging.PaymentInitiation >>> result = new List <List <RichMessaging.PaymentInitiation>> ();
RichMessaging.PaymentInitiation gatewayPaymentInitiation = new RichMessaging.PaymentInitiation();
gatewayPaymentInitiation.modeValue = RichMessaging.PaymentInitiationMode.GatewayCheckout;
RichMessaging.PaymentInitiationGateway gatewayValue = new RichMessaging.PaymentInitiationGateway();
gatewayValue.paymentConfigurationNameValue = 'test-razor-pay';
gatewayValue.providerNameValue = 'razorpay';
List <RichMessaging.PaymentGatewayProperties> providerParametersValue = new List <RichMessaging.PaymentGatewayProperties> ();
RichMessaging.PaymentGatewayProperties gatewayProperties = new RichMessaging.PaymentGatewayProperties('key', 'value');
providerParametersValue.add(gatewayProperties);
gatewayValue.providerParametersValue = providerParametersValue;
gatewayPaymentInitiation.gateway = gatewayValue;
List <RichMessaging.PaymentInitiation> options = new List <RichMessaging.PaymentInitiation>{
gatewayPaymentInitiation
};
result.add(options);
return result;
}
}
Payment Initiations - Brazil
This class configures how the customer can pay using WhatsApp Pay Brazil. 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. For more information, see PaymentInitiation Class.
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;
}
}

