Files
discourse-subscriptions/assets/javascripts/discourse/controllers/user-billing-subscriptions-card.js
T
Faizaan GaganandBlake Erickson 945af4f140 FIX: Allow user to update card details for recurring subscriptions (#123)
* add new route for card update

* create backend route

* update label

* basic functionality working

* ran rubocop

* added rspec tests for functionality

* make payment_method param compulsory

* fixed js linting

* improve client side error handling

* improve server side error handling

* improved update card page UI

* improve button UI for user subscriptions page

* give feedback to user about save status

* remove heading from last column

* fix padding on edit/delete buttons for update table

Co-authored-by: Blake Erickson <[email protected]>
2022-07-05 19:53:27 -06:00

57 lines
1.4 KiB
JavaScript

import Controller from "@ember/controller";
import { action } from "@ember/object";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import I18n from "I18n";
import bootbox from "bootbox";
export default Controller.extend({
loading: false,
saved: false,
init() {
this._super(...arguments);
this.set(
"stripe",
Stripe(this.siteSettings.discourse_subscriptions_public_key)
);
const elements = this.get("stripe").elements();
this.set("cardElement", elements.create("card", { hidePostalCode: true }));
},
@action
async updatePaymentMethod() {
this.set("loading", true);
this.set("saved", false);
const paymentMethodObject = await this.stripe.createPaymentMethod({
type: "card",
card: this.cardElement,
});
if (paymentMethodObject.error) {
bootbox.alert(
paymentMethodObject.error?.message || I18n.t("generic_error")
);
this.set("loading", false);
return;
}
const paymentMethod = paymentMethodObject.paymentMethod.id;
try {
await ajax(`/s/user/subscriptions/${this.model}`, {
method: "PUT",
data: {
payment_method: paymentMethod,
},
});
this.set("saved", true);
} catch (err) {
popupAjaxError(err);
} finally {
this.set("loading", false);
this.cardElement?.clear();
}
},
});