Skip to content

Commit

Permalink
Make CheckoutCompleted event types stricter (#141)
Browse files Browse the repository at this point in the history
* Make CheckoutCompleted event types stricter

* Fix tests

* Remove redundant CodingKeys
  • Loading branch information
markmur committed Mar 8, 2024
1 parent 6dcb4f8 commit a11c02a
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ extension CartViewController: CheckoutDelegate {
func checkoutDidComplete(event: ShopifyCheckoutSheetKit.CheckoutCompletedEvent) {
resetCart()

if let orderId = event.orderDetails?.id {
ShopifyCheckoutSheetKit.configuration.logger.log("Order created: \(orderId)")
}
ShopifyCheckoutSheetKit.configuration.logger.log("Order created: \(event.orderDetails.id)")
}

func checkoutDidCancel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
{
"sourceLanguage" : "en",
"strings" : {
"✓" : {

},
"App version" : {

},
"Clear" : {

},
"Clear logs" : {

},
"Events" : {

},
"Features" : {

},
"Logs" : {

},
"No logs available" : {

},
"OK" : {
"comment" : "Default action"
},
"Prefill buyer information" : {

},
"Preload checkout" : {

},
"SDK version" : {

},
"Settings" : {

},
"shopify_checkout_sheet_title" : {
"comment" : "The title of the checkout sheet.",
"extractionState" : "manual",
Expand All @@ -24,7 +63,16 @@
}
}
}
},
"Theme" : {

},
"Version" : {

},
"Web pixel events" : {

}
},
"version" : "1.0"
}
}
2 changes: 1 addition & 1 deletion Samples/SwiftUIExample/SwiftUIExample/CartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct CartView: View {
}
}
.onComplete { checkout in
print("Checkout completed - Order id: \(String(describing: checkout.orderDetails?.id))")
print("Checkout completed - Order id: \(String(describing: checkout.orderDetails.id))")
}
.onFail { error in
print(error)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ extension CheckoutBridge {
self = .checkoutComplete(event: checkoutCompletedEvent)
} catch {
logger.logError(error, "Error decoding CheckoutCompletedEvent")
self = .checkoutComplete(event: CheckoutCompletedEvent())
self = .checkoutComplete(event: emptyCheckoutCompletedEvent)
}
case "error":
// needs to support .checkoutUnavailable by parsing error payload on body
Expand Down
216 changes: 84 additions & 132 deletions Sources/ShopifyCheckoutSheetKit/CheckoutCompletedEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,175 +26,127 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO
import Foundation

public struct CheckoutCompletedEvent: Decodable {
public let orderDetails: OrderDetails?

enum CodingKeys: String, CodingKey {
case orderDetails
}

init() {
orderDetails = nil
}
public let orderDetails: OrderDetails
}

extension CheckoutCompletedEvent {
public struct OrderDetails: Decodable {
public let id: String?
public let email: String?
public let phone: String?
public let cart: CartInfo?
public let paymentMethods: [OrderPaymentMethod]?
public let billingAddress: Address?
public let deliveries: [DeliveryInfo]?

enum CodingKeys: String, CodingKey {
case id
case email
case phone
case cart
case paymentMethods
case billingAddress
case deliveries
}
}

public struct CartInfo: Decodable {
public let lines: [CartLine]?
public let price: PriceSet?

enum CodingKeys: String, CodingKey {
case lines
case price
}
}

public struct OrderPaymentMethod: Decodable {
public let type: String?
public let details: [String: String?]

enum CodingKeys: String, CodingKey {
case type
case details
}
}

public struct Address: Decodable {
public let referenceId: String?
public let name: String?
public let firstName: String?
public let lastName: String?
public let address1: String?
public let address2: String?
public let city: String?
public let countryCode: String?
public let zoneCode: String?
public let postalCode: String?
public let firstName: String?
public let lastName: String?
public let name: String?
public let phone: String?
public let postalCode: String?
public let referenceId: String?
public let zoneCode: String?
}

enum CodingKeys: String, CodingKey {
case referenceId
case name
case firstName
case lastName
case address1
case address2
case city
case countryCode
case zoneCode
case postalCode
case phone
}
public struct CartInfo: Decodable {
public let lines: [CartLine]
public let price: Price
public let token: String
}

public struct DeliveryInfo: Decodable {
public let method: String?
public let details: DeliveryDetails?
public struct CartLineImage: Decodable {
public let altText: String?
public let lg: String
public let md: String
public let sm: String
}

enum CodingKeys: String, CodingKey {
case method
case details
}
public struct CartLine: Decodable {
public let discounts: [Discount]?
public let image: CartLineImage?
public let merchandiseId: String?
public let price: Money
public let productId: String?
public let quantity: Int
public let title: String
}

public struct DeliveryDetails: Decodable {
public let name: String?
public let location: Address?
public let additionalInfo: String?

enum CodingKeys: String, CodingKey {
case name
case location
case additionalInfo
}
public let location: Address?
public let name: String?
}

public struct PriceSet: Decodable {
public let subtotal: Money?
public let total: Money?
public let taxes: Money?
public let discounts: [Discount]?
public let shipping: Money?

enum CodingKeys: String, CodingKey {
case subtotal
case total
case taxes
case discounts
case shipping
}
public struct DeliveryInfo: Decodable {
public let details: DeliveryDetails
public let method: String
}

public struct Discount: Decodable {
public let title: String?
public let amount: Money?
public let applicationType: String?
public let valueType: String?
public let title: String?
public let value: Double?
public let valueType: String?
}

public struct CartLineImage: Decodable {
public let sm: String?
public let md: String?
public let lg: String?
public let altText: String?
public struct OrderDetails: Decodable {
public let billingAddress: Address?
public let cart: CartInfo
public let deliveries: [DeliveryInfo]?
public let email: String?
public let id: String
public let paymentMethods: [PaymentMethod]?
public let phone: String?
}

enum CodingKeys: String, CodingKey {
case sm
case md
case lg
case altText
}
public struct PaymentMethod: Decodable {
public let details: [String: String?]
public let type: String
}

public struct CartLine: Decodable {
public let title: String?
public let quantity: Int?
public let price: Money?
public let image: CartLineImage?
public let merchandiseId: String?
public let productId: String?
public struct Price: Decodable {
public let discounts: [Discount]?

enum CodingKeys: String, CodingKey {
case title
case quantity
case price
case image
case merchandiseId
case productId
case discounts
}
public let shipping: Money?
public let subtotal: Money?
public let taxes: Money?
public let total: Money?
}

public struct Money: Decodable {
public let amount: Double?
public let currencyCode: String?

enum CodingKeys: String, CodingKey {
case amount
case currencyCode
}
}
}

// swiftlint:enable identifier_name

internal let emptyCheckoutCompletedEvent = CheckoutCompletedEvent(
orderDetails: CheckoutCompletedEvent.OrderDetails(
billingAddress: CheckoutCompletedEvent.Address(
address1: nil,
address2: nil,
city: nil,
countryCode: nil,
firstName: nil,
lastName: nil,
name: nil,
phone: nil,
postalCode: nil,
referenceId: nil,
zoneCode: nil
),
cart: CheckoutCompletedEvent.CartInfo(
lines: [],
price: CheckoutCompletedEvent.Price(
discounts: nil,
shipping: CheckoutCompletedEvent.Money(amount: nil, currencyCode: nil),
subtotal: CheckoutCompletedEvent.Money(amount: nil, currencyCode: nil),
taxes: CheckoutCompletedEvent.Money(amount: nil, currencyCode: nil),
total: CheckoutCompletedEvent.Money(amount: nil, currencyCode: nil)
),
token: ""
),
deliveries: nil,
email: nil,
id: "",
paymentMethods: nil,
phone: nil
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CheckoutCompletedEventDecoder {
let messageBody = try container.decode(String.self, forKey: .body)

guard let data = messageBody.data(using: .utf8) else {
return CheckoutCompletedEvent()
return emptyCheckoutCompletedEvent
}

return try JSONDecoder().decode(CheckoutCompletedEvent.self, from: data)
Expand Down
Loading

0 comments on commit a11c02a

Please sign in to comment.