@@ -98,40 +98,39 @@ class ApplePayExecutor: NSObject, ApplePayContextDelegate {
9898}
9999
100100extension ApplePayExecutor {
101- func transformPKContactToJSON( contact: PKContact ? ) -> JSArray {
101+ func transformPKContactToJSON( contact: PKContact ? ) -> JSObject {
102+ guard let contact else {
103+ return [ : ]
104+ }
105+
106+
107+ let name = contact. name
108+ let address = contact. postalAddress
109+
102110 var nameFormatted = " "
103111 if #available( iOS 15 . 0 , * ) {
104- nameFormatted = ( contact? . name? . nameSuffix as? String ?? " " )
105- }
106- var dataString = " [{ " +
107- " \" givenName \" : \" \( contact? . name? . givenName as? String ?? " " ) \" , " +
108- " \" familyName \" : \" \( contact? . name? . familyName as? String ?? " " ) \" , " +
109- " \" middleName \" : \" \( contact? . name? . middleName as? String ?? " " ) \" , " +
110- " \" namePrefix \" : \" \( contact? . name? . namePrefix as? String ?? " " ) \" , " +
111- " \" nameSuffix \" : \" \( contact? . name? . nameSuffix as? String ?? " " ) \" , " +
112- " \" nameFormatted \" : \" \( nameFormatted) \" , " +
113- " \" phoneNumber \" : \" \( contact? . phoneNumber? . stringValue as? String ?? " " ) \" , " +
114- " \" nickname \" : \" \( contact? . name? . nickname as? String ?? " " ) \" , " +
115- " \" street \" : \" \( contact? . postalAddress? . street as? String ?? " " ) \" , " +
116- " \" city \" : \" \( contact? . postalAddress? . city as? String ?? " " ) \" , " +
117- " \" state \" : \" \( contact? . postalAddress? . state as? String ?? " " ) \" , " +
118- " \" postalCode \" : \" \( contact? . postalAddress? . postalCode as? String ?? " " ) \" , " +
119- " \" country \" : \" \( contact? . postalAddress? . country as? String ?? " " ) \" , " +
120- " \" isoCountryCode \" : \" \( contact? . postalAddress? . isoCountryCode as? String ?? " " ) \" , " +
121- " \" subAdministrativeArea \" : \" \( contact? . postalAddress? . subAdministrativeArea as? String ?? " " ) \" , " +
122- " \" subLocality \" : \" \( contact? . postalAddress? . subLocality as? String ?? " " ) \" " +
123- " }] "
124- dataString = dataString. replacingOccurrences ( of: " \n " , with: " \\ n " )
125- let dataStringUTF8 = dataString. data ( using: . utf8) !
126- do {
127- if let jsonArray = try JSONSerialization . jsonObject ( with: dataStringUTF8, options: . allowFragments) as? JSArray {
128- return jsonArray
129- }
130- } catch let error as NSError {
131- print ( error)
132- return [ ]
112+ nameFormatted = ( contact. name? . nameSuffix as? String ?? " " )
133113 }
134- return [ ]
114+
115+ return [
116+ " givenName " : name? . givenName ?? " " ,
117+ " familyName " : name? . familyName ?? " " ,
118+ " middleName " : name? . middleName ?? " " ,
119+ " namePrefix " : name? . namePrefix ?? " " ,
120+ " nameSuffix " : name? . nameSuffix ?? " " ,
121+ " nameFormatted " : nameFormatted,
122+ " nickname " : name? . nickname ?? " " ,
123+ " phoneNumber " : contact. phoneNumber? . stringValue ?? " " ,
124+ " emailAddress " : contact. emailAddress ?? " " ,
125+ " street " : address? . street ?? " " ,
126+ " city " : address? . city ?? " " ,
127+ " state " : address? . state ?? " " ,
128+ " postalCode " : address? . postalCode ?? " " ,
129+ " country " : address? . country ?? " " ,
130+ " isoCountryCode " : address? . isoCountryCode ?? " " ,
131+ " subAdministrativeArea " : address? . subAdministrativeArea ?? " " ,
132+ " subLocality " : address? . subLocality ?? " "
133+ ]
135134 }
136135
137136 // For security reasons, Apple does not return the full address until a successful payment has been made.
0 commit comments