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).

1  
lib 1400447 // stripe
2  
lib 1400448 // gson
3  
4  
import com.stripe.Stripe;
5  
import com.stripe.model.checkout.Session;
6  
import com.stripe.param.checkout.SessionCreateParams;
7  
8  
cmodule StripeTest > DynPrintLog {
9  
  set flag NoNanoHTTPD.
10  
  !include #1029545 // API for Eleu
11  
  
12  
  transient S YOUR_DOMAIN = "https://stripe-test.botcompany.de";
13  
  transient S apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
14  
  
15  
  O html(IWebRequest req) ctex {
16  
    // This is a sample test API key. Sign in to see examples pre-filled with your key.
17  
    Stripe.apiKey = apiKey;
18  
19  
    if (eq(req.uri(), "/create-checkout-session")) {
20  
      SessionCreateParams params =
21  
        SessionCreateParams.builder()
22  
          .addPaymentMethodType(SessionCreateParams.PaymentMethodType.CARD)
23  
          .setMode(SessionCreateParams.Mode.PAYMENT)
24  
          .setSuccessUrl(YOUR_DOMAIN + "/success.html")
25  
          .setCancelUrl(YOUR_DOMAIN + "/cancel.html")
26  
          .addLineItem(
27  
            SessionCreateParams.LineItem.builder()
28  
              .setQuantity(1L)
29  
              .setPriceData(
30  
                SessionCreateParams.LineItem.PriceData.builder()
31  
                  .setCurrency("usd")
32  
                  .setUnitAmount(2000L)
33  
                  .setProductData(
34  
                    SessionCreateParams.LineItem.PriceData.ProductData.builder()
35  
                      .setName("Stubborn Attachments")
36  
                      .build())
37  
                  .build())
38  
              .build())
39  
          .build();
40  
      Session session = Session.create(params);
41  
      SS responseData = litmap(id := session.getId());
42  
      ret subBot_serveJSON(responseData);
43  
    }
44  
    
45  
    if (eq(req.uri(), "/")) ret [[
46  
<html>
47  
  <head>
48  
    <title>Buy cool new product</title>
49  
    <link rel="stylesheet" href="style.css">
50  
    <script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
51  
    <script src="https://js.stripe.com/v3/"></script>
52  
  </head>
53  
  <body>
54  
    <section>
55  
      <div class="product">
56  
        <img
57  
          src="https://i.imgur.com/EHyR2nP.png"
58  
          alt="The cover of Stubborn Attachments"
59  
        />
60  
        <div class="description">
61  
          <h3>Stubborn Attachments</h3>
62  
          <h5>$20.00</h5>
63  
        </div>
64  
      </div>
65  
      <button id="checkout-button">Checkout</button>
66  
    </section>
67  
  </body>
68  
  <script type="text/javascript">
69  
    // Create an instance of the Stripe object with your publishable API key
70  
    var stripe = Stripe("pk_test_TYooMQauvdEDq54NiTphI7jx");
71  
    var checkoutButton = document.getElementById("checkout-button");
72  
73  
    checkoutButton.addEventListener("click", function () {
74  
      fetch("/create-checkout-session", {
75  
        method: "POST",
76  
      })
77  
        .then(function (response) {
78  
          return response.json();
79  
        })
80  
        .then(function (session) {
81  
          return stripe.redirectToCheckout({ sessionId: session.id });
82  
        })
83  
        .then(function (result) {
84  
          // If redirectToCheckout fails due to a browser or network
85  
          // error, you should display the localized error message to your
86  
          // customer using error.message.
87  
          if (result.error) {
88  
            alert(result.error.message);
89  
          }
90  
        })
91  
        .catch(function (error) {
92  
          console.error("Error:", error);
93  
        });
94  
    });
95  
  </script>
96  
</html>
97  
    ]];
98  
    
99  
    if (eq(req.uri(), "/success.html")) ret [[
100  
<html>
101  
<head>
102  
  <title>Thanks for your order!</title>
103  
  <link rel="stylesheet" href="style.css">
104  
</head>
105  
<body>
106  
  <section>
107  
    <p>
108  
      We appreciate your business! If you have any questions, please email
109  
      <a href="mailto:orders@example.com">orders@example.com</a>.
110  
    </p>
111  
  </section>
112  
</body>
113  
</html>
114  
    ]];
115  
    
116  
    if (eq(req.uri(), "/cancel.html")) ret [[
117  
<html>
118  
<head>
119  
  <title>Checkout canceled</title>
120  
  <link rel="stylesheet" href="style.css">
121  
</head>
122  
<body>
123  
  <section>
124  
    <p>Forgot to add something to your cart? Shop around then come back to pay!</p>
125  
  </section>
126  
</body>
127  
</html>
128  
    ]];
129  
    
130  
    null;
131  
  }
132  
}

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: 218 / 29823
Version history: 9 change(s)
Referenced in: [show references]