diff --git a/lib/mollie/client.rb b/lib/mollie/client.rb index 6432601..c189383 100644 --- a/lib/mollie/client.rb +++ b/lib/mollie/client.rb @@ -62,7 +62,12 @@ def add_version_string(version_string) end def perform_http_call(http_method, api_method, id = nil, http_body = {}, query = {}) - path = "/#{API_VERSION}/#{api_method}/#{id}".chomp('/') + path = if api_method.start_with?(API_ENDPOINT) + URI.parse(api_method).path + else + "/#{API_VERSION}/#{api_method}/#{id}".chomp('/') + end + api_key = http_body.delete(:api_key) || query.delete(:api_key) || @api_key api_endpoint = http_body.delete(:api_endpoint) || query.delete(:api_endpoint) || @api_endpoint diff --git a/lib/mollie/customer/subscription.rb b/lib/mollie/customer/subscription.rb index 48085d5..0ccfa62 100644 --- a/lib/mollie/customer/subscription.rb +++ b/lib/mollie/customer/subscription.rb @@ -84,6 +84,13 @@ def customer(options = {}) Customer.get(customer_id, options) end + def payments(options = {}) + resource_url = Util.extract_url(links, 'payments') + return if resource_url.nil? + response = Mollie::Client.instance.perform_http_call('GET', resource_url, nil, {}, options) + Mollie::List.new(response, Payment) + end + def metadata=(metadata) @metadata = OpenStruct.new(metadata) if metadata.is_a?(Hash) end diff --git a/test/fixtures/subscriptions/get.json b/test/fixtures/subscriptions/get.json new file mode 100644 index 0000000..db1be3c --- /dev/null +++ b/test/fixtures/subscriptions/get.json @@ -0,0 +1,45 @@ +{ + "resource": "subscription", + "id": "sub_8JfGzs6v3K", + "mode": "live", + "createdAt": "2016-06-01T12:23:34+00:00", + "status": "active", + "amount": { + "value": "25.00", + "currency": "EUR" + }, + "times": 4, + "timesRemaining": 4, + "interval": "3 months", + "startDate": "2016-06-01", + "nextPaymentDate": "2016-09-01", + "description": "Quarterly payment", + "method": null, + "mandateId": "mdt_aGQNkteF6w", + "webhookUrl": "https://webshop.example.org/payments/webhook", + "metadata": { + "plan": "small" + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/subscriptions/sub_8JfGzs6v3K", + "type": "application/hal+json" + }, + "customer": { + "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U", + "type": "application/hal+json" + }, + "payments": { + "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/subscriptions/sub_8JfGzs6v3K/payments", + "type": "application/hal+json" + }, + "profile": { + "href": "https://api.mollie.com/v2/profiles/pfl_URR55HPMGx", + "type": "application/hal+json" + }, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/subscriptions-api/get-subscription", + "type": "text/html" + } + } +} diff --git a/test/fixtures/subscriptions/get_payments.json b/test/fixtures/subscriptions/get_payments.json new file mode 100644 index 0000000..c135261 --- /dev/null +++ b/test/fixtures/subscriptions/get_payments.json @@ -0,0 +1,75 @@ +{ + "_embedded": { + "payments": [ + { + "resource": "payment", + "id": "tr_DtKxVP2AgW", + "mode": "live", + "createdAt": "2018-09-19T12:49:52+00:00", + "amount": { + "value": "10.00", + "currency": "EUR" + }, + "description": "Some subscription 19 sep. 2018", + "method": "directdebit", + "metadata": null, + "status": "pending", + "isCancelable": true, + "expiresAt": "2019-09-19T12:49:52+00:00", + "locale": "nl_NL", + "profileId": "pfl_URR55HPMGx", + "customerId": "cst_8wmqcHMN4U", + "mandateId": "mdt_aGQNkteF6w", + "subscriptionId": "sub_8JfGzs6v3K", + "sequenceType": "recurring", + "redirectUrl": null, + "webhookUrl": "https://example.org/webhook", + "settlementAmount": { + "value": "10.00", + "currency": "EUR" + }, + "details": { + "transferReference": "SD67-6850-2204-6029", + "creditorIdentifier": "NL08ZZZ502057730000", + "consumerName": "Customer A", + "consumerAccount": "NL50INGB0006588912", + "consumerBic": "INGBNL2A", + "dueDate": "2018-09-21", + "signatureDate": "2018-09-19" + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_DtKxVP2AgW", + "type": "application/hal+json" + }, + "checkout": null, + "customer": { + "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U", + "type": "application/hal+json" + }, + "mandate": { + "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/mandates/mdt_aGQNkteF6w", + "type": "application/hal+json" + }, + "subscription": { + "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/subscriptions/sub_8JfGzs6v3K", + "type": "application/hal+json" + } + } + } + ] + }, + "count": 1, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/subscriptions-api/list-subscriptions-payments", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/subscriptions/sub_8JfGzs6v3K/payments?limit=50", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } +} diff --git a/test/mollie/customer/subscription_test.rb b/test/mollie/customer/subscription_test.rb index ec50bfb..ed94c32 100644 --- a/test/mollie/customer/subscription_test.rb +++ b/test/mollie/customer/subscription_test.rb @@ -143,6 +143,20 @@ def test_application_fee assert_equal 42.10, subscription.application_fee.amount.value assert_equal'EUR', subscription.application_fee.amount.currency end + + def test_get_payments + stub_request(:get, 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/subscriptions/sub_8JfGzs6v3K') + .to_return(status: 200, body: read_fixture('subscriptions/get.json'), headers: {}) + + stub_request(:get, 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/subscriptions/sub_8JfGzs6v3K/payments') + .to_return(status: 200, body: read_fixture('subscriptions/get_payments.json'), headers: {}) + + subscription = Customer::Subscription.get('sub_8JfGzs6v3K', customer_id: 'cst_8wmqcHMN4U') + payments = subscription.payments + + assert_equal payments.klass, Payment + assert_equal payments.first.id, 'tr_DtKxVP2AgW' + end end end end