diff --git a/lib/pin_payment/base.rb b/lib/pin_payment/base.rb index 2eab21b..2d90f4f 100644 --- a/lib/pin_payment/base.rb +++ b/lib/pin_payment/base.rb @@ -23,6 +23,10 @@ def self.put uri, options = {} def self.get uri, options = {} fetch Net::HTTP::Get, uri, options end + + def self.delete uri, options = {} + fetch Net::HTTP::Delete, uri, options + end def self.fetch klass, uri, options client = Net::HTTP.new(uri.host, uri.port) diff --git a/lib/pin_payment/customer.rb b/lib/pin_payment/customer.rb index 6b3bd4f..b5525fe 100644 --- a/lib/pin_payment/customer.rb +++ b/lib/pin_payment/customer.rb @@ -14,6 +14,10 @@ def self.create email, card_or_token = nil response = post(URI.parse(PinPayment.api_url).tap{|uri| uri.path = '/1/customers' }, options) new(response.delete('token'), response) end + + def self.delete_customer(token) + delete(URI.parse(PinPayment.api_url).tap{|uri| uri.path = "/1/customers/#{token}" }) + end # Update a customer using the pin API. # diff --git a/test/fixtures/responses.yml b/test/fixtures/responses.yml index 74836a0..316377b 100644 --- a/test/fixtures/responses.yml +++ b/test/fixtures/responses.yml @@ -12,6 +12,8 @@ customer: created: '{"response":{"token":"cus__03Cn1lSk3offZ0IGkwpCg","email":"foo@example.com","created_at":"2013-06-12T10:08:30Z","card":{"token":"card_qMwnMfpG-olOhfJeyxmrcg","display_number":"XXXX-XXXX-XXXX-0000","scheme":"master","address_line1":"123 Main St","address_line2":"","address_city":"Melbourne","address_postcode":"3000","address_state":"Victoria","address_country":"Australia"}}}' all: '{"response":[{"token":"cus_OEHdj9KE_OiC9eig0h3IEA","email":"user1@example.com","created_at":"2013-06-25T04:14:35Z","card":{"token":"card_7rviJ2IZM__WC9Dnj0RHJA","display_number":"XXXX-XXXX-XXXX-0000","scheme":"visa","address_line1":"123 Main St","address_line2":"","address_city":"Melbourne","address_postcode":"3000","address_state":"Victoria","address_country":"Australia"}},{"token":"cus_cO2VtF4YzK6i1QBcLsdzOA","email":"user2@example.com","created_at":"2013-06-24T05:53:42Z","card":{"token":"card_bVou2J4hzbDIwe-sQ3ZqdQ","display_number":"XXXX-XXXX-XXXX-0000","scheme":"visa","address_line1":"123 Main St","address_line2":"","address_city":"Melbourne","address_postcode":"3000","address_state":"Victoria","address_country":"Australia"}}],"count":2,"pagination":{"current":1,"previous":null,"next":null,"per_page":25,"pages":1,"count":2}}' create_with_card: '{"response":{"token":"cus_XZg1ULpWaROQCOT5PdwLkQ","email":"roland@pin.net.au","created_at":"2013-06-27T00:24:43Z","card":{"token":"card_nytGw7koRg23EEp9NTmz9w","display_number":"XXXX-XXXX-XXXX-0000","scheme":"master","address_line1":"42 Sevenoaks St","address_line2":null,"address_city":"Lathlain","address_postcode":"6454","address_state":"WA","address_country":"Australia"}}}' + deleted: '{"response":{}}' + delete_not_found: '{"error":"resource_not_found","error_description":"No resource was found at this URL."}' refund: success: '{"response":{"token":"rf_wMYx5YHKaZAwQgj5rtNuTg","success":null,"amount":1000,"currency":"USD","charge":"ch_BjGW-S6WUisI6mOgpDRimg","created_at":"2013-06-25T03:16:33Z","error_message":null,"status_message":"Pending"}}' duplicate: '{"error":"invalid_resource","error_description":"One or more parameters were missing or invalid.","messages":{"charge":["You have tried to refund more than the original charge"]}}' diff --git a/test/test_pin_customer.rb b/test/test_pin_customer.rb index ff8b427..52f913c 100644 --- a/test/test_pin_customer.rb +++ b/test/test_pin_customer.rb @@ -16,6 +16,21 @@ def test_create_success customer = created_customer assert_kind_of PinPayment::Customer, customer end + + def test_delete_success + customer = created_customer + FakeWeb.register_uri(:delete, "https://test-api.pin.net.au/1/customers/#{customer.token}", body: fixtures['responses']['customer']['deleted']) + response = PinPayment::Customer.delete_customer(customer.token) + assert_empty response + end + + def test_delete_not_found + token = 'none-existing-token' + FakeWeb.register_uri(:delete, "https://test-api.pin.net.au/1/customers/#{token}", body: fixtures['responses']['customer']['delete_not_found']) + assert_raises PinPayment::Error::ResourceNotFound do + PinPayment::Customer.delete_customer(token) + end + end def test_direct_update customer = created_customer