Skip to content

Commit

Permalink
Merge pull request #1 from Andrewangeta/Coupons
Browse files Browse the repository at this point in the history
Added Coupons
  • Loading branch information
anthonycastelli authored May 29, 2017
2 parents ceb4e00 + 840a014 commit 2602672
Show file tree
Hide file tree
Showing 28 changed files with 854 additions and 136 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ let object = try drop.stripe?.balance.history().serializedResponse()
~~~~
The object is returned response model, or model array.

## Testing

To avoid having to remember to add tests to `LinuxMain.swift` you can use [Sourcery][sourcery] to add your tets cases there for you. Just install the sourcery binary with Homebrew `brew install sourcery`, navigate to your project folder, and from the command line run the following:
~~~~bash
sourcery --sources Tests/ --templates Sourcery/LinuxMain.stencil --args testimports='@testable import StripeTests'
~~~~
It will generate the following with your tests added:

~~~~swift
import XCTest
@testable import StripeTests
extension BalanceTests {
static var allTests = [
("testBalance", testBalance),
...
]
}
.
.
XCTMain([
testCase(BalanceTests.allTests),
...
])
~~~~

## Whats Implemented
* [x] Balance Fetching
* [x] History
Expand All @@ -52,6 +77,12 @@ The object is returned response model, or model array.
* [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)
* [ ] Disputes
* [x] Refunds
* [x] Creating a Refund
Expand All @@ -68,3 +99,4 @@ The object is returned response model, or model array.

[stripe_home]: http://stripe.com "Stripe"
[stripe_api]: https://stripe.com/docs/api "Stripe API Endpoints"
[sourcery]: https://github.com/krzysztofzablocki/Sourcery "Sourcery"
17 changes: 17 additions & 0 deletions Sourcery/LinuxMain.stencil
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// sourcery:file:Tests/LinuxMain.swift
import XCTest
{{ argument.testimports }}

{% for type in types.classes|based:"XCTestCase" %}
{% if not type.annotations.disableTests %}extension {{ type.name }} {
static var allTests = [
{% for method in type.methods %}{% if method.parameters.count == 0 and method.shortName|hasPrefix:"test" %} ("{{ method.shortName }}", {{ method.shortName }}),
{% endif %}{% endfor %}]
}

{% endif %}{% endfor %}

XCTMain([
{% for type in types.classes|based:"XCTestCase" %}{% if not type.annotations.disableTests %} testCase({{ type.name }}.allTests),
{% endif %}{% endfor %}])
// sourcery:end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Filter.swift
// StripeFilter.swift
// Stripe
//
// Created by Anthony Castelli on 4/16/17.
Expand All @@ -12,7 +12,7 @@ import Helpers
import Node
import HTTP

public final class Filter {
public final class StripeFilter {

public init() { }

Expand All @@ -31,6 +31,26 @@ public final class Filter {
*/
public var created: Node?

/**
Can either be a UNIX timestamp, or a dictionary with these key/values

- gt: Return values where the created field is after this timestamp.
- gte: Return values where the created field is after or equal to this timestamp.
- lt: Return values where the created field is before this timestamp.
- lte: Return values where the created field is before or equal to this timestamp.

Example:
availableOn = Node(node: ["UNIX_TIMESTAMP": "gte"])
or
availableOn = Node(node: UNIX_TIMESTAMP)
*/
public var availableOn: Node?

/**
A currency
*/
public var currency: StripeCurrency?

/**
Only return charges for the customer specified by this customer ID.
*/
Expand Down Expand Up @@ -93,6 +113,21 @@ public final class Filter {
}
}

if let value = self.created {
if let value = value.object {
for (key, value) in value {
node["available_on[\(key)]"] = value
}
} else {
node["available_on"] = value
}
}

if let value = self.currency
{
node["currency"] = value.rawValue.makeNode(in: nil)
}

if let value = self.customerId {
node["customer"] = value
}
Expand Down
13 changes: 12 additions & 1 deletion Sources/API/Helpers/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal let APIVersion = "v1/"

internal let DefaultHeaders = [
HeaderKey.contentType: "application/x-www-form-urlencoded",
StripeHeader.Version: "2017-04-06"
StripeHeader.Version: "2017-05-25"
]

internal struct StripeHeader {
Expand Down Expand Up @@ -76,6 +76,14 @@ internal enum API {
case refunds
case refund(String)

/**
COUPONS
A coupon contains information about a percent-off or amount-off discount you might want to
apply to a customer. Coupons may be applied to invoices or orders.
*/
case coupons
case coupon(String)

var endpoint: String {
switch self {
case .balance: return APIBase + APIVersion + "balance"
Expand All @@ -95,6 +103,9 @@ internal enum API {

case .refunds: return APIBase + APIVersion + "refunds"
case .refund(let id): return APIBase + APIVersion + "refunds/\(id)"

case .coupons: return APIBase + APIVersion + "coupons"
case .coupon(let id): return APIBase + APIVersion + "coupons/\(id)"
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/API/Routes/BalanceRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public final class BalanceRoutes {
Returns a list of transactions that have contributed to the Stripe account balance (e.g., charges, transfers, and so forth).
The transactions are returned in sorted order, with the most recent transactions appearing first.

- parameter filter: A Filter item to ass query parameters when fetching results
- 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 history(forFilter filter: Filter?=nil) throws -> StripeRequest<BalanceHistoryList> {
public func history(forFilter filter: StripeFilter?=nil) throws -> StripeRequest<BalanceHistoryList> {
var query = [String : NodeRepresentable]()
if let data = try filter?.createQuery() {
query = data
Expand Down
4 changes: 2 additions & 2 deletions Sources/API/Routes/ChargeRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ public final class ChargeRoutes {
Returns a list of charges you’ve previously created. The charges are returned in sorted order, with the most
recent charges appearing first.

- parameter filter: A Filter item to ass query parameters when fetching results
- 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(filter: Filter?=nil) throws -> StripeRequest<ChargeList> {
public func listAll(filter: StripeFilter?=nil) throws -> StripeRequest<ChargeList> {
var query = [String : NodeRepresentable]()
if let data = try filter?.createQuery() {
query = data
Expand Down
Loading

0 comments on commit 2602672

Please sign in to comment.