Issue #859

To parse PKPayment and used with Wyre CreateAppleOrder API, we can declare some Encodable structs

import PassKit
import Foundation

struct PaymentObject: Encodable {
    var billingContact: Contact?
    var shippingContact: Contact?
    var token: JSONValue
}

extension PaymentObject {
    struct Contact: Encodable {
        var addressLines: [String]?
        var country: String?
        var countryCode: String?
        var familyName: String?
        var givenname: String?
        var locality: String?
        var postalCode: String?
        var administrativeArea: String?
        var subAdministrativeArea: String?
        var subLocality: String?
    }
}

extension PaymentObject {
    init(payment: PKPayment) throws {
        let json = try JSONSerialization.jsonObject(
            with: payment.token.paymentData,
            options: .mutableContainers
        )

        self.init(
            billingContact: Contact(contact: payment.billingContact),
            shippingContact: Contact(contact: payment.shippingContact),
            token: JSONValue(any: json)
        )
    }
}

extension PaymentObject.Contact {
    init?(contact: PKContact?) {
        guard let contact = contact else { return nil }

        self.country = contact.postalAddress?.country
        self.countryCode = contact.postalAddress?.isoCountryCode
        self.postalCode = contact.postalAddress?.postalCode
        self.familyName = contact.name?.familyName
        self.givenname = contact.name?.givenName
        self.locality = contact.postalAddress?.city
        self.subLocality = contact.postalAddress?.subLocality
        self.administrativeArea = contact.postalAddress?.state
        self.subAdministrativeArea = contact.postalAddress?.subAdministrativeArea
        self.addressLines = contact.postalAddress?.street
            .split(separator: "\n")
            .map({ String($0) })
    }
}

Read more