Skip to content

Commit

Permalink
Add helpers for creating PaymentIntents and Stripe Terminal charges. F…
Browse files Browse the repository at this point in the history
…ixes #828
  • Loading branch information
excid3 committed Aug 22, 2023
1 parent 5a32793 commit 4637a0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### Unreleased

* Update to Stripe `2023-08-16` API version
* [Stripe] Add `pay_customer.create_payment_intent` for creating payment intents without immediate confirmation
* [Stripe] Add `pay_customer.terminal_charge` for creating Stripe Terminal payments

### 6.7.2

Expand Down
26 changes: 20 additions & 6 deletions lib/pay/stripe/billable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,17 @@ def update_customer!(**attributes)
)
end

# Charges an amount to the customer's default payment method
def charge(amount, options = {})
add_payment_method(payment_method_token, default: true) if payment_method_token?

payment_method = pay_customer.default_payment_method
args = {
amount: amount,
confirm: true,
currency: "usd",
customer: processor_id,
expand: ["latest_charge.refunds"],
payment_method: payment_method&.processor_id,
return_url: root_url
}.merge(options)

payment_intent = ::Stripe::PaymentIntent.create(args, stripe_options)
payment_intent = create_payment_intent(amount, args)
Pay::Payment.new(payment_intent).validate

charge = payment_intent.latest_charge
Expand All @@ -100,6 +96,24 @@ def charge(amount, options = {})
raise Pay::Stripe::Error, e
end

# Creates and returns a Stripe::PaymentIntent
def create_payment_intent(amount, options = {})
args = {
amount: amount,
currency: "usd",
customer: processor_id,
expand: ["latest_charge.refunds"],
return_url: root_url
}.merge(options)

::Stripe::PaymentIntent.create(args, stripe_options)
end

# Used for creating Stripe Terminal charges
def terminal_charge(amount, options = {})
create_payment_intent(amount, options.merge( payment_method_types: ["card_present"], capture_method: "manual"))
end

def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options)
quantity = options.delete(:quantity)
opts = {
Expand Down

0 comments on commit 4637a0b

Please sign in to comment.