-
Notifications
You must be signed in to change notification settings - Fork 70
Add payment methods via Stripe #2011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jim
wants to merge
9
commits into
main
Choose a base branch
from
jim-stripe-payment-methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ba57257
Add gem
jim 8e11986
wip
jim 56efeb8
wip
jim 41d21eb
Sketch out integration
jim f79c0e4
Rearrange migrations after rebase
crismali fd6516a
Made skipping csrf protection narrower for the github security bot
crismali a5b0f12
Added basic VCR/webmock setup and some tests for StripeCheckout
crismali c7ac9d3
Added tests for StripeCheckout
crismali ffad075
WIP: system tests for payment methods, non-nullable colums
crismali 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
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
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
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
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,34 @@ | ||
| module Account | ||
| class PaymentMethodsController < BaseController | ||
| def new | ||
| @result = checkout.prepare_to_collect_payment_info(current_user) | ||
| unless @result.success? | ||
| Rails.logger.error(result.error) | ||
| flash[:error] = "There was a problem connecting to our payment processor." | ||
| redirect_to_back_or_default | ||
| end | ||
| end | ||
|
|
||
| def index | ||
| checkout.sync_payment_methods(current_user) | ||
| @payment_methods = current_user.payment_methods.active | ||
| end | ||
|
|
||
| def destroy | ||
| @payment_method = current_user.payment_methods.find(params[:id]) | ||
| result = checkout.delete_payment_method(@payment_method) | ||
| if result.success? | ||
| flash[:success] = "Successfully deleted payment method" | ||
| else | ||
| flash[:error] = result.error | ||
| end | ||
| redirect_to account_payment_methods_path, status: :see_other | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def checkout | ||
| StripeCheckout.build | ||
| end | ||
| end | ||
| end |
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,41 @@ | ||
| class StripeController < ApplicationController | ||
| skip_before_action :verify_authenticity_token, only: %i[webhook] | ||
|
|
||
| def webhook | ||
| payload = request.body.read | ||
| event = nil | ||
|
|
||
| begin | ||
| event = Stripe::Event.construct_from( | ||
| JSON.parse(payload, symbolize_names: true) | ||
| ) | ||
| rescue JSON::ParserError | ||
| # Invalid payload | ||
| render status: 400 | ||
| return | ||
| end | ||
|
|
||
| # Retrieve the event by verifying the signature using the raw body and the endpoint secret | ||
| signing_secret = ENV["STRIPE_WEBHOOK_SIGNING_SECRET"] | ||
| signature = request.env["HTTP_STRIPE_SIGNATURE"] | ||
| begin | ||
| event = Stripe::Webhook.construct_event( | ||
| payload, signature, signing_secret | ||
| ) | ||
| rescue Stripe::SignatureVerificationError => e | ||
| Rails.logger.warn "⚠️ Webhook signature verification failed. #{e.message}" | ||
| render status: 400 | ||
| return | ||
| end | ||
|
|
||
| # Handle the event | ||
| case event.type | ||
| when "checkout.session.completed" | ||
| session = event.data.object # contains a Stripe::PaymentIntent | ||
| Rails.logger.info session | ||
| else | ||
| Rails.logger.info "Unhandled event type: #{event.type}" | ||
| end | ||
| render json: {message: :success} | ||
| end | ||
| end | ||
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
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,59 @@ | ||
| import { Controller } from '@hotwired/stimulus' | ||
|
|
||
| export default class extends Controller { | ||
| static targets = ['elements', 'errors'] | ||
| static values = { | ||
| intentSecret: String, | ||
| returnUrl: String, | ||
| } | ||
|
|
||
| connect() { | ||
| this.clientKey = document | ||
| .querySelector('meta[name=stripe-client-key') | ||
| .getAttribute('content') | ||
| this.client = Stripe(this.clientKey) | ||
|
|
||
| const options = { | ||
| clientSecret: this.intentSecretValue, | ||
| // Fully customizable with appearance API. | ||
| appearance: { | ||
| /*...*/ | ||
| }, | ||
| } | ||
|
|
||
| // Set up Stripe.js and Elements using the SetupIntent's client secret | ||
| this.elements = this.client.elements(options) | ||
|
|
||
| // Create and mount the Payment Element | ||
| const paymentElementOptions = { layout: 'accordion' } | ||
| const paymentElement = this.elements.create( | ||
| 'payment', | ||
| paymentElementOptions | ||
| ) | ||
| paymentElement.mount(this.elementsTarget) | ||
| } | ||
|
|
||
| async submit(event) { | ||
| event.preventDefault() | ||
|
|
||
| const { error } = await this.client.confirmSetup({ | ||
| elements: this.elements, | ||
| confirmParams: { | ||
| return_url: this.returnUrlValue, | ||
| }, | ||
| }) | ||
|
|
||
| if (error) { | ||
| // This point will only be reached if there is an immediate error when | ||
| // confirming the payment. Show error to your customer (for example, payment | ||
| // details incomplete) | ||
| this.errorsTarget.textContent = error.message | ||
| } else { | ||
| // Your customer will be redirected to your `return_url`. For some payment | ||
| // methods like iDEAL, your customer will be redirected to an intermediate | ||
| // site first to authorize the payment, then redirected to the `return_url`. | ||
| } | ||
| } | ||
|
|
||
| disconnect() {} | ||
| } |
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
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.
Check failure
Code scanning / CodeQL
CSRF protection weakened or disabled High
Copilot Autofix
AI about 1 month ago
Copilot could not generate an autofix suggestion
Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.