diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb new file mode 100644 index 0000000..4839e31 --- /dev/null +++ b/app/controllers/customers_controller.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module DiscoursePatrons + class CustomersController < ::ApplicationController + include DiscoursePatrons::Stripe + + before_action :set_api_key + + def create + customer = ::Stripe::Customer.create( + email: current_user.email, + source: params[:source] + ) + + render_json_dump customer + end + end +end diff --git a/assets/javascripts/discourse/components/subscribe-card.js.es6 b/assets/javascripts/discourse/components/subscribe-card.js.es6 new file mode 100644 index 0000000..d773d59 --- /dev/null +++ b/assets/javascripts/discourse/components/subscribe-card.js.es6 @@ -0,0 +1,7 @@ +export default Ember.Component.extend({ + didInsertElement() { + this._super(...arguments); + this.cardElement.mount("#card-element"); + }, + didDestroyElement() {} +}); diff --git a/assets/javascripts/discourse/controllers/patrons-index.js.es6 b/assets/javascripts/discourse/controllers/patrons-index.js.es6 index ad83561..803e911 100644 --- a/assets/javascripts/discourse/controllers/patrons-index.js.es6 +++ b/assets/javascripts/discourse/controllers/patrons-index.js.es6 @@ -13,7 +13,7 @@ export default Ember.Controller.extend({ }, paymentSuccessHandler(paymentIntentId) { - DiscourseURL.redirectTo(`patrons/${paymentIntentId}`); + // DiscourseURL.redirectTo(`patrons/${paymentIntentId}`); } } }); diff --git a/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 b/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 index 8e16cae..caac906 100644 --- a/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 +++ b/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 @@ -1,11 +1,37 @@ -export default Ember.Controller.extend({ - actions: { - stripePaymentHandler(/* data */) { - // console.log('stripePaymentHandler', data); - }, +import { ajax } from "discourse/lib/ajax"; - paymentSuccessHandler(/* paymentIntentId */) { - // console.log('paymentSuccessHandler'); +export default Ember.Controller.extend({ + init() { + this._super(...arguments); + this.set( + "stripe", + Stripe(Discourse.SiteSettings.discourse_patrons_public_key) + ); + const elements = this.get("stripe").elements(); + this.set("cardElement", elements.create("card", { hidePostalCode: true })); + }, + + actions: { + stripePaymentHandler() { + // https://stripe.com/docs/billing/subscriptions/payment#signup-flow + + this.stripe.createToken(this.get("cardElement")).then(result => { + if (result.error) { + // Inform the customer that there was an error. + // var errorElement = document.getElementById('card-errors'); + // errorElement.textContent = result.error.message; + } else { + const data = { + source: result.token.id + }; + + return ajax("/patrons/customers", { method: "post", data }).then( + result => { + // create subscription + } + ); + } + }); } } }); diff --git a/assets/javascripts/discourse/templates/components/subscribe-card.hbs b/assets/javascripts/discourse/templates/components/subscribe-card.hbs new file mode 100644 index 0000000..f2afe6e --- /dev/null +++ b/assets/javascripts/discourse/templates/components/subscribe-card.hbs @@ -0,0 +1,2 @@ + +
diff --git a/assets/javascripts/discourse/templates/patrons/subscribe.hbs b/assets/javascripts/discourse/templates/patrons/subscribe.hbs index 940fe65..1562aed 100644 --- a/assets/javascripts/discourse/templates/patrons/subscribe.hbs +++ b/assets/javascripts/discourse/templates/patrons/subscribe.hbs @@ -13,8 +13,22 @@