-
Notifications
You must be signed in to change notification settings - Fork 356
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e2b4ae
get sessions working
dnsbty f0bce5c
add tests
dnsbty cc06e1f
convert response to object
dnsbty a321677
update stripe-mock version
dnsbty 4dee26c
test converter change
dnsbty 7790ffd
use existing types
dnsbty a4eb82d
make the type use the struct
dnsbty 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 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
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,88 @@ | ||
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 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) => Stripe.Types.metadata(), | ||
optional(:on_behalf_of) => String.t(), | ||
optional(:receipt_email) => String.t(), | ||
optional(:shipping) => Stripe.Types.shipping(), | ||
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 => Stripe.Types.metadata(), | ||
: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 :: %__MODULE__{ | ||
: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 |
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
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,5 @@ | ||
{ | ||
"id": "CdWP8EBmSp1tJNIw4ZLF6w3XKd8MNKkEvlnSK7QmwFlDZ8rrjqBn9VI9vKiVdhfE", | ||
"livemode": false, | ||
"object": "checkout.session" | ||
} |
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,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 |
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 |
---|---|---|
|
@@ -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 |
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.
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 👍