This repository was archived by the owner on Jul 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Fully working GPay/ApplePay implementation #157
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
assets/javascripts/discourse/components/subscribe-payments.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import Transaction from "discourse/plugins/discourse-subscriptions/discourse/models/transaction"; | ||
| import Subscription from "discourse/plugins/discourse-subscriptions/discourse/models/subscription"; | ||
| import Component from "@ember/component"; | ||
| import { observes } from "discourse-common/utils/decorators"; | ||
| import { inject as service } from "@ember/service"; | ||
|
|
||
| export default Component.extend({ | ||
| dialog: service(), | ||
|
|
||
| @observes("selectedPlan", "plans") | ||
| setupButtonElement() { | ||
| const plan = this.plans | ||
| .filterBy("id", this.selectedPlan) | ||
| .get("firstObject"); | ||
| console.log("plan") | ||
| console.log(plan) | ||
|
|
||
| if (!plan) { | ||
| this.alert("plans.validate.payment_options.required"); | ||
| return; | ||
| } | ||
|
|
||
| if (this.selectedPlan) { | ||
| const elements = this.stripe.elements(); | ||
| const paymentRequest = this.stripe.paymentRequest({ | ||
| currency: "usd", | ||
| country: "US", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should country and currency be hardcoded? |
||
| requestPayerName: true, | ||
| requestPayerEmail: true, | ||
| total: { | ||
| label: plan.subscriptionRate, | ||
| amount: plan.unit_amount, | ||
| }, | ||
| }); | ||
| this.set("buttonElement", | ||
| elements.create('paymentRequestButton', { | ||
| paymentRequest: paymentRequest, | ||
| }) | ||
| ); | ||
| this.set("paymentRequest", paymentRequest); | ||
| } | ||
|
|
||
| this.paymentRequest.canMakePayment().then((result) => { | ||
| if (result) { | ||
| // mount the element | ||
| this.buttonElement.mount("#payment-request-button"); | ||
| } else { | ||
| //hide the button | ||
| // document.getElementById("payment-request-button").style.display = "none"; | ||
| console.log("GooglePay and ApplePay is unvailable"); | ||
| } | ||
| }); | ||
|
|
||
| this.paymentRequest.on('token', (result) => { | ||
| console.log("this.paymentRequest.on('token', (result)", result); | ||
| const subscription = Subscription.create({ | ||
| source: result.token.id, | ||
| plan: plan.get("id"), | ||
| promo: this.promoCode, | ||
| }); | ||
|
|
||
| console.log("subscription", subscription); | ||
| console.log("tokenid",result.token.id); | ||
| console.log("planid",plan.get("id")); | ||
| console.log("promocode",this.promoCode); | ||
|
|
||
|
|
||
| subscription.save().then(save => { | ||
| console.log("on subscription.save() Result: ", save); | ||
| if (save.error) { | ||
| this.dialog.alert(save.error.message || save.error); | ||
| console.log("ERROR"); | ||
| } else { | ||
| // save.complete('success'); | ||
| console.log("COMPLETED"); | ||
| } | ||
| }); | ||
|
|
||
| result.complete('success'); | ||
| this._advanceSuccessfulTransaction(plan); | ||
| }); | ||
| }, | ||
|
|
||
| didInsertElement() { | ||
| this._super(...arguments); | ||
| }, | ||
| }); | ||
1 change: 1 addition & 0 deletions
1
assets/javascripts/discourse/templates/components/subscribe-payments.hbs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <div id="payment-request-button"></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,18 @@ | |
| class="btn btn-primary btn-payment" | ||
| label="discourse_subscriptions.plans.payment_button" | ||
| }} | ||
|
|
||
| {{subscribe-payments | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide a screenshot of what this would look like? |
||
| stripe=stripe | ||
| plans=model.plans | ||
| selectedPlan=selectedPlan | ||
| promoCode=promoCode | ||
| alert=alert | ||
| _advanceSuccessfulTransaction=_advanceSuccessfulTransaction | ||
| handleAuthentication=handleAuthentication | ||
| }} | ||
|
|
||
|
|
||
| {{/if}} | ||
| {{else}} | ||
| <h2>{{i18n "discourse_subscriptions.subscribe.already_purchased"}}</h2> | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticing a lot of
console.logs in this PR.