mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-11-29 00:01:18 -05:00
Merge pull request #657 from stripe/remi-add-payment-intent
Add support for the PaymentIntent resource
This commit is contained in:
commit
d4bd5aad5a
@ -38,7 +38,7 @@ Metrics/MethodLength:
|
||||
# Offense count: 1
|
||||
# Configuration parameters: CountComments.
|
||||
Metrics/ModuleLength:
|
||||
Max: 307
|
||||
Max: 350
|
||||
|
||||
# Offense count: 6
|
||||
# Configuration parameters: CountKeywordArgs.
|
||||
|
||||
@ -63,6 +63,7 @@ require "stripe/issuer_fraud_record"
|
||||
require "stripe/login_link"
|
||||
require "stripe/order"
|
||||
require "stripe/order_return"
|
||||
require "stripe/payment_intent"
|
||||
require "stripe/payout"
|
||||
require "stripe/plan"
|
||||
require "stripe/product"
|
||||
|
||||
31
lib/stripe/payment_intent.rb
Normal file
31
lib/stripe/payment_intent.rb
Normal file
@ -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
|
||||
@ -72,6 +72,7 @@ module Stripe
|
||||
LoginLink::OBJECT_NAME => LoginLink,
|
||||
Order::OBJECT_NAME => Order,
|
||||
OrderReturn::OBJECT_NAME => OrderReturn,
|
||||
PaymentIntent::OBJECT_NAME => PaymentIntent,
|
||||
Payout::OBJECT_NAME => Payout,
|
||||
Plan::OBJECT_NAME => Plan,
|
||||
Product::OBJECT_NAME => Product,
|
||||
|
||||
96
test/stripe/payment_intent_test.rb
Normal file
96
test/stripe/payment_intent_test.rb
Normal file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user