Skip to content

Commit

Permalink
MARP-954 Redirect to another pages
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy committed Jan 8, 2025
1 parent 91774b6 commit 568a2f0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.axonivy.connector.stripe.controller;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.axonivy.connector.stripe.service.PaymentService;
import com.stripe.model.checkout.Session;
import com.stripe.param.checkout.SessionCreateParams;

@Path("/stripe")
public class StripeApiEndpoint {
// @POST
// @Path("/create-checkout-session")
// @Produces("application/json")
// public Response createCheckoutSession() {
// try {
// SessionCreateParams params = SessionCreateParams.builder().setMode(SessionCreateParams.Mode.PAYMENT)
// .setSuccessUrl("https://yourdomain.com/success").setCancelUrl("https://yourdomain.com/cancel")
// .addLineItem(SessionCreateParams.LineItem
// .builder().setQuantity(
// 1L)
// .setPriceData(SessionCreateParams.LineItem.PriceData.builder().setCurrency("usd")
// .setUnitAmount(2000L) // Amount in cents
// .setProductData(SessionCreateParams.LineItem.PriceData.ProductData.builder()
// .setName("Product Name").build())
// .build())
// .build())
// .build();
//
// Session session = Session.create(params);
// return Response.ok("{\"id\":\"" + session.getId() + "\"}").build();
// } catch (Exception e) {
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
// }
// }

@POST
@Path("/create-checkout-session/{priceId}/{quantity}")
@Produces("application/json")
public Response createCheckoutSession(@PathParam("priceId") String priceId, @PathParam("quantity") Long quantity) {
try {
Session session = PaymentService.getInstance().createCheckoutSession(priceId, quantity);
return Response.ok("{\"id\":\"" + session.getId() + "\"}").build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class CreateCheckoutSessionBean {
public void onSendRequest() throws Exception {
Ivy.log().warn("priceId: " + priceId);
Ivy.log().warn("quantity: " + quantity);
checkoutSessionUrl = PaymentService.getInstance().createCheckoutSession(priceId, quantity);
Ivy.log().warn(checkoutSessionUrl);
// checkoutSessionUrl = PaymentService.getInstance().createCheckoutSession(priceId, quantity);
// Ivy.log().warn(checkoutSessionUrl);
}

public String getPriceId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:ic="http://ivyteam.ch/jsf/component"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions">

<h:body>
<ui:composition template="/layouts/frame-10-full-width.xhtml">
<ui:param name="managedBean" value="#{createCheckoutSessionBean}" />
Expand All @@ -30,15 +31,37 @@
<div class="command-btns">
<p:commandLink id="cancel" actionListener="#{logic.close}"
value="Cancel" immediate="true" />
<p:commandButton id="asd" widgetVar="btn"
actionListener="#{managedBean.onSendRequest()}" value="Create"
update="form paymentFrame" icon="pi pi-check" />
<p:commandButton id="resquest-button" widgetVar="btn"
process="@form" onclick="startCheckout()" value="Create"
update="form" icon="pi pi-check" />
</div>
</h:form>

<p:outputPanel id="paymentFrame">
<iframe src="#{managedBean.checkoutSessionUrl}" width="100%" height="600px"></iframe>
</p:outputPanel>
<script src="https://js.stripe.com/v3/"></script>
<script type="text/javascript">
function startCheckout() {
const priceId = document.getElementById('form:price').value;
const quantity = document.getElementById('form:quantity').value;
const url = "/designer/api/stripe/create-checkout-session/" + priceId + "/" + quantity;
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' , 'X-Requested-By': 'ivy'}
})
.then(response => response.json())
.then(data => {
if (data.id) {
console.log('asdasdadsadsdas');
const stripe = Stripe('pk_test_51Qe9v3LaeAomYD3LNDn8V1WzZSn6HSx8AHVeKxq8ZGee5Q8ONKDGsrL6hXLFj33gvzl080xMcdhQXMS4fDzipJeX00Fe0BdnpK'); // Replace with your Stripe Publishable Key
stripe.redirectToCheckout({ sessionId: data.id });
} else {
alert('Error creating checkout session');
}
})
.catch(error => {
console.error('Error:', error);
alert('Failed to create checkout session');
});
}
</script>
</ui:define>
</ui:composition>
</h:body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public PaymentLink deactivatePaymentLink(String paymentLinkId) throws StripeExce
return resource.update(params);
}

public String createCheckoutSession(String priceId, Long quantity) throws Exception {
public Session createCheckoutSession(String priceId, Long quantity) throws Exception {
Price price = Price.retrieve(priceId);
String successUrl = Ivy.request().getRequestPath().concat("page/stripe-connector$1/NewFile.jsp");

Expand All @@ -73,8 +73,7 @@ public String createCheckoutSession(String priceId, Long quantity) throws Except
.addLineItem(SessionCreateParams.LineItem.builder().setPrice(priceId).setQuantity(quantity).build())
.build();

Session session = Session.create(params);
return session.getUrl();
return Session.create(params);
}

private List<SessionCreateParams.PaymentMethodType> getSupportedPaymentMethodTypes(Price price) {
Expand Down

0 comments on commit 568a2f0

Please sign in to comment.