-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #745 from stripe/remi-add-payment-methods
Add support for the PaymentMethod resource
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Stripe | ||
class PaymentMethod < APIResource | ||
extend Stripe::APIOperations::Create | ||
include Stripe::APIOperations::Save | ||
extend Stripe::APIOperations::List | ||
|
||
OBJECT_NAME = "payment_method".freeze | ||
|
||
def attach(params = {}, opts = {}) | ||
url = resource_url + "/attach" | ||
resp, opts = request(:post, url, params, opts) | ||
initialize_from(resp.data, opts) | ||
end | ||
|
||
def detach(params = {}, opts = {}) | ||
url = resource_url + "/detach" | ||
resp, opts = request(:post, url, params, opts) | ||
initialize_from(resp.data, opts) | ||
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
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,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require ::File.expand_path("../../test_helper", __FILE__) | ||
|
||
module Stripe | ||
class PaymentMethodTest < Test::Unit::TestCase | ||
should "be listable" do | ||
payment_methods = Stripe::PaymentMethod.list( | ||
customer: "cus_123", | ||
type: "card" | ||
) | ||
assert_requested :get, "#{Stripe.api_base}/v1/payment_methods?customer=cus_123&type=card" | ||
assert payment_methods.data.is_a?(Array) | ||
assert payment_methods.first.is_a?(Stripe::PaymentMethod) | ||
end | ||
|
||
should "be retrievable" do | ||
payment_method = Stripe::PaymentMethod.retrieve("pm_123") | ||
assert_requested :get, "#{Stripe.api_base}/v1/payment_methods/pm_123" | ||
assert payment_method.is_a?(Stripe::PaymentMethod) | ||
end | ||
|
||
should "be creatable" do | ||
payment_method = Stripe::PaymentMethod.create( | ||
type: "card" | ||
) | ||
assert_requested :post, "#{Stripe.api_base}/v1/payment_methods" | ||
assert payment_method.is_a?(Stripe::PaymentMethod) | ||
end | ||
|
||
should "be saveable" do | ||
payment_method = Stripe::PaymentMethod.retrieve("pm_123") | ||
payment_method.metadata["key"] = "value" | ||
payment_method.save | ||
assert_requested :post, "#{Stripe.api_base}/v1/payment_methods/#{payment_method.id}" | ||
end | ||
|
||
should "be updateable" do | ||
payment_method = Stripe::PaymentMethod.update("pm_123", metadata: { key: "value" }) | ||
assert_requested :post, "#{Stripe.api_base}/v1/payment_methods/pm_123" | ||
assert payment_method.is_a?(Stripe::PaymentMethod) | ||
end | ||
|
||
context "#attach" do | ||
should "attach payment_method" do | ||
payment_method = Stripe::PaymentMethod.construct_from(id: "pm_123", object: "payment_method") | ||
payment_method = payment_method.attach( | ||
customer: "cus_123" | ||
) | ||
|
||
assert_requested :post, "#{Stripe.api_base}/v1/payment_methods/pm_123/attach" | ||
assert payment_method.is_a?(Stripe::PaymentMethod) | ||
end | ||
end | ||
|
||
context "#detach" do | ||
should "detach payment_method" do | ||
payment_method = Stripe::PaymentMethod.construct_from(id: "pm_123", object: "payment_method") | ||
payment_method = payment_method.detach | ||
|
||
assert_requested :post, "#{Stripe.api_base}/v1/payment_methods/pm_123/detach" | ||
assert payment_method.is_a?(Stripe::PaymentMethod) | ||
end | ||
end | ||
end | ||
end |