From 50ca174e183783a4f1c22632d499250d39576725 Mon Sep 17 00:00:00 2001 From: Adrienne Dreyfus Date: Tue, 19 Nov 2019 08:55:39 -0800 Subject: [PATCH] Added better success page --- client/success.html | 19 ++++++++++++++++--- .../main/java/com/stripe/sample/Server.java | 14 ++++++++++++-- server/node/server.js | 15 ++++++++++----- server/php-slim/index.php | 12 +++++++++--- server/php/public/checkout-session.php | 9 +++++++++ server/php/public/create-checkout-session.php | 4 ++-- server/php/public/success.html | 17 +++++++++++++++-- server/python/server.py | 16 +++++++++++++--- server/ruby/server.rb | 15 +++++++++++++-- 9 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 server/php/public/checkout-session.php diff --git a/client/success.html b/client/success.html index ff35336..41f526d 100644 --- a/client/success.html +++ b/client/success.html @@ -7,7 +7,6 @@ - @@ -19,10 +18,10 @@

Your test payment succeeded!

- View PaymentIntent response: + View CheckoutSession response:

- + diff --git a/server/java/src/main/java/com/stripe/sample/Server.java b/server/java/src/main/java/com/stripe/sample/Server.java index 177619c..d422f8e 100644 --- a/server/java/src/main/java/com/stripe/sample/Server.java +++ b/server/java/src/main/java/com/stripe/sample/Server.java @@ -42,7 +42,7 @@ public Boolean getIsBuyingSticker() { public static void main(String[] args) { port(4242); - + Dotenv dotenv = Dotenv.load(); Stripe.apiKey = dotenv.get("STRIPE_SECRET_KEY"); @@ -58,6 +58,16 @@ public static void main(String[] args) { return gson.toJson(responseData); }); + // Fetch the Checkout Session to display the JSON result on the success page + get("/checkout-session", (request, response) -> { + response.type("application/json"); + + String sessionId = request.queryParams("sessionId"); + Session session = Session.retrieve(sessionId); + + return gson.toJson(session); + }); + post("/create-checkout-session", (request, response) -> { response.type("application/json"); PostBody postBody = gson.fromJson(request.body(), PostBody.class); @@ -70,7 +80,7 @@ public static void main(String[] args) { SubscriptionData.Item plan = new SubscriptionData.Item.Builder().setPlan(planId).build(); SubscriptionData subscriptionData = new SubscriptionData.Builder().addItem(plan).build(); - builder.setSuccessUrl(domainUrl + "/success.html").setCancelUrl(domainUrl + "/cancel.html") + builder.setSuccessUrl(domainUrl + "/success.html?session_id={CHECKOUT_SESSION_ID}").setCancelUrl(domainUrl + "/cancel.html") .setSubscriptionData(subscriptionData).addPaymentMethodType(PaymentMethodType.CARD); if (postBody.getIsBuyingSticker()) { diff --git a/server/node/server.js b/server/node/server.js index 7376591..45a3e01 100644 --- a/server/node/server.js +++ b/server/node/server.js @@ -23,6 +23,13 @@ app.get("/", (req, res) => { res.sendFile(path); }); +// Fetch the Checkout Session to display the JSON result on the success page +app.get("/checkout-session", async (req, res) => { + const { sessionId } = req.query; + const session = await stripe.checkout.sessions.retrieve(sessionId); + res.send(session); +}); + app.post("/create-checkout-session", async (req, res) => { const planId = process.env.SUBSCRIPTION_PLAN_ID; const domainURL = process.env.DOMAIN; @@ -48,7 +55,7 @@ app.post("/create-checkout-session", async (req, res) => { } ] }, - success_url: `${domainURL}/success.html`, + success_url: `${domainURL}/success.html?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${domainURL}/cancel.html` }); } else { @@ -62,7 +69,7 @@ app.post("/create-checkout-session", async (req, res) => { } ] }, - success_url: `${domainURL}/success.html`, + success_url: `${domainURL}/success.html?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${domainURL}/cancel.html` }); } @@ -118,9 +125,7 @@ app.post("/webhook", async (req, res) => { items[0].custom.name === "Pasha e-book" ) { console.log( - `🔔 Customer is subscribed and bought an e-book! Send the e-book to ${ - customer.email - }.` + `🔔 Customer is subscribed and bought an e-book! Send the e-book to ${customer.email}.` ); } else { console.log(`🔔 Customer is subscribed but did not buy an e-book.`); diff --git a/server/php-slim/index.php b/server/php-slim/index.php index b14fc38..5b00a77 100644 --- a/server/php-slim/index.php +++ b/server/php-slim/index.php @@ -26,7 +26,6 @@ Stripe::setApiKey(getenv('STRIPE_SECRET_KEY')); return $next($request, $response); }); - $app->get('/', function (Request $request, Response $response, array $args) { // Display checkout page @@ -38,6 +37,13 @@ return $response->withJson([ 'publicKey' => $pub_key ]); }); +$app->get('/checkout-session', function (Request $request, Response $response, array $args) { + $id = $request->getQueryParams()['sessionId']; + $checkout_session = \Stripe\Checkout\Session::retrieve($id); + + return $response->withJson($checkout_session); +}); + $app->post('/create-checkout-session', function(Request $request, Response $response, array $args) { $domain_url = getenv('DOMAIN'); $plan_id = getenv('SUBSCRIPTION_PLAN_ID'); @@ -46,7 +52,7 @@ if($body->isBuyingSticker) { // Customer is signing up for a subscription and purchasing the extra e-book $checkout_session = \Stripe\Checkout\Session::create([ - 'success_url' => $domain_url . '/success.html', + 'success_url' => $domain_url . '/success.html?session_id={CHECKOUT_SESSION_ID}', 'cancel_url' => $domain_url . '/cancel.html', 'payment_method_types' => ['card'], 'subscription_data' => [ @@ -64,7 +70,7 @@ } else { // Customer is only signing up for a subscription $checkout_session = \Stripe\Checkout\Session::create([ - 'success_url' => $domain_url . '/success.html', + 'success_url' => $domain_url . '/success.html?session_id={CHECKOUT_SESSION_ID}', 'cancel_url' => $domain_url . '/cancel.html', 'payment_method_types' => ['card'], 'subscription_data' => [ diff --git a/server/php/public/checkout-session.php b/server/php/public/checkout-session.php new file mode 100644 index 0000000..d5ee956 --- /dev/null +++ b/server/php/public/checkout-session.php @@ -0,0 +1,9 @@ +isBuyingSticker) { // Customer is signing up for a subscription and purchasing the extra e-book $checkout_session = \Stripe\Checkout\Session::create([ - 'success_url' => $domain_url . '/success.html', + 'success_url' => $domain_url . '/success.html?session_id={CHECKOUT_SESSION_ID}', 'cancel_url' => $domain_url . '/cancel.html', 'payment_method_types' => ['card'], 'subscription_data' => [ @@ -26,7 +26,7 @@ } else { // Customer is only signing up for a subscription $checkout_session = \Stripe\Checkout\Session::create([ - 'success_url' => $domain_url . '/success.html', + 'success_url' => $domain_url . '/success.html?session_id={CHECKOUT_SESSION_ID}', 'cancel_url' => $domain_url . '/cancel.html', 'payment_method_types' => ['card'], 'subscription_data' => [ diff --git a/server/php/public/success.html b/server/php/public/success.html index ff35336..336f94f 100644 --- a/server/php/public/success.html +++ b/server/php/public/success.html @@ -7,7 +7,6 @@ - @@ -22,7 +21,7 @@

View PaymentIntent response:

- + diff --git a/server/python/server.py b/server/python/server.py index b057254..516a804 100644 --- a/server/python/server.py +++ b/server/python/server.py @@ -18,7 +18,8 @@ stripe.api_key = os.getenv('STRIPE_SECRET_KEY') stripe.api_version = os.getenv('STRIPE_API_VERSION') -static_dir = str(os.path.abspath(os.path.join(__file__ , "..", os.getenv("STATIC_DIR")))) +static_dir = str(os.path.abspath(os.path.join( + __file__, "..", os.getenv("STATIC_DIR")))) app = Flask(__name__, static_folder=static_dir, static_url_path="", template_folder=static_dir) @@ -32,6 +33,13 @@ def get_example(): def get_publishable_key(): return jsonify({'publicKey': os.getenv('STRIPE_PUBLISHABLE_KEY')}) +# Fetch the Checkout Session to display the JSON result on the success page +@app.route('/checkout-session', methods=['GET']) +def get_checkout_session(): + id = request.args.get('sessionId') + checkout_session = stripe.checkout.Session.retrieve(id) + return jsonify(checkout_session) + @app.route('/create-checkout-session', methods=['POST']) def create_checkout_session(): @@ -43,7 +51,8 @@ def create_checkout_session(): if data['isBuyingSticker']: # Customer is signing up for a subscription and purchasing the extra e-book checkout_session = stripe.checkout.Session.create( - success_url=domain_url + "/success.html", + success_url=domain_url + + "/success.html?session_id={CHECKOUT_SESSION_ID}", cancel_url=domain_url + "/cancel.html", payment_method_types=["card"], subscription_data={"items": [{"plan": plan_id}]}, @@ -59,7 +68,8 @@ def create_checkout_session(): else: # Customer is only signing up for a subscription checkout_session = stripe.checkout.Session.create( - success_url=domain_url + "/success.html", + success_url=domain_url + + "/success.html?session_id={CHECKOUT_SESSION_ID}", cancel_url=domain_url + "/cancel.html", payment_method_types=["card"], subscription_data={"items": [{"plan": plan_id}]}, diff --git a/server/ruby/server.rb b/server/ruby/server.rb index 1e1514e..93140a5 100644 --- a/server/ruby/server.rb +++ b/server/ruby/server.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'stripe' require 'sinatra' require 'dotenv' @@ -26,7 +28,7 @@ def create_checkout_session(is_buying_sticker, plan_id, domain_url) checkout_session = if is_buying_sticker # Customer is signing up for a subscription and purchasing the extra e-book Stripe::Checkout::Session.create( - success_url: domain_url + '/success.html', + success_url: ENV['DOMAIN'] + '/success.html?session_id={CHECKOUT_SESSION_ID}', cancel_url: domain_url + '/cancel.html', payment_method_types: ['card'], subscription_data: { @@ -42,7 +44,7 @@ def create_checkout_session(is_buying_sticker, plan_id, domain_url) else # Customer is only signing up for a subscription Stripe::Checkout::Session.create( - success_url: domain_url + '/success.html', + success_url: ENV['DOMAIN'] + '/success.html?session_id={CHECKOUT_SESSION_ID}', cancel_url: domain_url + '/cancel.html', payment_method_types: ['card'], subscription_data: { @@ -63,6 +65,15 @@ def create_checkout_session(is_buying_sticker, plan_id, domain_url) }.to_json end +# Fetch the Checkout Session to display the JSON result on the success page +get '/checkout-session' do + content_type 'application/json' + session_id = params[:sessionId] + + session = Stripe::Checkout::Session.retrieve(session_id) + session.to_json +end + post '/webhook' do # You can use webhooks to receive information about asynchronous payment events. # For more about our webhook events check out https://stripe.com/docs/webhooks.