-
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 #657 from stripe/remi-add-payment-intent
Add support for the PaymentIntent resource
- Loading branch information
Showing
5 changed files
with
130 additions
and
1 deletion.
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Stripe | ||
class PaymentIntent < APIResource | ||
extend Stripe::APIOperations::Create | ||
include Stripe::APIOperations::Delete | ||
extend Stripe::APIOperations::List | ||
include Stripe::APIOperations::Save | ||
|
||
OBJECT_NAME = "payment_intent".freeze | ||
|
||
def self.resource_url | ||
"/v1/payment_intents" | ||
end | ||
|
||
def cancel | ||
resp, api_key = request(:post, resource_url + "/cancel") | ||
initialize_from(resp.data, api_key) | ||
end | ||
|
||
def capture | ||
resp, api_key = request(:post, resource_url + "/capture") | ||
initialize_from(resp.data, api_key) | ||
end | ||
|
||
def confirm | ||
resp, api_key = request(:post, resource_url + "/confirm") | ||
initialize_from(resp.data, api_key) | ||
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,96 @@ | ||
# frozen_string_literal: true | ||
|
||
require File.expand_path("../../test_helper", __FILE__) | ||
|
||
module Stripe | ||
TEST_RESOURCE_ID = "pi_123".freeze | ||
|
||
class PaymentIntentTest < Test::Unit::TestCase | ||
should "be listable" do | ||
stub_request(:get, "#{Stripe.api_base}/v1/payment_intents") | ||
.to_return(body: JSON.generate(object: "list", data: [{ id: "pi_123", object: "payment_intent" }])) | ||
|
||
payment_intents = Stripe::PaymentIntent.list | ||
assert_requested :get, "#{Stripe.api_base}/v1/payment_intents" | ||
assert payment_intents.data.is_a?(Array) | ||
assert payment_intents.data[0].is_a?(Stripe::PaymentIntent) | ||
end | ||
|
||
should "be retrievable" do | ||
stub_request(:get, "#{Stripe.api_base}/v1/payment_intents/pi_123") | ||
.to_return(body: JSON.generate(id: "pi_123", object: "payment_intent")) | ||
|
||
payment_intent = Stripe::PaymentIntent.retrieve("pi_123") | ||
assert_requested :get, "#{Stripe.api_base}/v1/payment_intents/pi_123" | ||
assert payment_intent.is_a?(Stripe::PaymentIntent) | ||
end | ||
|
||
should "be creatable" do | ||
stub_request(:post, "#{Stripe.api_base}/v1/payment_intents") | ||
.to_return(body: JSON.generate(id: "pi_123", object: "payment_intent")) | ||
|
||
payment_intent = Stripe::PaymentIntent.create | ||
assert_requested :post, "#{Stripe.api_base}/v1/payment_intents" | ||
assert payment_intent.is_a?(Stripe::PaymentIntent) | ||
end | ||
|
||
should "be saveable" do | ||
stub_request(:post, "#{Stripe.api_base}/v1/payment_intents/pi_123") | ||
.to_return(body: JSON.generate(id: "pi_123", object: "payment_intent")) | ||
|
||
payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123", object: "payment_intent", metadata: {}) | ||
payment_intent.metadata["key"] = "value" | ||
payment_intent.save | ||
assert_requested :post, "#{Stripe.api_base}/v1/payment_intents/#{payment_intent.id}" | ||
end | ||
|
||
should "be updateable" do | ||
stub_request(:post, "#{Stripe.api_base}/v1/payment_intents/pi_123") | ||
.to_return(body: JSON.generate(id: "pi_123", object: "payment_intent")) | ||
|
||
payment_intent = Stripe::PaymentIntent.update("pi_123", metadata: { foo: "bar" }) | ||
|
||
assert_requested :post, "#{Stripe.api_base}/v1/payment_intents/pi_123" | ||
assert payment_intent.is_a?(Stripe::PaymentIntent) | ||
end | ||
|
||
context "#cancel" do | ||
should "cancel a payment_intent" do | ||
stub_request(:post, "#{Stripe.api_base}/v1/payment_intents/pi_123/cancel") | ||
.to_return(body: JSON.generate(id: "pi_123", object: "payment_intent")) | ||
|
||
payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123", object: "payment_intent") | ||
payment_intent = payment_intent.cancel | ||
|
||
assert_requested :post, "#{Stripe.api_base}/v1/payment_intents/pi_123/cancel" | ||
assert payment_intent.is_a?(Stripe::PaymentIntent) | ||
end | ||
end | ||
|
||
context "#capture" do | ||
should "capture a payment_intent" do | ||
stub_request(:post, "#{Stripe.api_base}/v1/payment_intents/pi_123/capture") | ||
.to_return(body: JSON.generate(id: "pi_123", object: "payment_intent")) | ||
|
||
payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123", object: "payment_intent") | ||
payment_intent = payment_intent.capture | ||
|
||
assert_requested :post, "#{Stripe.api_base}/v1/payment_intents/pi_123/capture" | ||
assert payment_intent.is_a?(Stripe::PaymentIntent) | ||
end | ||
end | ||
|
||
context "#confirm" do | ||
should "confirm a payment_intent" do | ||
stub_request(:post, "#{Stripe.api_base}/v1/payment_intents/pi_123/confirm") | ||
.to_return(body: JSON.generate(id: "pi_123", object: "payment_intent")) | ||
|
||
payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123", object: "payment_intent") | ||
payment_intent = payment_intent.confirm | ||
|
||
assert_requested :post, "#{Stripe.api_base}/v1/payment_intents/pi_123/confirm" | ||
assert payment_intent.is_a?(Stripe::PaymentIntent) | ||
end | ||
end | ||
end | ||
end |