From dfeedca3fd663d91f662d68098a10280b364d538 Mon Sep 17 00:00:00 2001 From: Andrew Edwards Date: Sun, 24 Feb 2019 20:24:53 -0500 Subject: [PATCH] Updated charge routes to latest API --- Sources/Stripe/API/Routes/ChargeRoutes.swift | 139 +++++++++---------- Sources/Stripe/Models/Charges/Charge.swift | 11 ++ Tests/StripeTests/ChargeTests.swift | 12 +- 3 files changed, 90 insertions(+), 72 deletions(-) diff --git a/Sources/Stripe/API/Routes/ChargeRoutes.swift b/Sources/Stripe/API/Routes/ChargeRoutes.swift index 3316533..82c1411 100644 --- a/Sources/Stripe/API/Routes/ChargeRoutes.swift +++ b/Sources/Stripe/API/Routes/ChargeRoutes.swift @@ -9,9 +9,29 @@ 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 + 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 func retrieve(charge: String) throws -> Future - func update(charge: String, customer: String?, description: String?, fraudDetails: StripeFraudDetails?, metadata: [String: String]?, receiptEmail: String?, shipping: ShippingLabel?, transferGroup: String?) throws -> Future + func update(charge: String, + customer: String?, + description: String?, + fraudDetails: [String: Any]?, + metadata: [String: String]?, + receiptEmail: String?, + shipping: [String: Any]?, + transferGroup: String?) throws -> Future func capture(charge: String, amount: Int?, applicationFee: Int?, destinationAmount: Int?, receiptEmail: String?, statementDescriptor: String?) throws -> Future func listAll(filter: [String: Any]?) throws -> Future } @@ -19,36 +39,32 @@ public protocol ChargeRoutes { 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 { + statementDescriptor: String? = nil, + transferData: [String: Any]? = nil, + transferGroup: String? = nil) throws -> Future { 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 { @@ -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 { return try update(charge: chargeId, customer: customer, @@ -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 { + statementDescriptor: String?, + transferData: [String: Any]?, + transferGroup: String?) throws -> Future { 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 @@ -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 { var body: [String: Any] = [:] @@ -209,19 +214,11 @@ 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 { @@ -229,7 +226,7 @@ public struct StripeChargeRoutes: ChargeRoutes { } if let shipping = shipping { - try shipping.toEncodedDictionary().forEach { body["shipping[\($0)]"] = $1 } + shipping.forEach { body["shipping[\($0)]"] = $1 } } if let transferGroup = transferGroup { diff --git a/Sources/Stripe/Models/Charges/Charge.swift b/Sources/Stripe/Models/Charges/Charge.swift index 2217b28..fdbaeb2 100644 --- a/Sources/Stripe/Models/Charges/Charge.swift +++ b/Sources/Stripe/Models/Charges/Charge.swift @@ -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? @@ -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? @@ -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 { @@ -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 @@ -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 @@ -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? +} diff --git a/Tests/StripeTests/ChargeTests.swift b/Tests/StripeTests/ChargeTests.swift index 6a6591b..554b228 100644 --- a/Tests/StripeTests/ChargeTests.swift +++ b/Tests/StripeTests/ChargeTests.swift @@ -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, @@ -45,6 +46,7 @@ class ChargeTests: XCTestCase { "type": "issuer_declined" }, "paid": false, + "payment_intent": "bop", "receipt_email": "a@b.com", "receipt_number": "some number", "refunded": false, @@ -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" + } } """ @@ -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)) @@ -103,6 +110,7 @@ class ChargeTests: XCTestCase { XCTAssertEqual(charge.outcome?.type, .issuerDeclined) XCTAssertEqual(charge.paid, false) + XCTAssertEqual(charge.paymentIntent, "bop") XCTAssertEqual(charge.receiptEmail, "a@b.com") XCTAssertEqual(charge.receiptNumber, "some number") XCTAssertEqual(charge.refunded, false) @@ -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)")