Skip to content

Commit

Permalink
Model PaymentIntent metadata matching Stripe
Browse files Browse the repository at this point in the history
Previously, for any PaymentIntent metadata created using our convenience initializers, we stored the orderID as an Int64, however Stripe would transform this to a String. This is due to Stripe sending the metadata over the network in a x-www-urlformencoded request, without any type information, and returning it as a String.

This meant that the type stored in the [AnyHashable: Any] for orderID could be a String, or an Int64, depending how the PaymentIntent was created (in tests with the convenience intitializer, vs from Stripe's network response.)

This change maintains the Int64 for a convenient(!) convenience init, while also matching the real-life Stripe metadata for accuracy in tests.
  • Loading branch information
joshheald committed Sep 30, 2021
1 parent dd867a6 commit a6ced2b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
10 changes: 6 additions & 4 deletions Hardware/Hardware/CardReader/PaymentIntent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct PaymentIntent: Identifiable {
public let currency: String

/// Set of key-value pairs attached to the object.
public let metadata: [AnyHashable: Any]?
public let metadata: [String: String]?

// Charges that were created by this PaymentIntent, if any.
public let charges: [Charge]
Expand Down Expand Up @@ -89,14 +89,16 @@ public extension PaymentIntent {
orderID: Int64? = nil,
orderKey: String? = nil,
paymentType: PaymentTypes? = nil
) -> [AnyHashable: Any] {
var metadata = [AnyHashable: Any]()
) -> [String: String] {
var metadata = [String: String]()

metadata[PaymentIntent.MetadataKeys.store] = store
metadata[PaymentIntent.MetadataKeys.customerName] = customerName
metadata[PaymentIntent.MetadataKeys.customerEmail] = customerEmail
metadata[PaymentIntent.MetadataKeys.siteURL] = siteURL
metadata[PaymentIntent.MetadataKeys.orderID] = orderID
if let orderID = orderID {
metadata[PaymentIntent.MetadataKeys.orderID] = String(orderID)
}
metadata[PaymentIntent.MetadataKeys.orderKey] = orderKey
metadata[PaymentIntent.MetadataKeys.paymentType] = paymentType?.rawValue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extension PaymentIntent {
self.created = intent.created
self.amount = intent.amount
self.currency = intent.currency
self.metadata = intent.metadata
self.metadata = intent.metadata as? [String: String]
self.charges = intent.charges.map { .init(charge: $0) }
}
}
Expand Down
16 changes: 8 additions & 8 deletions Hardware/HardwareTests/PaymentIntentMetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class PaymentIntentMetadataTests: XCTestCase {
func test_non_nil_store() throws {
let metadata = PaymentIntent.initMetadata(store: "foo")
let store = try XCTUnwrap(metadata["paymentintent.storename"])
XCTAssertEqual(store as? String, "foo")
XCTAssertEqual(store, "foo")
XCTAssertEqual(metadata.count, 1)
}

Expand All @@ -33,7 +33,7 @@ final class PaymentIntentMetadataTests: XCTestCase {
func test_non_nil_customer_name() throws {
let metadata = PaymentIntent.initMetadata(customerName: "foo")
let customerName = try XCTUnwrap(metadata["customer_name"])
XCTAssertEqual(customerName as? String, "foo")
XCTAssertEqual(customerName, "foo")
XCTAssertEqual(metadata.count, 1)
}

Expand All @@ -48,7 +48,7 @@ final class PaymentIntentMetadataTests: XCTestCase {
func test_non_nil_customer_email() throws {
let metadata = PaymentIntent.initMetadata(customerEmail: "foo")
let customerEmail = try XCTUnwrap(metadata["customer_email"])
XCTAssertEqual(customerEmail as? String, "foo")
XCTAssertEqual(customerEmail, "foo")
XCTAssertEqual(metadata.count, 1)
}

Expand All @@ -63,7 +63,7 @@ final class PaymentIntentMetadataTests: XCTestCase {
func test_non_nil_site_url() throws {
let metadata = PaymentIntent.initMetadata(siteURL: "foo")
let siteURL = try XCTUnwrap(metadata["site_url"])
XCTAssertEqual(siteURL as? String, "foo")
XCTAssertEqual(siteURL, "foo")
XCTAssertEqual(metadata.count, 1)
}

Expand All @@ -78,7 +78,7 @@ final class PaymentIntentMetadataTests: XCTestCase {
func test_non_nil_orderID() throws {
let metadata = PaymentIntent.initMetadata(orderID: 1234)
let orderID = try XCTUnwrap(metadata["order_id"])
XCTAssertEqual(orderID as? Int64, 1234)
XCTAssertEqual(orderID, "1234")
XCTAssertEqual(metadata.count, 1)
}

Expand All @@ -93,7 +93,7 @@ final class PaymentIntentMetadataTests: XCTestCase {
func test_non_nil_order_key() throws {
let metadata = PaymentIntent.initMetadata(orderKey: "wc_order_0000000000000")
let orderKey = try XCTUnwrap(metadata["order_key"])
XCTAssertEqual(orderKey as? String, "wc_order_0000000000000")
XCTAssertEqual(orderKey, "wc_order_0000000000000")
XCTAssertEqual(metadata.count, 1)
}

Expand All @@ -108,7 +108,7 @@ final class PaymentIntentMetadataTests: XCTestCase {
func test_single_payment_type() throws {
let metadata = PaymentIntent.initMetadata(paymentType: PaymentIntent.PaymentTypes.single)
let paymentType = try XCTUnwrap(metadata["payment_type"])
XCTAssertEqual(paymentType as? String, "single")
XCTAssertEqual(paymentType, "single")
XCTAssertEqual(metadata.count, 1)
}

Expand All @@ -118,7 +118,7 @@ final class PaymentIntentMetadataTests: XCTestCase {
func test_recurring_payment_type() throws {
let metadata = PaymentIntent.initMetadata(paymentType: PaymentIntent.PaymentTypes.recurring)
let paymentType = try XCTUnwrap(metadata["payment_type"])
XCTAssertEqual(paymentType as? String, "recurring")
XCTAssertEqual(paymentType, "recurring")
XCTAssertEqual(metadata.count, 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public extension PaymentIntent {
return CardPresentReceiptParameters(amount: amount,
currency: currency,
date: created,
storeName: metadata?[CardPresentReceiptParameters.MetadataKeys.store] as? String,
storeName: metadata?[CardPresentReceiptParameters.MetadataKeys.store],
cardDetails: cardDetails)
}
}

0 comments on commit a6ced2b

Please sign in to comment.