wizyashas
wizyashas

Reputation: 95

Apple pay integration with Stripe (STPTestPaymentAuthorizationViewController in debug)

I am trying to integrate apple pay with stripe in my iOS app. Using ApplePayStub provide by stripe to check apple pay in DEBUG Mode

I am using the latest stripe and ApplePayStub code from git

Trying to run on iPhone 6 simulator and the code that i am using is:

paymentController = [[STPTestPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
((STPTestPaymentAuthorizationViewController*) paymentController).delegate = self;

Getting error:

Error Domain=com.stripe.lib Code=50 "Your payment information is formatted improperly. Please make sure you're correctly using the latest version of our iOS library. For more information see https://stripe.com/docs/mobile/ios ." UserInfo=0xxxxxxxxxxx {com.stripe.lib:ErrorMessageKey=Your payment information is formatted improperly. Please make sure you're correctly using the latest version of our iOS library. For more information see https://stripe.com/docs/mobile/ios ., NSLocalizedDescription=Your payment information is formatted improperly. Please make sure you're correctly using the latest version of our iOS library. For more information see https://stripe.com/docs/mobile/ios .},

Any help is greatly appreciated.

Upvotes: 1

Views: 1703

Answers (2)

Mohsin Khubaib Ahmed
Mohsin Khubaib Ahmed

Reputation: 1018

@Wizyashas, Your setup of the STPTestPaymentAuthorizationViewController is perfectly fine. The problem happens when you try to generate the token via STPAPIClient. There's where it all gets messy.

This little bit of RnD helped me. Digging into the CustomSampleProject provided by Stripe themselves, ApplePayStubs works pretty well when the STPCard is recognized when the delegate

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                   didAuthorizePayment:(PKPayment *)payment
                            completion:(void (^)(PKPaymentAuthorizationStatus))completion

of PKPaymentAuthorizationViewControllerDelegate is called. The sample code here checked if the code was run in debug that is for ApplePayStubs, the (PKPayment *)payment in the delegate is converted to a STPCard and is launched to the STPAPIClient for STPToken generation. Following is the body of the above mentioned delegate:

#if DEBUG // This is to handle a test result from ApplePayStubs
if (payment.stp_testCardNumber)
{
    STPCard *card = [STPCard new];
    card.number = payment.stp_testCardNumber;
    card.expMonth = 12;
    card.expYear = 2020;
    card.cvc = @"123";
    [[STPAPIClient sharedClient] createTokenWithCard:card
                                          completion:^(STPToken *token, NSError *error)
    {
        if (error)
        {
            completion(PKPaymentAuthorizationStatusFailure);
            [[[UIAlertView alloc] initWithTitle:@"Error"
                                        message:@"Payment Unsuccessful! \n Please Try Again"
                                       delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
            return;
        }
    /*
     Handle Token here
     */
                                            }];
}
#else
[[STPAPIClient sharedClient] createTokenWithPayment:payment
                                         completion:^(STPToken *token, NSError *error)
{
    if (error)
    {
        completion(PKPaymentAuthorizationStatusFailure);
        [[[UIAlertView alloc] initWithTitle:@"Error"
                                    message:@"Payment Unsuccessful!"
                                   delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
        return;
    }
    /*
     Handle Token here
     */
}];
#endif

This worked for me. With ApplePayStubs (on Simulator) and without them (on Device).

Hope this Helps :)

Upvotes: 1

iAnurag
iAnurag

Reputation: 9356

Try making request using PaymentKit

@IBAction func payTapped(AnyObject) {
    // Valiate that this device can take payments
    if (PKPaymentAuthorizationViewController.canMakePayments()) {
        // Construct the basic payment request
        var paymentRequest = PKPaymentRequest()
        paymentRequest.merchantIdentifier = "merchant.com.example";
        paymentRequest.supportedNetworks = [PKPaymentNetworkVisa, PKPaymentNetworkMasterCard, PKPaymentNetworkAmex]
        paymentRequest.merchantCapabilities = PKMerchantCapability.Capability3DS | PKMerchantCapability.CapabilityEMV
        paymentRequest.countryCode = "US"
        paymentRequest.currencyCode = "USD"
        paymentRequest.requiredShippingAddressFields = PKAddressField.All;

        // Add a line item
        var totalItem = PKPaymentSummaryItem(label:"Foo", amount:NSDecimalNumber(string:"0.05"))
        paymentRequest.paymentSummaryItems = [totalItem];

        // Show the Apple Pay controller
        var payAuth = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
        payAuth.delegate = self
        self.presentViewController(payAuth, animated:true, completion: nil)
    }
}

Upvotes: 0

Related Questions