From e9c596de66fa3720eafc25fff703f6d2b336d983 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 24 Dec 2020 20:03:46 +0530 Subject: [PATCH] Test PaymentIntent for payment of generic onetime products --- datacenterlight/views.py | 23 +++++++++++------------ hosting/static/hosting/js/payment.js | 23 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index cd1de47d..bc586ec2 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -291,23 +291,22 @@ class PaymentOrderView(FormView): payment_intent_response = stripe_utils.get_payment_intent( int(product.get_actual_price() * 100) ) - if not payment_intent_response.get('response_object'): + payment_intent = payment_intent_response.get('response_object') + if not payment_intent: logger.error("Could not create payment_intent %s" % str(payment_intent_response)) else: - logger.debug("*******") - logger.debug( - "payment_intent_obj = %s" % - str(payment_intent_response.get('response_object'))) - logger.debug("*******") + logger.debug("payment_intent_obj = %s" % + str(payment_intent)) context.update({'generic_payment_form': ProductPaymentForm( prefix='generic_payment_form', - initial={'product_name': product.product_name, - 'amount': float(product.get_actual_price()), - 'recurring': product.product_is_subscription, - 'description': product.product_description, - 'payment_intent_secret': 'secret_here' - }, + initial={ + 'product_name': product.product_name, + 'amount': float(product.get_actual_price()), + 'recurring': product.product_is_subscription, + 'description': product.product_description, + 'payment_intent_secret': payment_intent.client_secret + }, product_id=product.id ), }) else: diff --git a/hosting/static/hosting/js/payment.js b/hosting/static/hosting/js/payment.js index be94a866..933e15df 100644 --- a/hosting/static/hosting/js/payment.js +++ b/hosting/static/hosting/js/payment.js @@ -178,7 +178,28 @@ $(document).ready(function () { } var $form_new = $('#payment-form-new'); - $form_new.submit(payWithStripe_new); + $form_new.submit(payWithPaymentIntent); + function payWithPaymentIntent(e) { + e.preventDefault(); + + stripe.confirmCardPayment( + window.paymentIntentSecret, + { + payment_method: {card: cardNumberElement} + } + ).then(function(result) { + if (result.error) { + // Display error.message in your UI. + var errorElement = document.getElementById('card-errors'); + errorElement.textContent = result.error.message; + } else { + // The payment has succeeded + // Display a success message + alert("Thanks for the order. Your product will be provisioned " + + "as soon as we receive the payment. Thank you."); + } + }); + } function payWithStripe_new(e) { e.preventDefault();