Skip to content

Commit

Permalink
Merge pull request #122 from mollie/GH-103
Browse files Browse the repository at this point in the history
Add helper to fetch subscription payments
  • Loading branch information
vernondegoede authored Sep 16, 2019
2 parents 2ef9c74 + 2543eca commit 10f1666
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/mollie/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions lib/mollie/customer/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions test/fixtures/subscriptions/get.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
75 changes: 75 additions & 0 deletions test/fixtures/subscriptions/get_payments.json
Original file line number Diff line number Diff line change
@@ -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
}
}
14 changes: 14 additions & 0 deletions test/mollie/customer/subscription_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 10f1666

Please sign in to comment.