-
Notifications
You must be signed in to change notification settings - Fork 353
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
Support new Checkout flow #466
Changes from 5 commits
6e2b4ae
f0bce5c
cc06e1f
a321677
4dee26c
7790ffd
a4eb82d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
defmodule Stripe.Session do | ||
@moduledoc """ | ||
Work with Stripe Checkout Session objects. | ||
|
||
You can: | ||
|
||
- Create a new session | ||
|
||
Stripe API reference: https://stripe.com/docs/api/checkout/sessions | ||
""" | ||
|
||
use Stripe.Entity | ||
import Stripe.Request | ||
|
||
@type line_item :: %{ | ||
:amount => integer(), | ||
:currency => String.t(), | ||
:name => String.t(), | ||
:quantity => integer(), | ||
optional(:description) => String.t(), | ||
optional(:images) => list(String.t()) | ||
} | ||
|
||
@type capture_method :: :automatic | :manual | ||
|
||
@type address :: %{ | ||
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. in |
||
:line1 => String.t(), | ||
optional(:city) => String.t(), | ||
optional(:country) => String.t(), | ||
optional(:line2) => String.t(), | ||
optional(:postal_code) => String.t(), | ||
optional(:state) => String.t() | ||
} | ||
|
||
@type shipping_info :: %{ | ||
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. in |
||
:address => address, | ||
optional(:carrier) => String.t(), | ||
optional(:phone) => String.t(), | ||
optional(:tracking_number) => String.t() | ||
} | ||
|
||
@type transfer_data :: %{ | ||
:destination => String.t() | ||
} | ||
|
||
@type payment_intent_data :: %{ | ||
optional(:application_fee_amount) => integer(), | ||
optional(:capture_method) => capture_method, | ||
optional(:description) => String.t(), | ||
optional(:metadata) => map(), | ||
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. r/String.t()/Stripe.Types.metadata() |
||
optional(:on_behalf_of) => String.t(), | ||
optional(:receipt_email) => String.t(), | ||
optional(:shipping) => shipping_info, | ||
optional(:statement_descriptor) => String.t(), | ||
optional(:transfer_data) => transfer_data | ||
} | ||
|
||
@type item :: %{ | ||
:plan => String.t(), | ||
optional(:quantity) => integer() | ||
} | ||
|
||
@type subscription_data :: %{ | ||
:items => list(item), | ||
:metadata => map(), | ||
:trial_end => integer(), | ||
:trial_period_days => integer() | ||
} | ||
|
||
@type create_params :: %{ | ||
:cancel_url => String.t(), | ||
:payment_method_types => list(String.t()), | ||
:success_url => String.t(), | ||
optional(:client_reference_id) => String.t(), | ||
optional(:customer_email) => String.t(), | ||
optional(:line_items) => list(line_item), | ||
optional(:locale) => String.t(), | ||
optional(:payment_intent_data) => payment_intent_data, | ||
optional(:subscription_data) => subscription_data | ||
} | ||
|
||
@type t :: %{ | ||
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. Do we need 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. Great catch! |
||
:id => Stripe.id(), | ||
:object => String.t(), | ||
:livemode => boolean() | ||
} | ||
|
||
defstruct [ | ||
:id, | ||
:object, | ||
:livemode | ||
] | ||
|
||
@plural_endpoint "checkout/sessions" | ||
|
||
@spec create(create_params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()} | ||
def create(params, opts \\ []) do | ||
new_request(opts) | ||
|> put_endpoint(@plural_endpoint) | ||
|> put_params(params) | ||
|> put_method(:post) | ||
|> make_request() | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"id": "CdWP8EBmSp1tJNIw4ZLF6w3XKd8MNKkEvlnSK7QmwFlDZ8rrjqBn9VI9vKiVdhfE", | ||
"livemode": false, | ||
"object": "checkout.session" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule Stripe.SessionTest do | ||
use Stripe.StripeCase, async: true | ||
|
||
test "is creatable" do | ||
params = %{ | ||
cancel_url: "https://stripe.com", | ||
payment_method_types: ["card"], | ||
success_url: "https://stripe.com" | ||
} | ||
|
||
assert {:ok, %Stripe.Session{}} = Stripe.Session.create(params) | ||
assert_stripe_requested(:post, "/v1/checkout/sessions") | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,4 +243,17 @@ defmodule Stripe.ConverterTest do | |
|
||
assert result == expected_result | ||
end | ||
|
||
test "converts a checkout.session response properly" do | ||
expected_result = %Stripe.Session{ | ||
id: "CdWP8EBmSp1tJNIw4ZLF6w3XKd8MNKkEvlnSK7QmwFlDZ8rrjqBn9VI9vKiVdhfE", | ||
livemode: false, | ||
object: "checkout.session" | ||
} | ||
|
||
fixture = Helper.load_fixture("session.json") | ||
result = Converter.convert_result(fixture) | ||
|
||
assert result == expected_result | ||
end | ||
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. 👍 |
||
end |
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.
We have a
Stripe.LineItem
object. Has everything exceptimages
. Don't see it here though 🤔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.
I think that might be a new feature for the new Stripe Checkout: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items
My personal thinking is that the
Stripe.LineItem
type has a lot of fields on it that won't be accepted by this endpoint, so it would be better to have a second localline_item
type that is specific to this use case. If you prefer though, I'm happy to add animages
field toStripe.LineItem
and use that.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.
na we can leave it as is 👍