Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for paying an invoice #80

Merged
merged 1 commit into from
Aug 19, 2016
Merged
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
104 changes: 104 additions & 0 deletions fixture/vcr_cassettes/invoices_test/pay.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
[
{
"request": {
"body": "customer=cus_8r2jCf6wk8YLyU&amount=10&currency=usd&subscription=sub_8r2j78r42DD6xb",
"headers": {
"Authorization": "Bearer non_empty_secret_key_string",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Stripe/v1 stripity-stripe/1.4.0"
},
"method": "post",
"options": [],
"request_body": "",
"url": "https://api.stripe.com/v1/invoiceitems"
},
"response": {
"body": "{\n \"id\": \"ii_18joevGTLcoLJlYWnw0wFBA1\",\n \"object\": \"invoiceitem\",\n \"amount\": 10,\n \"currency\": \"usd\",\n \"customer\": \"cus_8r2jCf6wk8YLyU\",\n \"date\": 1471519565,\n \"description\": null,\n \"discountable\": true,\n \"invoice\": null,\n \"livemode\": false,\n \"metadata\": {},\n \"period\": {\n \"start\": 1471519565,\n \"end\": 1471519565\n },\n \"plan\": null,\n \"proration\": false,\n \"quantity\": null,\n \"subscription\": \"sub_8r2j78r42DD6xb\"\n}\n",
"headers": {
"Server": "nginx",
"Date": "Thu, 18 Aug 2016 11:26:05 GMT",
"Content-Type": "application/json",
"Content-Length": "433",
"Connection": "keep-alive",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS, DELETE",
"Access-Control-Allow-Origin": "*",
"Access-Control-Max-Age": "300",
"Cache-Control": "no-cache, no-store",
"Request-Id": "req_91sP1taoWesj9j",
"Stripe-Version": "2016-03-07",
"Strict-Transport-Security": "max-age=31556926; includeSubDomains"
},
"status_code": 200,
"type": "ok"
}
},
{
"request": {
"body": "customer=cus_8r2jCf6wk8YLyU&subscription=sub_8r2j78r42DD6xb",
"headers": {
"Authorization": "Bearer non_empty_secret_key_string",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Stripe/v1 stripity-stripe/1.4.0"
},
"method": "post",
"options": [],
"request_body": "",
"url": "https://api.stripe.com/v1/invoices"
},
"response": {
"body": "{\n \"id\": \"in_18joevGTLcoLJlYWh0wfFUIS\",\n \"object\": \"invoice\",\n \"amount_due\": 10,\n \"application_fee\": null,\n \"attempt_count\": 0,\n \"attempted\": false,\n \"charge\": null,\n \"closed\": false,\n \"currency\": \"usd\",\n \"customer\": \"cus_8r2jCf6wk8YLyU\",\n \"date\": 1471519565,\n \"description\": null,\n \"discount\": null,\n \"ending_balance\": null,\n \"forgiven\": false,\n \"lines\": {\n \"object\": \"list\",\n \"data\": [\n {\n \"id\": \"ii_18joevGTLcoLJlYWnw0wFBA1\",\n \"object\": \"line_item\",\n \"amount\": 10,\n \"currency\": \"usd\",\n \"description\": null,\n \"discountable\": true,\n \"livemode\": false,\n \"metadata\": {},\n \"period\": {\n \"start\": 1471519565,\n \"end\": 1471519565\n },\n \"plan\": null,\n \"proration\": false,\n \"quantity\": null,\n \"subscription\": \"sub_8r2j78r42DD6xb\",\n \"type\": \"invoiceitem\"\n }\n ],\n \"has_more\": false,\n \"total_count\": 1,\n \"url\": \"/v1/invoices/in_18joevGTLcoLJlYWh0wfFUIS/lines\"\n },\n \"livemode\": false,\n \"metadata\": {},\n \"next_payment_attempt\": 1471523165,\n \"paid\": false,\n \"period_end\": 1471519565,\n \"period_start\": 1471519559,\n \"receipt_number\": null,\n \"starting_balance\": 0,\n \"statement_descriptor\": null,\n \"subscription\": \"sub_8r2j78r42DD6xb\",\n \"subtotal\": 10,\n \"tax\": null,\n \"tax_percent\": null,\n \"total\": 10,\n \"webhooks_delivered_at\": null\n}\n",
"headers": {
"Server": "nginx",
"Date": "Thu, 18 Aug 2016 11:26:05 GMT",
"Content-Type": "application/json",
"Content-Length": "1400",
"Connection": "keep-alive",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS, DELETE",
"Access-Control-Allow-Origin": "*",
"Access-Control-Max-Age": "300",
"Cache-Control": "no-cache, no-store",
"Request-Id": "req_91sP4o4cjeggzt",
"Stripe-Version": "2016-03-07",
"Strict-Transport-Security": "max-age=31556926; includeSubDomains"
},
"status_code": 200,
"type": "ok"
}
},
{
"request": {
"body": "",
"headers": {
"Authorization": "Bearer non_empty_secret_key_string",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Stripe/v1 stripity-stripe/1.4.0"
},
"method": "post",
"options": [],
"request_body": "",
"url": "https://api.stripe.com/v1/invoices/in_18joevGTLcoLJlYWh0wfFUIS/pay"
},
"response": {
"body": "{\n \"id\": \"in_18joevGTLcoLJlYWh0wfFUIS\",\n \"object\": \"invoice\",\n \"amount_due\": 0,\n \"application_fee\": null,\n \"attempt_count\": 0,\n \"attempted\": true,\n \"charge\": null,\n \"closed\": true,\n \"currency\": \"usd\",\n \"customer\": \"cus_8r2jCf6wk8YLyU\",\n \"date\": 1471519565,\n \"description\": null,\n \"discount\": null,\n \"ending_balance\": 10,\n \"forgiven\": false,\n \"lines\": {\n \"object\": \"list\",\n \"data\": [\n {\n \"id\": \"ii_18joevGTLcoLJlYWnw0wFBA1\",\n \"object\": \"line_item\",\n \"amount\": 10,\n \"currency\": \"usd\",\n \"description\": null,\n \"discountable\": true,\n \"livemode\": false,\n \"metadata\": {},\n \"period\": {\n \"start\": 1471519565,\n \"end\": 1471519565\n },\n \"plan\": null,\n \"proration\": false,\n \"quantity\": null,\n \"subscription\": \"sub_8r2j78r42DD6xb\",\n \"type\": \"invoiceitem\"\n }\n ],\n \"has_more\": false,\n \"total_count\": 1,\n \"url\": \"/v1/invoices/in_18joevGTLcoLJlYWh0wfFUIS/lines\"\n },\n \"livemode\": false,\n \"metadata\": {},\n \"next_payment_attempt\": null,\n \"paid\": true,\n \"period_end\": 1471519565,\n \"period_start\": 1471519559,\n \"receipt_number\": null,\n \"starting_balance\": 0,\n \"statement_descriptor\": null,\n \"subscription\": \"sub_8r2j78r42DD6xb\",\n \"subtotal\": 10,\n \"tax\": null,\n \"tax_percent\": null,\n \"total\": 10,\n \"webhooks_delivered_at\": 1471519565\n}\n",
"headers": {
"Server": "nginx",
"Date": "Thu, 18 Aug 2016 11:26:06 GMT",
"Content-Type": "application/json",
"Content-Length": "1394",
"Connection": "keep-alive",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS, DELETE",
"Access-Control-Allow-Origin": "*",
"Access-Control-Max-Age": "300",
"Cache-Control": "no-cache, no-store",
"Request-Id": "req_91sP6YoQEIPIRz",
"Stripe-Version": "2016-03-07",
"Strict-Transport-Security": "max-age=31556926; includeSubDomains"
},
"status_code": 200,
"type": "ok"
}
}
]
23 changes: 23 additions & 0 deletions lib/stripe/invoices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,27 @@
Stripe.make_request_with_key(:get, "#{@endpoint}/upcoming", key, params)
|> Stripe.Util.handle_stripe_response
end

@doc """
Stripe automatically creates and then attempts to pay invoices for customers on subscriptions.
However, if you’d like to attempt to collect payment on an invoice out of the normal retry schedule or for some other reason, you can do so.

## Example
{:ok, invoice} = Stripe.Invoices.pay "invoice_id"
"""
def pay(invoice_id) do
pay invoice_id, Stripe.config_or_env_key
end

@doc """
Stripe automatically creates and then attempts to pay invoices for customers on subscriptions.
However, if you’d like to attempt to collect payment on an invoice out of the normal retry schedule or for some other reason, you can do so.

## Example
{:ok, invoice} = Stripe.Invoices.pay "invoice_id", key
"""
def pay(invoice_id, key) do
Stripe.make_request_with_key(:post, "#{@endpoint}/#{invoice_id}/pay", key)
|> Stripe.Util.handle_stripe_response
end
end
23 changes: 23 additions & 0 deletions test/stripe/invoices_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,27 @@ defmodule Stripe.InvoicesTest do
assert upcoming_invoice[:customer] == customer1.id
end
end

@tag disabled: false
test "Pay an invoice", %{customer1: customer1, sub1: sub1} do
use_cassette "invoices_test/pay", match_requests_on: [:query, :request_body] do
params = [
customer: customer1.id,
amount: 10,
currency: "usd",
subscription: sub1.id
]
{:ok, _} = Stripe.InvoiceItems.create params

{:ok, invoice} = Stripe.Invoices.create(customer1.id, [subscription: sub1.id])
|> Tuple.to_list
|> Enum.at(1)
|> Map.fetch!(:id)
|> Stripe.Invoices.pay

assert invoice.attempted == true
assert invoice.closed == true
assert invoice.object == "invoice"
end
end
end