From e89caf7a0d376155d5f0f0e51ec69ec69706246a Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 25 Nov 2021 12:00:45 +0100 Subject: [PATCH] Add stripe as payment method --- .../concerns/checkout_callbacks.rb | 8 ++++++- app/views/split_checkout/_payment.html.haml | 2 +- .../split_checkout/payment/_stripe.html.haml | 23 +++++++++++++++++++ config/locales/en.yml | 4 ++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 app/views/split_checkout/payment/_stripe.html.haml diff --git a/app/controllers/concerns/checkout_callbacks.rb b/app/controllers/concerns/checkout_callbacks.rb index ff8d207add..ae9c2fb357 100644 --- a/app/controllers/concerns/checkout_callbacks.rb +++ b/app/controllers/concerns/checkout_callbacks.rb @@ -13,7 +13,7 @@ module CheckoutCallbacks prepend_before_action :require_order_cycle prepend_before_action :require_distributor_chosen - before_action :load_order, :associate_user, :load_saved_addresses + before_action :load_order, :associate_user, :load_saved_addresses, :load_saved_credit_cards before_action :load_shipping_methods, :load_countries, if: -> { params[:step] == "details" } before_action :ensure_order_not_completed @@ -41,6 +41,11 @@ module CheckoutCallbacks @order.ship_address ||= finder.ship_address end + def load_saved_credit_cards + @saved_credit_cards = spree_current_user ? spree_current_user.credit_cards.with_payment_profile.all : [] + @selected_card = nil + end + def load_shipping_methods @shipping_methods = Spree::ShippingMethod.for_distributor(@order.distributor).order(:name) end @@ -89,4 +94,5 @@ module CheckoutCallbacks def check_authorization authorize!(:edit, current_order, session[:access_token]) end + end diff --git a/app/views/split_checkout/_payment.html.haml b/app/views/split_checkout/_payment.html.haml index 0c4cf666d8..45333c5c9f 100644 --- a/app/views/split_checkout/_payment.html.haml +++ b/app/views/split_checkout/_payment.html.haml @@ -22,7 +22,7 @@ .paymentmethod-description.panel #{payment_method.description} .paymentmethod-form - - if payment_method.method_type == "gateway" + - if payment_method.method_type == "gateway" || payment_method.method_type == "stripe" = render partial: "split_checkout/payment/#{payment_method.method_type}", locals: { payment_method: payment_method, f: f } - else = render partial: "spree/checkout/payment/#{payment_method.method_type}", :locals => { payment_method: payment_method } diff --git a/app/views/split_checkout/payment/_stripe.html.haml b/app/views/split_checkout/payment/_stripe.html.haml new file mode 100644 index 0000000000..47f6b30f7e --- /dev/null +++ b/app/views/split_checkout/payment/_stripe.html.haml @@ -0,0 +1,23 @@ +- content_for :injection_data do + - if Stripe.publishable_key + :javascript + angular.module('Darkswarm').value("stripeObject", Stripe("#{Stripe.publishable_key}")) + +- if @saved_credit_cards.length > 0 + .checkout-input + %label + = t('split_checkout.step2.form.stripe.use_saved_card') + = select_tag :card, options_for_select(@saved_credit_cards.map {|cc| [ cc.id, "#{cc.brand} #{cc.number}" ] }, @selected_card) + +.checkout-input + - if @saved_credit_cards.length > 0 + %label + = t('split_checkout.step2.form.stripe.use_new_card') + %stripe-elements + + +- if spree_current_user + .checkout-input + = check_box_tag :save_card, {id: "save_card", name: "save_card", checked: false, value: "1"} + = label :save_card, t('split_checkout.step2.form.stripe.save_card'), { for: "save_card" } + \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 1b68988a48..de27c5c0c2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1737,6 +1737,10 @@ en: card_year: label: Year placeholder: 2020 + stripe: + use_saved_card: Use saved card + use_new_card: or use a new card + save_card: Save card for future use explaination: You can review and confirm your order in the next step which includes the final costs. submit: Next - Order summary cancel: Back to Your details