forked from pay-rails/pay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
584 additions
and
103 deletions.
There are no files selected for viewing
This file contains 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 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,21 @@ | ||
<%= button_tag title, | ||
id: "checkout-#{session.id}", | ||
class: local_assigns[:class], | ||
style: (local_assigns[:class] || local_assigns[:style]) ? local_assigns[:style] : 'background-color:#6772E5;color:#FFF;padding:8px 12px;border:0;border-radius:4px;font-size:1em' | ||
%> | ||
<%= tag.div id: "error-for-#{session.id}" %> | ||
|
||
<script> | ||
(() => { | ||
const checkoutButton = document.getElementById("checkout-<%= session.id %>"); | ||
checkoutButton.addEventListener('click', function () { | ||
Stripe("<%= Pay::Stripe.public_key %>").redirectToCheckout({ | ||
sessionId: '<%= session.id %>' | ||
}).then(function (result) { | ||
if (result.error) { | ||
document.getElementById('error-message').innerText = result.error.message; | ||
} | ||
}); | ||
}); | ||
})() | ||
</script> |
This file contains 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 |
---|---|---|
@@ -1 +1,37 @@ | ||
# Using Pay with Stripe | ||
|
||
Stripe has multiple integrations: | ||
|
||
* Stripe Checkout - Hosted pages for payments | ||
|
||
## Stripe Checkout | ||
|
||
[Stripe Checkout](https://stripe.com/docs/payments/checkout) allows you to simply redirect to Stripe for handling payments. The main benefit is that it's super fast to setup payments in your application, they're SCA compatible, and they will get improved automatically by Stripe. | ||
|
||
![stripe checkout example](https://i.imgur.com/nFsCBCK.gif) | ||
|
||
### How to use Stripe Checkout with Pay | ||
|
||
1. Create a checkout session | ||
|
||
Choose the checkout button mode you need and pass any required arguments. Read the [Stripe Checkout Session API docs](https://stripe.com/docs/api/checkout/sessions/create) to see what options are available. | ||
|
||
```ruby | ||
# Make sure the user's payment processor is Stripe | ||
current_user.processor = :stripe | ||
|
||
# One-time payments | ||
@checkout_session = current_user.payment_processor.checkout(mode: "payment", line_items: "price_1ILVZaKXBGcbgpbZQ26kgXWG") | ||
|
||
# Subscriptions | ||
@checkout_session = current_user.payment_processor.checkout(mode: "subscription", line_items: "default") | ||
|
||
# Setup a new card for future use | ||
@subscription = current_user.payment_processor.checkout(mode: "setup") | ||
``` | ||
|
||
2. Render the button | ||
|
||
```erb | ||
<%= render partial: "pay/stripe/checkout_button", locals: { session: @checkout_session, title: "Checkout" } %> | ||
``` |
This file contains 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
32 changes: 17 additions & 15 deletions
32
test/dummy/app/controllers/stripe/charges/imports_controller.rb
This file contains 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 |
---|---|---|
@@ -1,21 +1,23 @@ | ||
class Stripe::Charges::ImportsController < ApplicationController | ||
def new | ||
end | ||
module Stripe | ||
class Charges::ImportsController < ApplicationController | ||
def new | ||
end | ||
|
||
def create | ||
object = find_stripe_object | ||
charge = Pay::Stripe::Webhooks::ChargeSucceeded.new.create_charge(User.first, object) | ||
redirect_to stripe_charge_path(charge) | ||
end | ||
def create | ||
object = find_stripe_object | ||
charge = Pay::Stripe::Webhooks::ChargeSucceeded.new.create_charge(User.first, object) | ||
redirect_to stripe_charge_path(charge) | ||
end | ||
|
||
private | ||
private | ||
|
||
def find_stripe_object | ||
case params[:id] | ||
when /^ch_/ | ||
Stripe::Charge.retrieve(params[:id]) | ||
when /^pi_/ | ||
Stripe::PaymentIntent.retrieve(params[:id]).charges.first | ||
def find_stripe_object | ||
case params[:id] | ||
when /^ch_/ | ||
Stripe::Charge.retrieve(params[:id]) | ||
when /^pi_/ | ||
Stripe::PaymentIntent.retrieve(params[:id]).charges.first | ||
end | ||
end | ||
end | ||
end |
This file contains 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 |
---|---|---|
@@ -1,39 +1,41 @@ | ||
class Stripe::ChargesController < ApplicationController | ||
before_action :set_charge, only: [:show, :refund] | ||
module Stripe | ||
class ChargesController < ApplicationController | ||
before_action :set_charge, only: [:show, :refund] | ||
|
||
def index | ||
@charges = Pay::Charge.where(processor: :stripe).order(created_at: :desc) | ||
end | ||
def index | ||
@charges = Pay::Charge.where(processor: :stripe).order(created_at: :desc) | ||
end | ||
|
||
def show | ||
end | ||
def show | ||
end | ||
|
||
def new | ||
end | ||
def new | ||
end | ||
|
||
def create | ||
current_user.processor = params[:processor] | ||
current_user.card_token = params[:card_token] | ||
charge = current_user.charge(params[:amount]) | ||
redirect_to stripe_charge_path(charge) | ||
rescue Pay::ActionRequired => e | ||
redirect_to pay.payment_path(e.payment.id) | ||
rescue Pay::Error => e | ||
flash[:alert] = e.message | ||
render :new | ||
end | ||
def create | ||
current_user.processor = params[:processor] | ||
current_user.card_token = params[:card_token] | ||
charge = current_user.charge(params[:amount]) | ||
redirect_to stripe_charge_path(charge) | ||
rescue Pay::ActionRequired => e | ||
redirect_to pay.payment_path(e.payment.id) | ||
rescue Pay::Error => e | ||
flash[:alert] = e.message | ||
render :new | ||
end | ||
|
||
def refund | ||
@charge.refund! | ||
rescue Pay::Error => e | ||
flash[:alert] = e.message | ||
ensure | ||
redirect_to stripe_charge_path(@charge) | ||
end | ||
def refund | ||
@charge.refund! | ||
rescue Pay::Error => e | ||
flash[:alert] = e.message | ||
ensure | ||
redirect_to stripe_charge_path(@charge) | ||
end | ||
|
||
private | ||
private | ||
|
||
def set_charge | ||
@charge = Pay::Charge.find(params[:id]) | ||
def set_charge | ||
@charge = Pay::Charge.find(params[:id]) | ||
end | ||
end | ||
end |
This file contains 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,10 @@ | ||
module Stripe | ||
class CheckoutsController < ApplicationController | ||
def show | ||
current_user.processor = :stripe | ||
@payment = current_user.payment_processor.checkout(mode: "payment", line_items: "price_1ILVZaKXBGcbgpbZQ26kgXWG") | ||
@subscription = current_user.payment_processor.checkout(mode: "subscription", line_items: "default") | ||
@setup = current_user.payment_processor.checkout(mode: "setup") | ||
end | ||
end | ||
end |
18 changes: 10 additions & 8 deletions
18
test/dummy/app/controllers/stripe/payment_methods_controller.rb
This file contains 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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
class Stripe::PaymentMethodsController < ApplicationController | ||
def edit | ||
@setup_intent = ::Stripe::SetupIntent.create | ||
end | ||
module Stripe | ||
class PaymentMethodsController < ApplicationController | ||
def edit | ||
@setup_intent = ::Stripe::SetupIntent.create | ||
end | ||
|
||
def update | ||
current_user.processor = params[:processor] | ||
current_user.update_card(params[:card_token]) | ||
redirect_to payment_method_path | ||
def update | ||
current_user.processor = params[:processor] | ||
current_user.update_card(params[:card_token]) | ||
redirect_to payment_method_path | ||
end | ||
end | ||
end |
102 changes: 52 additions & 50 deletions
102
test/dummy/app/controllers/stripe/subscriptions_controller.rb
This file contains 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 |
---|---|---|
@@ -1,52 +1,54 @@ | ||
class Stripe::SubscriptionsController < ApplicationController | ||
before_action :set_subscription, only: [:show, :edit, :update, :destroy, :cancel, :resume] | ||
|
||
def index | ||
@subscriptions = Pay::Subscription.where(processor: :stripe).order(created_at: :desc) | ||
end | ||
|
||
def show | ||
end | ||
|
||
def new | ||
end | ||
|
||
def create | ||
current_user.processor = params[:processor] | ||
current_user.card_token = params[:card_token] | ||
subscription = current_user.subscribe(plan: params[:price_id]) | ||
redirect_to stripe_subscription_path(subscription) | ||
rescue Pay::ActionRequired => e | ||
redirect_to pay.payment_path(e.payment.id) | ||
rescue Pay::Error => e | ||
flash[:alert] = e.message | ||
render :new | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
end | ||
|
||
def destroy | ||
@subscription.cancel_now! | ||
redirect_to stripe_subscription_path(@subscription) | ||
end | ||
|
||
def cancel | ||
@subscription.cancel | ||
redirect_to stripe_subscription_path(@subscription) | ||
end | ||
|
||
def resume | ||
@subscription.resume | ||
redirect_to stripe_subscription_path(@subscription) | ||
end | ||
|
||
private | ||
|
||
def set_subscription | ||
@subscription = Pay::Subscription.where(processor: :stripe).find(params[:id]) | ||
module Stripe | ||
class SubscriptionsController < ApplicationController | ||
before_action :set_subscription, only: [:show, :edit, :update, :destroy, :cancel, :resume] | ||
|
||
def index | ||
@subscriptions = Pay::Subscription.where(processor: :stripe).order(created_at: :desc) | ||
end | ||
|
||
def show | ||
end | ||
|
||
def new | ||
end | ||
|
||
def create | ||
current_user.processor = params[:processor] | ||
current_user.card_token = params[:card_token] | ||
subscription = current_user.subscribe(plan: params[:price_id]) | ||
redirect_to stripe_subscription_path(subscription) | ||
rescue Pay::ActionRequired => e | ||
redirect_to pay.payment_path(e.payment.id) | ||
rescue Pay::Error => e | ||
flash[:alert] = e.message | ||
render :new | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
end | ||
|
||
def destroy | ||
@subscription.cancel_now! | ||
redirect_to stripe_subscription_path(@subscription) | ||
end | ||
|
||
def cancel | ||
@subscription.cancel | ||
redirect_to stripe_subscription_path(@subscription) | ||
end | ||
|
||
def resume | ||
@subscription.resume | ||
redirect_to stripe_subscription_path(@subscription) | ||
end | ||
|
||
private | ||
|
||
def set_subscription | ||
@subscription = Pay::Subscription.where(processor: :stripe).find(params[:id]) | ||
end | ||
end | ||
end |
Oops, something went wrong.