Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

132
LINES

< > BotCompany Repo | #1030416 // Stripe.com Test [dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 4749K of libraries. Click here for Pure Java version (2044L/12K).

lib 1400447 // stripe
lib 1400448 // gson

import com.stripe.Stripe;
import com.stripe.model.checkout.Session;
import com.stripe.param.checkout.SessionCreateParams;

cmodule StripeTest > DynPrintLog {
  set flag NoNanoHTTPD.
  !include #1029545 // API for Eleu
  
  transient S YOUR_DOMAIN = "https://stripe-test.botcompany.de";
  transient S apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
  
  O html(IWebRequest req) ctex {
    // This is a sample test API key. Sign in to see examples pre-filled with your key.
    Stripe.apiKey = apiKey;

    if (eq(req.uri(), "/create-checkout-session")) {
      SessionCreateParams params =
        SessionCreateParams.builder()
          .addPaymentMethodType(SessionCreateParams.PaymentMethodType.CARD)
          .setMode(SessionCreateParams.Mode.PAYMENT)
          .setSuccessUrl(YOUR_DOMAIN + "/success.html")
          .setCancelUrl(YOUR_DOMAIN + "/cancel.html")
          .addLineItem(
            SessionCreateParams.LineItem.builder()
              .setQuantity(1L)
              .setPriceData(
                SessionCreateParams.LineItem.PriceData.builder()
                  .setCurrency("usd")
                  .setUnitAmount(2000L)
                  .setProductData(
                    SessionCreateParams.LineItem.PriceData.ProductData.builder()
                      .setName("Stubborn Attachments")
                      .build())
                  .build())
              .build())
          .build();
      Session session = Session.create(params);
      SS responseData = litmap(id := session.getId());
      ret subBot_serveJSON(responseData);
    }
    
    if (eq(req.uri(), "/")) ret [[
<html>
  <head>
    <title>Buy cool new product</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
    <script src="https://js.stripe.com/v3/"></script>
  </head>
  <body>
    <section>
      <div class="product">
        <img
          src="https://i.imgur.com/EHyR2nP.png"
          alt="The cover of Stubborn Attachments"
        />
        <div class="description">
          <h3>Stubborn Attachments</h3>
          <h5>$20.00</h5>
        </div>
      </div>
      <button id="checkout-button">Checkout</button>
    </section>
  </body>
  <script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("pk_test_TYooMQauvdEDq54NiTphI7jx");
    var checkoutButton = document.getElementById("checkout-button");

    checkoutButton.addEventListener("click", function () {
      fetch("/create-checkout-session", {
        method: "POST",
      })
        .then(function (response) {
          return response.json();
        })
        .then(function (session) {
          return stripe.redirectToCheckout({ sessionId: session.id });
        })
        .then(function (result) {
          // If redirectToCheckout fails due to a browser or network
          // error, you should display the localized error message to your
          // customer using error.message.
          if (result.error) {
            alert(result.error.message);
          }
        })
        .catch(function (error) {
          console.error("Error:", error);
        });
    });
  </script>
</html>
    ]];
    
    if (eq(req.uri(), "/success.html")) ret [[
<html>
<head>
  <title>Thanks for your order!</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <section>
    <p>
      We appreciate your business! If you have any questions, please email
      <a href="mailto:orders@example.com">orders@example.com</a>.
    </p>
  </section>
</body>
</html>
    ]];
    
    if (eq(req.uri(), "/cancel.html")) ret [[
<html>
<head>
  <title>Checkout canceled</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <section>
    <p>Forgot to add something to your cart? Shop around then come back to pay!</p>
  </section>
</body>
</html>
    ]];
    
    null;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030416
Snippet name: Stripe.com Test [dev.]
Eternal ID of this version: #1030416/10
Text MD5: b03a5a962ce736c2c22bf715c3a1d9ff
Transpilation MD5: 18ed6d95c3b97bed5e15e3d02bbb4aa7
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-13 23:55:31
Source code size: 4117 bytes / 132 lines
Pitched / IR pitched: No / No
Views / Downloads: 216 / 29819
Version history: 9 change(s)
Referenced in: [show references]