Skip to content

Commit

Permalink
Merge pull request #91 from vapor-community/2019-02-19-API-Upgrade
Browse files Browse the repository at this point in the history
Updated charge routes to latest API
  • Loading branch information
anthonycastelli authored Feb 25, 2019
2 parents 82f6043 + dfeedca commit 12d82d0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 72 deletions.
139 changes: 68 additions & 71 deletions Sources/Stripe/API/Routes/ChargeRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,62 @@
import Vapor

public protocol ChargeRoutes {
func create(amount: Int, currency: StripeCurrency, applicationFee: Int?, capture: Bool?, description: String?, directAccountHeader: String?, destinationAccount: String?, destinationAmount: Int?, transferGroup: String?, onBehalfOf: String?, metadata: [String: String]?, receiptEmail: String?, shipping: ShippingLabel?, customer: String?, source: Any?, statementDescriptor: String?) throws -> Future<StripeCharge>
func create(amount: Int,
currency: StripeCurrency,
applicationFeeAmount: Int?,
capture: Bool?,
customer: String?,
description: String?,
metadata: [String: String]?,
onBehalfOf: String?,
receiptEmail: String?,
shipping: [String: Any]?,
source: Any?,
statementDescriptor: String?,
transferData: [String: Any]?,
transferGroup: String?) throws -> Future<StripeCharge>
func retrieve(charge: String) throws -> Future<StripeCharge>
func update(charge: String, customer: String?, description: String?, fraudDetails: StripeFraudDetails?, metadata: [String: String]?, receiptEmail: String?, shipping: ShippingLabel?, transferGroup: String?) throws -> Future<StripeCharge>
func update(charge: String,
customer: String?,
description: String?,
fraudDetails: [String: Any]?,
metadata: [String: String]?,
receiptEmail: String?,
shipping: [String: Any]?,
transferGroup: String?) throws -> Future<StripeCharge>
func capture(charge: String, amount: Int?, applicationFee: Int?, destinationAmount: Int?, receiptEmail: String?, statementDescriptor: String?) throws -> Future<StripeCharge>
func listAll(filter: [String: Any]?) throws -> Future<ChargesList>
}

extension ChargeRoutes {
public func create(amount: Int,
currency: StripeCurrency,
applicationFee: Int? = nil,
applicationFeeAmount: Int? = nil,
capture: Bool? = nil,
customer: String? = nil,
description: String? = nil,
directAccountHeader: String? = nil,
destinationAccount: String? = nil,
destinationAmount: Int? = nil,
transferGroup: String? = nil,
metadata: [String: String]? = nil,
onBehalfOf: String? = nil,
metadata: [String : String]? = nil,
receiptEmail: String? = nil,
shipping: ShippingLabel? = nil,
customer: String? = nil,
shipping: [String: Any]? = nil,
source: Any? = nil,
statementDescriptor: String? = nil) throws -> Future<StripeCharge> {
statementDescriptor: String? = nil,
transferData: [String: Any]? = nil,
transferGroup: String? = nil) throws -> Future<StripeCharge> {
return try create(amount: amount,
currency: currency,
applicationFee: applicationFee,
applicationFeeAmount: applicationFeeAmount,
capture: capture,
customer: customer,
description: description,
directAccountHeader: directAccountHeader,
destinationAccount: destinationAccount,
destinationAmount: destinationAmount,
transferGroup: transferGroup,
onBehalfOf: onBehalfOf,
metadata: metadata,
onBehalfOf: onBehalfOf,
receiptEmail: receiptEmail,
shipping: shipping,
customer: customer,
source: source,
statementDescriptor: statementDescriptor)
statementDescriptor: statementDescriptor,
transferData: transferData,
transferGroup: transferGroup)
}

public func retrieve(charge: String) throws -> Future<StripeCharge> {
Expand All @@ -58,10 +74,10 @@ extension ChargeRoutes {
public func update(charge chargeId: String,
customer: String? = nil,
description: String? = nil,
fraudDetails: StripeFraudDetails? = nil,
fraudDetails: [String: Any]? = nil,
metadata: [String: String]? = nil,
receiptEmail: String? = nil,
shipping: ShippingLabel? = nil,
shipping: [String: Any]? = nil,
transferGroup: String? = nil) throws -> Future<StripeCharge> {
return try update(charge: chargeId,
customer: customer,
Expand Down Expand Up @@ -103,83 +119,72 @@ public struct StripeChargeRoutes: ChargeRoutes {
/// [Learn More →](https://stripe.com/docs/api/curl#create_charge)
public func create(amount: Int,
currency: StripeCurrency,
applicationFee: Int?,
applicationFeeAmount: Int?,
capture: Bool?,
customer: String?,
description: String?,
directAccountHeader: String?,
destinationAccount: String?,
destinationAmount: Int?,
transferGroup: String?,
metadata: [String: String]?,
onBehalfOf: String?,
metadata: [String : String]?,
receiptEmail: String?,
shipping: ShippingLabel?,
customer: String?,
shipping: [String: Any]?,
source: Any?,
statementDescriptor: String?) throws -> Future<StripeCharge> {
statementDescriptor: String?,
transferData: [String: Any]?,
transferGroup: String?) throws -> Future<StripeCharge> {
var body: [String: Any] = ["amount": amount, "currency": currency.rawValue]
var headers: HTTPHeaders = [:]
if let applicationFee = applicationFee {
body["application_fee"] = applicationFee
if let applicationFeeAmount = applicationFeeAmount {
body["application_fee_amount"] = applicationFeeAmount
}

if let capture = capture {
body["capture"] = capture
}

if let directAccountHeader = directAccountHeader {
headers.replaceOrAdd(name: HTTPHeaderName.stripeAccount, value: directAccountHeader)
if let customer = customer {
body["customer"] = customer
}

if let description = description {
body["description"] = description
}

if let destinationAccount = destinationAccount {
body["destination[account]"] = destinationAccount
}

if let destinationAmount = destinationAmount {
body["destination[amount]"] = destinationAmount
}

if let transferGroup = transferGroup {
body["transfer_group"] = transferGroup
if let metadata = metadata {
metadata.forEach { body["metadata[\($0)]"] = $1}
}

if let onBehalfOf = onBehalfOf {
body["on_behalf_of"] = onBehalfOf
}

if let metadata = metadata {
metadata.forEach { body["metadata[\($0)]"] = $1}
}

if let receiptEmail = receiptEmail {
body["receipt_email"] = receiptEmail
}

if let shipping = shipping {
try shipping.toEncodedDictionary().forEach { body["shipping[\($0)]"] = $1 }
}

if let customer = customer {
body["customer"] = customer
shipping.forEach { body["shipping[\($0)]"] = $1 }
}

if let tokenSource = source as? String {
body["source"] = tokenSource
}

if let cardDictionarySource = source as? [String: Any] {
cardDictionarySource.forEach { body["source[\($0)]"] = $1 }
if let hashSource = source as? [String: Any] {
hashSource.forEach { body["source[\($0)]"] = $1 }
}

if let statementDescriptor = statementDescriptor {
body["statement_descriptor"] = statementDescriptor
}

return try request.send(method: .POST, path: StripeAPIEndpoint.charges.endpoint, body: body.queryParameters, headers: headers)
if let transferData = transferData {
transferData.forEach { body["transfer_data[\($0)]"] = $1 }
}

if let transferGroup = transferGroup {
body["transfer_group"] = transferGroup
}

return try request.send(method: .POST, path: StripeAPIEndpoint.charges.endpoint, body: body.queryParameters)
}

/// Retrieve a charge
Expand All @@ -193,10 +198,10 @@ public struct StripeChargeRoutes: ChargeRoutes {
public func update(charge chargeId: String,
customer: String?,
description: String?,
fraudDetails: StripeFraudDetails?,
fraudDetails: [String: Any]?,
metadata: [String: String]?,
receiptEmail: String?,
shipping: ShippingLabel?,
shipping: [String: Any]?,
transferGroup: String?) throws -> Future<StripeCharge> {
var body: [String: Any] = [:]

Expand All @@ -209,27 +214,19 @@ public struct StripeChargeRoutes: ChargeRoutes {
}

if let fraud = fraudDetails {
if let userReport = fraud.userReport?.rawValue {
body["fraud_details[user_report]"] = userReport
}

if let stripeReport = fraud.stripeReport?.rawValue {
body["fraud_details[stripe_report]"] = stripeReport
}
fraud.forEach { body["fraud_details[\($0)]"] = $1 }
}

if let metadata = metadata {
metadata.forEach { key, value in
body["metadata[\(key)]"] = value
}
metadata.forEach { body["metadata[\($0)]"] = $1 }
}

if let receiptEmail = receiptEmail {
body["receipt_email"] = receiptEmail
}

if let shipping = shipping {
try shipping.toEncodedDictionary().forEach { body["shipping[\($0)]"] = $1 }
shipping.forEach { body["shipping[\($0)]"] = $1 }
}

if let transferGroup = transferGroup {
Expand Down
11 changes: 11 additions & 0 deletions Sources/Stripe/Models/Charges/Charge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public struct StripeCharge: StripeModel {
public var amountRefunded: Int?
public var application: String?
public var applicationFee: String?
public var applicationFeeAmount: Int?
public var balanceTransaction: String?
public var captured: Bool
public var created: Date?
Expand All @@ -38,6 +39,7 @@ public struct StripeCharge: StripeModel {
public var order: String?
public var outcome: StripeOutcome?
public var paid: Bool?
public var paymentIntent: String?
public var receiptEmail: String?
public var receiptNumber: String?
public var refunded: Bool?
Expand All @@ -49,6 +51,7 @@ public struct StripeCharge: StripeModel {
public var statementDescriptor: String?
public var status: StripeStatus?
public var transfer: String?
public var transferData: StripeChargeTransferData?
public var transferGroup: String?

public enum CodingKeys: String, CodingKey {
Expand All @@ -58,6 +61,7 @@ public struct StripeCharge: StripeModel {
case amountRefunded = "amount_refunded"
case application
case applicationFee = "application_fee"
case applicationFeeAmount = "application_fee_amount"
case balanceTransaction = "balance_transaction"
case captured
case created
Expand All @@ -76,6 +80,7 @@ public struct StripeCharge: StripeModel {
case order
case outcome
case paid
case paymentIntent = "payment_intent"
case receiptEmail = "receipt_email"
case receiptNumber = "receipt_number"
case refunded
Expand All @@ -87,6 +92,12 @@ public struct StripeCharge: StripeModel {
case statementDescriptor = "statement_descriptor"
case status
case transfer
case transferData = "transfer_data"
case transferGroup = "transfer_group"
}
}

public struct StripeChargeTransferData: StripeModel {
public var amount: Int?
public var destination: String?
}
12 changes: 11 additions & 1 deletion Tests/StripeTests/ChargeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ChargeTests: XCTestCase {
"amount_refunded": 0,
"application": "oops",
"application_fee": "fee_something",
"application_fee_amount": 300,
"balance_transaction": "txn_19XJJ02eZvKYlo2ClwuJ1rbA",
"captured": false,
"created": 1517704056,
Expand All @@ -45,6 +46,7 @@ class ChargeTests: XCTestCase {
"type": "issuer_declined"
},
"paid": false,
"payment_intent": "bop",
"receipt_email": "[email protected]",
"receipt_number": "some number",
"refunded": false,
Expand All @@ -61,7 +63,11 @@ class ChargeTests: XCTestCase {
"source_transfer": "sickness",
"statement_descriptor": "for a shirt",
"status": "failed",
"transfer_group": "group a"
"transfer_group": "group a",
"transfer_data": {
"amount": 45,
"destination": "acc_123"
}
}
"""

Expand All @@ -83,6 +89,7 @@ class ChargeTests: XCTestCase {
XCTAssertEqual(charge.amountRefunded, 0)
XCTAssertEqual(charge.application, "oops")
XCTAssertEqual(charge.applicationFee, "fee_something")
XCTAssertEqual(charge.applicationFeeAmount, 300)
XCTAssertEqual(charge.balanceTransaction, "txn_19XJJ02eZvKYlo2ClwuJ1rbA")
XCTAssertEqual(charge.captured, false)
XCTAssertEqual(charge.created, Date(timeIntervalSince1970: 1517704056))
Expand All @@ -103,6 +110,7 @@ class ChargeTests: XCTestCase {
XCTAssertEqual(charge.outcome?.type, .issuerDeclined)

XCTAssertEqual(charge.paid, false)
XCTAssertEqual(charge.paymentIntent, "bop")
XCTAssertEqual(charge.receiptEmail, "[email protected]")
XCTAssertEqual(charge.receiptNumber, "some number")
XCTAssertEqual(charge.refunded, false)
Expand All @@ -111,6 +119,8 @@ class ChargeTests: XCTestCase {
XCTAssertEqual(charge.statementDescriptor, "for a shirt")
XCTAssertEqual(charge.status, .failed)
XCTAssertEqual(charge.transferGroup, "group a")
XCTAssertEqual(charge.transferData?.amount, 45)
XCTAssertEqual(charge.transferData?.destination, "acc_123")

}.catch { (error) in
XCTFail("\(error)")
Expand Down

0 comments on commit 12d82d0

Please sign in to comment.