Skip to content

Commit

Permalink
Merge pull request #15 from vapor-community/invoices
Browse files Browse the repository at this point in the history
Invoices
  • Loading branch information
anthonycastelli authored Sep 7, 2017
2 parents 8fd0855 + e3d96aa commit dd771ec
Show file tree
Hide file tree
Showing 13 changed files with 1,455 additions and 53 deletions.
54 changes: 7 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,64 +63,21 @@ XCTMain([

## Whats Implemented
* [x] Balance Fetching
* [x] History
* [x] Balance by ID
* [x] Charges
* [x] Creating Charges
* [x] Retrieving a charge by id
* [x] Listing all charges
* [x] Updating a charge
* [x] Capturing a charge
* [x] Customers
* [x] Creating
* [x] Updating
* [x] Deleting
* [x] Fetching by Customer ID
* [x] Listing All Customers (With filters)
* [x] Coupons
* [x] Creating
* [x] Updating
* [x] Deleting
* [x] Fetching by Coupon ID
* [x] Listing All Coupons (With filters)
* [x] Plans
* [x] Creating
* [x] Updating
* [x] Deleting
* [x] Fetching by Plan ID
* [x] Listing All Plans (With filters)
* [x] Refunds
* [x] Creating a Refund
* [x] Retrieval
* [x] Updating
* [x] Listing all
* [x] Tokens
* [x] Card Creation
* [x] Bank Creation
* [x] Token Retrieval
* [x] Sources
* [x] Creating
* [x] Updating
* [x] Fetching by Source ID
* [x] Subscriptions
* [x] Creating
* [x] Updating
* [x] Deleting
* [x] Fetching by subscription ID
* [x] Listing All Subscriptions (With filters)
* [x] Subscriptions
* [x] Connect account
* [x] Creating
* [x] Updating
* [x] Deleting
* [x] Fetching by account ID
* [x] Listing All Accounts (With filters)
* [x] Rejecting accounts
* [x] Creating dashboard login link for express accounts
* [x] Orders
* [x] Order Items
* [x] Products
* [x] Disputes
* [ ] Cards
* [x] Disputes
* [x] Invoices
* [x] Invoice Items

[stripe_home]: http://stripe.com "Stripe"
[stripe_api]: https://stripe.com/docs/api "Stripe API Endpoints"
Expand All @@ -129,3 +86,6 @@ XCTMain([
## License

Vapor Stripe Provider is available under the MIT license. See the [LICENSE](LICENSE) file for more info.

## Want to help?
Feel free to submit a pull request whether it's a clean up, a new approach to handling things, adding a new part of the API, or even if it's just a typo. All help is welcomed! 😀
43 changes: 39 additions & 4 deletions Sources/Stripe/API/Helpers/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,34 @@ internal enum API {

/**
SOURCES
Source objects allow you to accept a variety of payment methods. They represent a customer's payment instrument and can be used with the Stripe API just like a card object: once chargeable, they can be charged, or attached to customers.
Source objects allow you to accept a variety of payment methods. They represent a customer's payment instrument
and can be used with the Stripe API just like a card object: once chargeable, they can be charged, or attached
to customers.
*/
case sources
case source(String)

/**
SUBSCRIPTION ITEMS
Subscription items allow you to create customer subscriptions with more than one plan, making it easy to represent complex billing relationships.
Subscription items allow you to create customer subscriptions with more than one plan, making it easy to represent
complex billing relationships.
*/
case subscriptionItem
case subscriptionItems(String)

/**
SUBSCRIPTIONS
Subscriptions allow you to charge a customer's card on a recurring basis. A subscription ties a customer to a particular plan you've created.
Subscriptions allow you to charge a customer's card on a recurring basis. A subscription ties a customer to a
particular plan you've created.
*/
case subscription
case subscriptions(String)
case subscriptionDiscount(String)

/**
ACCOUNTS
This is an object representing your Stripe account. You can retrieve it to see properties on the account like its current e-mail address or if the account is enabled yet to make live charges.
This is an object representing your Stripe account. You can retrieve it to see properties on the account like its
current e-mail address or if the account is enabled yet to make live charges.
*/
case account
case accounts(String)
Expand Down Expand Up @@ -161,6 +166,27 @@ internal enum API {
case orderReturn
case orderReturns(String)

/**
INVOICES
Invoices are statements of what a customer owes for a particular billing period, including subscriptions,
invoice items, and any automatic proration adjustments if necessary.
*/
case invoices
case invoice(String)
case payInvoice(String)
case invoiceLines(String)
case upcomingInvoices

/**
INVOICE ITEMS
Sometimes you want to add a charge or credit to a customer but only actually charge the customer's card at
the end of a regular billing cycle. This is useful for combining several charges to minimize per-transaction
fees or having Stripe tabulate your usage-based billing totals.
*/
case invoiceItems
case invoiceItem(String)


var endpoint: String {
switch self {
case .balance: return APIBase + APIVersion + "balance"
Expand Down Expand Up @@ -220,6 +246,15 @@ internal enum API {

case .orderReturn: return APIBase + APIVersion + "order_returns"
case .orderReturns(let id): return APIBase + APIVersion + "order_returns/\(id)"

case .invoices: return APIBase + APIVersion + "invoices"
case .invoice(let id): return APIBase + APIVersion + "invoices/\(id)"
case .payInvoice(let id): return APIBase + APIVersion + "invoices/\(id)/pay"
case .invoiceLines(let id): return APIBase + APIVersion + "invoices/\(id)/lines"
case .upcomingInvoices: return APIBase + APIVersion + "invoices/upcoming"

case .invoiceItems:return APIBase + APIVersion + "invoiceitems"
case .invoiceItem(let id): return APIBase + APIVersion + "invoiceitems/\(id)"
}
}
}
165 changes: 165 additions & 0 deletions Sources/Stripe/API/Routes/InvoiceItemRoutes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//
// InvoiceItemRoutes.swift
// Stripe
//
// Created by Anthony Castelli on 9/5/17.
//
//

import Foundation
import Node
import HTTP

public final class InvoiceItemRoutes {

let client: StripeClient

init(client: StripeClient) {
self.client = client
}

/**
Create an invoice item
Adds an arbitrary charge or credit to the customer’s upcoming invoice.

- parameter customer: The ID of the customer who will be billed when this invoice item is billed.
- parameter amount: The integer amount in cents of the charge to be applied to the upcoming invoice.
To apply a credit to the customer’s account, pass a negative amount.
- parameter currency: Three-letter ISO currency code, in lowercase. Must be a supported currency.
- parameter invoice: The ID of an existing invoice to add this invoice item to. When left blank, the
invoice item will be added to the next upcoming scheduled invoice. Use this when adding
invoice items in response to an invoice.created webhook. You cannot add an invoice item
to an invoice that has already been paid, attempted or closed.
- parameter subscription: The ID of a subscription to add this invoice item to. When left blank, the invoice item
will be be added to the next upcoming scheduled invoice. When set, scheduled invoices for
subscriptions other than the specified subscription will ignore the invoice item. Use this
when you want to express that an invoice item has been accrued within the context of a
particular subscription.
- parameter description: An arbitrary string which you can attach to the invoice item. The description is displayed
in the invoice for easy tracking. This will be unset if you POST an empty value.
- parameter discountable: Controls whether discounts apply to this invoice item. Defaults to false for prorations or
negative invoice items, and true for all other invoice items.
- parameter metadata: A set of key/value pairs that you can attach to an invoice item object. It can be useful for
storing additional information about the invoice item in a structured format. You can unset
individual keys if you POST an empty value for that key. You can clear all keys if you POST
an empty value for metadata.

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/
public func createItem(forCustomer customer: String, amount: Int, inCurrency currency: StripeCurrency, toInvoice invoice: String? = nil, toSubscription subscription: String? = nil, description: String? = nil, discountable: Bool? = nil, metadata: Node? = nil) throws -> StripeRequest<InvoiceItem> {
var body = Node([:])

body["customer"] = Node(customer)
body["amount"] = Node(amount)
body["currency"] = Node(currency.rawValue)

if let subscription = subscription {
body["subscription"] = Node(subscription)
}

if let invoice = invoice {
body["invoice"] = Node(invoice)
}

if let description = description {
body["description"] = Node(description)
}

if let discountable = discountable {
body["discountable"] = Node(discountable)
}

if let metadata = metadata?.object {
for (key, value) in metadata {
body["metadata[\(key)]"] = value
}
}

return try StripeRequest(client: self.client, method: .post, route: .invoiceItems, body: Body.data(body.formURLEncoded()), headers: nil)
}

/**
Fetch an invoice
Retrieves the invoice with the given ID.

- parameter invoice: The ID of the desired invoice item.

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/
public func fetch(invoiceItem invoiceItemId: String) throws -> StripeRequest<InvoiceItem> {
return try StripeRequest(client: self.client, method: .post, route: .invoiceItem(invoiceItemId), body: nil, headers: nil)
}

/**
Update an invoice item
Updates the amount or description of an invoice item on an upcoming invoice. Updating an invoice item
is only possible before the invoice it’s attached to is closed.

- parameter amount: The integer amount in cents of the charge to be applied to the upcoming invoice.
To apply a credit to the customer’s account, pass a negative amount.
- parameter description: An arbitrary string which you can attach to the invoice item. The description is displayed
in the invoice for easy tracking. This will be unset if you POST an empty value.
- parameter discountable: Controls whether discounts apply to this invoice item. Defaults to false for prorations or
negative invoice items, and true for all other invoice items.
- parameter metadata: A set of key/value pairs that you can attach to an invoice item object. It can be useful for
storing additional information about the invoice item in a structured format. You can unset
individual keys if you POST an empty value for that key. You can clear all keys if you POST
an empty value for metadata.

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/
public func update(invoiceItem invoiceItemId: String, amount: Int, description: String? = nil, discountable: Bool? = nil, metadata: Node? = nil) throws -> StripeRequest<InvoiceItem> {
var body = Node([:])

body["amount"] = Node(amount)

if let description = description {
body["description"] = Node(description)
}

if let discountable = discountable {
body["discountable"] = Node(discountable)
}

if let metadata = metadata?.object {
for (key, value) in metadata {
body["metadata[\(key)]"] = value
}
}

return try StripeRequest(client: self.client, method: .post, route: .invoiceItem(invoiceItemId), body: Body.data(body.formURLEncoded()), headers: nil)
}

/**
Delete an invoice item
Removes an invoice item from the upcoming invoice. Removing an invoice item is only possible before the
invoice it’s attached to is closed.

- parameter invoice: The ID of the desired invoice item.

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/
public func delete(invoiceItem invoiceItemId: String) throws -> StripeRequest<DeletedObject> {
return try StripeRequest(client: self.client, method: .delete, route: .invoiceItem(invoiceItemId), body: nil, headers: nil)
}

/**
List all invoice items
Returns a list of your invoice items. Invoice items are returned sorted by creation date, with the most
recently created invoice items appearing first.

- parameter filter: A Filter item to pass query parameters when fetching results

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/
public func listAll(customer: String? = nil, filter: StripeFilter? = nil) throws -> StripeRequest<InvoiceItemList> {
var query = [String : NodeRepresentable]()
if let customer = customer {
query["customer"] = customer
}
if let data = try filter?.createQuery() {
query = data
}
return try StripeRequest(client: self.client, method: .get, route: .invoiceItems, query: query, body: nil, headers: nil)
}
}
Loading

0 comments on commit dd771ec

Please sign in to comment.