How to Build a Food...
December 18, 2024
There are mainly two ways to implement stripe payment integration
1. Prebuit Payment Page
2. Custom Payment flow
Note: In this integration, we will be creating objects, one service Wrapper module of UserLocalService, and one rest builder module.
Liferay User Id | |
Stripe Customer Id | |
Paypal Customer Id | |
Type | Calander Event, Chat |
Calander Event Relationship | Calander Event Id |
Site Id |
Adult Price | |
Adult Quantity | |
Children Price | |
Children Quantity | |
Guest Price | |
Guest Quantity | |
Tax Amount | |
Total Amount | |
Payment Status | Failed, Cancelled, Succeeded, Require Action, |
Transaction Id | |
Calander Event ID | |
Site Id |
private void stripeCustomerCrud(User user, String operationType) {
// If operation type is not equal to create, update, or delete, give an error
if (!Arrays.asList(CREATE_OP, UPDATE_OP, DELETE_OP).contains(operationType)) {
log.error(“Operations must be in Create, Update, and Delete. Your choice is: ” + operationType + ” operation.”);
return;
}
try {
Stripe.apiKey = “your api key”;
String stripeCustomerId = “”;
// Get Stripe Customer ID from the user custom field when operation equals to update or delete
if (operationType.equals(UPDATE_OP) || operationType.equals(DELETE_OP)) {
ExpandoBridge expandoBridge = user.getExpandoBridge();
stripeCustomerId = (String) expandoBridge.getAttribute(STRIPE_CUST_ID);
if (stripeCustomerId == null || stripeCustomerId.isEmpty()) {
throw new NullPointerException(“Stripe Customer Id is empty”);
}
}
Map<String, Object> customerParams = new HashMap<>();
// Add name, email, and metadata in the map when operation equals to create or update
if (!operationType.equals(DELETE_OP)) {
Map<String, String> metadataParams = new HashMap<>();
metadataParams.put(“liferayUserId”, String.valueOf(user.getUserId()));
customerParams.put(“name”, user.getFullName());
customerParams.put(“email”, user.getEmailAddress());
customerParams.put(“metadata”, metadataParams);
}
Customer customer = null;
// Operation-wise call a method of the stripe SDK
if (operationType.equals(CREATE_OP)) {
customer = Customer.create(customerParams);
setExpando(user.getUserId(), STRIPE_CUST_ID, customer.getId());
} else if (operationType.equals(UPDATE_OP)) {
Customer existingCustomer = Customer.retrieve(stripeCustomerId);
customer = existingCustomer.update(customerParams);
} else if (operationType.equals(DELETE_OP)) {
Customer existingCustomer = Customer.retrieve(stripeCustomerId);
customer = existingCustomer.delete();
}
log.info(operationType + ” operation is performed on the Stripe customer. (Data = Id: ” + customer.getId() + “, ” +
“Name: ” + customer.getName() + “, Email: ” + customer.getEmail() + “)”);
} catch (NullPointerException e) {
log.error(“Site custom field does not exist or is empty for the Stripe module: ” + e.getMessage());
} catch (StripeException e) {
log.error(“Stripe Exception while performing ” + operationType + ” operation: ” + e.getMessage());
}
}
NOTE: Create Customer in the stripe will call in two scenarios
{
“amount”: 123400,
“amount_capturable”: 0,
“amount_received”: 0,
“capture_method”: “automatic”,
“client_secret”: “pi_3NPlCJSIEK4dUWZs1S8JVSoS_secret_qNlSX4NGn4NVMCcTk8xj9aDcv”,
“confirmation_method”: “automatic”,
“created”: 1688384739,
“currency”: “inr”,
“customer”: “cus_OC8SsIBxziFMm9”,
“id”: “pi_3NPlCJSIEK4dUWZs1S8JVSoS”,
“object”: “payment_intent”,
“payment_method_types”: [
“card”
]
}
Note: Created a rest builder module and created custom apis create-payment-intent, webhook
@POST
@Path(“/create-payment-intent”)
@Produces(MediaType.APPLICATION_JSON)
public Response createPaymentIntent(@RequestBody PaymentDetailsDTO paymentDetailsDTO) {
return userRegistrationService.createPaymentIntent(paymentDetailsDTO);
}
public Response createPaymentIntent(PaymentDetailsDTO paymentDetailsDTO) {
try {
Stripe.apiKey = “custom key for user”;
/* Multiply 100 with the amount because Stripe converts the amount from the standard currency unit
to the smallest unit (cents or pence) used by Stripe. */
long amount = paymentDetailsDTO.getAmount() * 100;
// Set data in the PaymentIntent Object
PaymentIntentCreateParams params =
PaymentIntentCreateParams
.builder()
.setCustomer(paymentDetailsDTO.getCustomerId())
.setSetupFutureUsage(PaymentIntentCreateParams.SetupFutureUsage.OFF_SESSION)
.setAmount(amount)
.putMetadata(“Liferay Calendar Event Id”, String.valueOf(paymentDetailsDTO.getCalendarEventId()))
.putMetadata(“Liferay User Id”, String.valueOf(paymentDetailsDTO.getLiferayUserId()))
.setCurrency(paymentDetailsDTO.getCurrency())
.build();
return Response.ok(new ResponseDTO(Response.Status.OK.getStatusCode(), “Payment Intent Created Successfully”, setPaymentIntentToDTO(PaymentIntent.create(params)))).build();
} catch (Exception e) {
log.error(e.getMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new ResponseDTO(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), “Payment Intent is not Created”, null)).build();
}
}
APIs wise Flow for the Stripe payment integration
NOTE: Only card payment is available.
Create Payment Intent API
Get User Payment Detail Id if Not Prent Then Add
Integrating Stripe payment functionality with Liferay Development has proven to be a seamless and efficient solution for businesses seeking to streamline their online payment processes. By implementing this integration, businesses can offer their customers a secure and convenient payment experience, leading to increased customer satisfaction and loyalty.