Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
affirm (1.1.3)
affirm (1.2.0)
typhoeus

GEM
Expand Down Expand Up @@ -44,3 +44,6 @@ DEPENDENCIES
rake
rspec (= 3.2.0)
webmock (= 1.21.0)

BUNDLED WITH
2.2.16
2 changes: 1 addition & 1 deletion affirm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Gem::Specification.new do |s|
s.name = "affirm"
s.summary = "Affirm Ruby Client Library"
s.description = "Ruby client library for integrating with Affirm financing payments"
s.version = "1.1.3"
s.version = "1.2.0"
s.license = "Apache License Version 2.0"
s.author = "Reverb.com"
s.email = "dev@reverb.com"
Expand Down
1 change: 1 addition & 0 deletions lib/affirm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'affirm/response'
require 'affirm/charge_event'
require 'affirm/charge'
require 'affirm/checkout'

require 'affirm/errors/error'
require 'affirm/errors/charge_error'
Expand Down
45 changes: 45 additions & 0 deletions lib/affirm/checkout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Affirm
class Checkout
attr_reader :id, :merchant, :shipping, :billing, :items, :discounts, :metadata, :order_id,
:currency, :financing_program, :shipping_amount, :tax_amount, :total

##
# RETRIEVE
#
# id - (required) string. The checkout id
def self.retrieve(id, client: Affirm::API.client)
new(attrs: {"id" => id}, client: client).refresh
end

def initialize(attrs: {}, client: Affirm::API.client)
@client = client
@id = attrs['id']
set_attrs(attrs)
end

def refresh
response = @client.make_request("/checkout/#{id}", :get)

set_attrs(response.body)

self
end

private

def set_attrs(attrs)
@merchant = attrs["merchant"]
@shipping = attrs["shipping"]
@billing = attrs["billing"]
@items = attrs["items"]
@discounts = attrs["discounts"]
@metadata = attrs["metadata"]
@order_id = attrs["order_id"]
@currency = attrs["currency"]
@financing_program = attrs["financing_program"]
@shipping_amount = attrs["shipping_amount"]
@tax_amount = attrs["tax_amount"]
@total = attrs["total"]
end
end
end
62 changes: 62 additions & 0 deletions spec/checkout_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'spec_helper'

describe Affirm::Checkout do
let(:id) { "ABCD" }
let!(:request) do
stub_request(request_method, request_url).to_return(status: response_code, body: response_body)
end

let(:response_code) { 200 }

let(:checkout) { Affirm::Checkout.new(attrs: {"id" => id}) }

describe "self.retrieve" do
let(:request_method) { :get }
let(:request_url) { "#{TEST_URL}/checkout/#{id}" }
let(:response_body) { load_fixture("checkout/retrieve.json") }

it "returns a checkout" do
checkout = Affirm::Checkout.retrieve(id)

checkout.should be_a Affirm::Checkout
end

it "sets attributes" do
checkout = Affirm::Checkout.retrieve(id)

checkout.currency.should == "USD"
checkout.total.should == 6100
checkout.order_id.should == "order_id_123"
checkout.financing_program.should == "flyus_3z6r12r"
checkout.tax_amount.should == 700
checkout.shipping_amount.should == 800
checkout.metadata.should == { "mode" => "modal", "invoice_id" => "invoice_id_123"}
end

it "does not unset the id" do
checkout = Affirm::Checkout.retrieve(id)

checkout.id.should == id
end

context "not found" do
let(:response_code) { 404 }

it "raises an error" do
expect { Affirm::Checkout.retrieve(id) }.to raise_error(Affirm::ResourceNotFoundError)
end
end
end

context "with specified client" do
let(:client) { Affirm::Client.new(public_key: "other_public", secret_key: "other_secret") }

let(:request_method) { :get }
let(:response_body) { load_fixture("checkout/retrieve.json") }
let(:request_url) { /.*other_public:other_secret.*/ }

it "uses the client's creds" do
Affirm::Checkout.retrieve(id, client: client)
end
end
end
36 changes: 36 additions & 0 deletions spec/fixtures/checkout/retrieve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"currency":"USD",
"total":6100,
"order_id":"order_id_123",
"items":{
"sweater-a92123":{
"sku":"sweater-a92123",
"display_name":"Sweater",
"qty":1,
"item_type":"physical",
"item_image_url":"http://placehold.it/350x150",
"item_url":"http://placehold.it/350x150",
"unit_price":5000
}
},
"shipping_amount":400,
"tax_amount":700,
"financing_program": "flyus_3z6r12r",
"shipping_amount":800,
"shipping":{
"name":{
"full":"John Doe"
},
"address":{
"line1":"325 Pacific Ave",
"city":"San Francisco",
"state":"CA",
"zipcode":"94112",
"country":"USA"
}
},
"metadata": {
"mode": "modal",
"invoice_id": "invoice_id_123"
}
}