Skip to content

Commit

Permalink
Add support for the PaymentIntent resource
Browse files Browse the repository at this point in the history
This feature is gated so the tests are stubbed for now
  • Loading branch information
remi-stripe committed Jun 27, 2018
1 parent 89d1994 commit 201f9c2
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 307
Max: 350

# Offense count: 6
# Configuration parameters: CountKeywordArgs.
Expand Down
1 change: 1 addition & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
require "stripe/login_link"
require "stripe/order"
require "stripe/order_return"
require "stripe/payment_intent"
require "stripe/payout"
require "stripe/plan"
require "stripe/product"
Expand Down
31 changes: 31 additions & 0 deletions lib/stripe/payment_intent.rb
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
1 change: 1 addition & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def self.object_classes
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,
Expand Down
96 changes: 96 additions & 0 deletions test/stripe/payment_intent_test.rb
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

0 comments on commit 201f9c2

Please sign in to comment.