2

I am trying to get rid of the all deprecated objects when integrating the Stripe API with my Swift 2.0 project. I have replaced the code that was obvious but the 3 lines I need help with are:

ABMultiValueRef addressValues = ABRecordCopyValue(payment.billingAddress, kABPersonAddressProperty);

And

if (ABMultiValueGetCount(addressValues) > 0) {
            CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressValues, 0);
}

The Warnings are(below):

'ABMultiValueRef' is deprecated: first deprecated in iOS 9.0 - use NSArray of CNLabeledValue

'ABRecordCopyValue' is deprecated: first deprecated in iOS 9.0 - use CN object's properties

'ABMultiValueGetCount' is deprecated: first deprecated in iOS 9.0 - use NSArray.count with the labeled value property

'ABMultiValueCopyValueAtIndex' is deprecated: first deprecated in iOS 9.0 - use [[NSArray objectAtIndex:] value] with the labeled value property

The full code is the following:

#import <AddressBook/AddressBook.h>

#import "STPAPIClient+ApplePay.h"
#import "PKPayment+Stripe.h"
#import "STPAPIClient+Private.h"

@implementation STPAPIClient (ApplePay)

- (void)createTokenWithPayment:(PKPayment *)payment completion:(STPTokenCompletionBlock)completion {
    [self createTokenWithData:[self.class formEncodedDataForPayment:payment] completion:completion];
}

+ (NSData *)formEncodedDataForPayment:(PKPayment *)payment {
    NSCAssert(payment != nil, @"Cannot create a token with a nil payment.");
    NSMutableCharacterSet *set = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
    [set removeCharactersInString:@"+="];
    NSString *paymentString =
        [[[NSString alloc] initWithData:payment.token.paymentData encoding:NSUTF8StringEncoding] stringByAddingPercentEncodingWithAllowedCharacters:set];
    __block NSString *payloadString = [@"pk_token=" stringByAppendingString:paymentString];

    if (payment.billingContact) {
        NSMutableDictionary *params = [NSMutableDictionary dictionary];

        NSString *firstName = CNContactGivenNameKey;
        NSString *lastName = CNContactFamilyNameKey;
        if (firstName.length && lastName.length) {
            params[@"name"] = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
        }
        ABMultiValueRef addressValues = ABRecordCopyValue(payment.billingAddress, kABPersonAddressProperty);
        if (addressValues != NULL) {
            if (ABMultiValueGetCount(addressValues) > 0) {
                CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressValues, 0);
                NSString *line1 = CFDictionaryGetValue(dict, CNPostalAddressStreetKey);
                if (line1) {
                    params[@"address_line1"] = line1;
                }
                NSString *city = CFDictionaryGetValue(dict, CNPostalAddressCityKey);
                if (city) {
                    params[@"address_city"] = city;
                }
                NSString *state = CFDictionaryGetValue(dict, CNPostalAddressStateKey);
                if (state) {
                    params[@"address_state"] = state;
                }
                NSString *zip = CFDictionaryGetValue(dict, CNPostalAddressPostalCodeKey);
                if (zip) {
                    params[@"address_zip"] = zip;
                }
                NSString *country = CFDictionaryGetValue(dict, CNPostalAddressCountryKey);
                if (country) {
                    params[@"address_country"] = country;
                }
                CFRelease(dict);
                [params enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, __unused BOOL *stop) {
                    NSString *param = [NSString stringWithFormat:@"&card[%@]=%@", key, [obj stringByAddingPercentEncodingWithAllowedCharacters:set]];
                    payloadString = [payloadString stringByAppendingString:param];
                }];
            }
            CFRelease(addressValues);
        }
    }

    if (payment.token.paymentMethod) {
        NSString *param = [NSString stringWithFormat:@"&pk_token_instrument_name=%@", payment.token.paymentMethod];
        payloadString = [payloadString stringByAppendingString:param];
    }

    if (payment.token.paymentMethod) {
        NSString *param = [NSString stringWithFormat:@"&pk_token_payment_network=%@", payment.token.paymentMethod];
        payloadString = [payloadString stringByAppendingString:param];
    }

    if (payment.token.transactionIdentifier) {
        NSString *transactionIdentifier = payment.token.transactionIdentifier;
        if ([payment stp_isSimulated]) {
            transactionIdentifier = [PKPayment stp_testTransactionIdentifier];
        }
        NSString *param = [NSString stringWithFormat:@"&pk_token_transaction_id=%@", transactionIdentifier];
        payloadString = [payloadString stringByAppendingString:param];
    }

    return [payloadString dataUsingEncoding:NSUTF8StringEncoding];
}

@end

void linkSTPAPIClientApplePayCategory(void){}
JMax
  • 53
  • 6

1 Answers1

0

Try this

CNPostalAddress *addressMultiValue = payment.billingContact.postalAddress;

instead of

ABMultiValueRef addressValues = ABRecordCopyValue(payment.billingAddress, kABPersonAddressProperty);
Rohit Wankhede
  • 506
  • 5
  • 15