diff --git a/Gemfile.lock b/Gemfile.lock index 4d9a8d4..fe4f41f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - affirm (1.1.3) + affirm (1.2.0) typhoeus GEM @@ -44,3 +44,6 @@ DEPENDENCIES rake rspec (= 3.2.0) webmock (= 1.21.0) + +BUNDLED WITH + 2.2.16 diff --git a/affirm.gemspec b/affirm.gemspec index 4f98442..23151b6 100644 --- a/affirm.gemspec +++ b/affirm.gemspec @@ -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" diff --git a/lib/affirm.rb b/lib/affirm.rb index 6899de5..63f1c53 100644 --- a/lib/affirm.rb +++ b/lib/affirm.rb @@ -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' diff --git a/lib/affirm/checkout.rb b/lib/affirm/checkout.rb new file mode 100644 index 0000000..8dcae44 --- /dev/null +++ b/lib/affirm/checkout.rb @@ -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 diff --git a/spec/checkout_spec.rb b/spec/checkout_spec.rb new file mode 100644 index 0000000..2b19391 --- /dev/null +++ b/spec/checkout_spec.rb @@ -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 diff --git a/spec/fixtures/checkout/retrieve.json b/spec/fixtures/checkout/retrieve.json new file mode 100644 index 0000000..2867ae9 --- /dev/null +++ b/spec/fixtures/checkout/retrieve.json @@ -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" + } +}