GithubHelp home page GithubHelp logo

tempbottle / applepaystubs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stripe-archive/applepaystubs

0.0 2.0 0.0 434 KB

Test your Apple Pay integration without Apple Pay

License: MIT License

Ruby 2.71% Objective-C 97.29%

applepaystubs's Introduction

ApplePayStubs

First, some big news!

This library exists because as of iOS8 it was impossible to display a PKPaymentAuthorizationViewController (a.k.a. an Apple Pay payment sheet) in the iOS simulator. Fortunately, this is no longer necessary - as of iOS 9, Apple Pay works in the simulator, and returns dummy test cards. In other words, ApplePayStubs has been sherlocked! As such, consider this library deprecated as of iOS 9. If you're interested in using it until the iOS 9 / Xcode 7 release, though, read on!

What is this?

ApplePay is awesome. However, since it isn't available in every country yet, and only then on the newest iOS devices, we want to make it easier for developers to plan and test their Apple Pay integrations.

We've created a replacement component for PKPaymentAuthorizationViewController (the primary class involved in ApplePay transactions) for businesses interested in working with ApplePay called STPTestPaymentAuthorizationViewController. These classes appear visually similar and behave almost identically. The primary difference is that STPTestPaymentAuthorizationViewController yields test credit cards and addresses instead of accessing actual information stored on a user's iPhone. You can use it to build and test all of your UI and application logic around ApplePay, and switch it out for the real thing once you have access to a proper testing device.

Please note that this is for testing and development purposes only.

Dependencies

  • Xcode 6+
  • iOS 8+

ApplePayStubs also depends on the PassKit framework.

Installation

Use Cocoapods or manually add the files to your repository.

Usage

You create and use instances of STPTestPaymentAuthorizationViewController exactly the same way as with PKPaymentAuthorizationViewController.

// ViewController.m
- (void)checkoutButtonTapped {
    PKPaymentRequest *request = ...;
    UIViewController *controller;
#if DEBUG
    controller = [[STPTestPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
    controller.delegate = self;
#else
    controller = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
    controller.delegate = self;

    [self presentViewController:controller];
}

STPTestPaymentAuthorizationViewController will trigger the same PKPaymentAuthorizationViewControllerDelegate callbacks at the appropriate time on its delegate.

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                  didSelectShippingAddress:(ABRecordRef)address
                                completion:(void (^)(PKPaymentAuthorizationStatus status, NSArray *shippingMethods, NSArray *summaryItems))completion {
    [self fetchShippingCostsForAddress:address completion:^(NSArray *shippingMethods, NSError *error) {
        if (error) {
            completion(PKPaymentAuthorizationStatusFailure, nil, nil);
            return;
        }
        completion(PKPaymentAuthorizationStatusSuccess, shippingMethods, [self summaryItemsForShippingMethod:shippingMethods.firstObject]);
    }];
}

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                   didSelectShippingMethod:(PKShippingMethod *)shippingMethod
                                completion:(void (^)(PKPaymentAuthorizationStatus, NSArray *summaryItems))completion {
    completion(PKPaymentAuthorizationStatusSuccess, [self summaryItemsForShippingMethod:shippingMethod]);
}

- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
    [self dismissViewControllerAnimated:YES completion:nil];
}

When the user finishes selecting a card, as usual STPTestPaymentAuthorizationViewController will call paymentAuthorizationViewController:didAuthorizePayment:completion on its delegate. This delegate method includes a PKPayment object, which itself has an instance of PKPaymentToken that contains encrypted credit card data that you'd pass off to your payment processor (such as Stripe). While the PKPayment and PKPaymentToken returned by ApplePayStubs have stubbed (and invalid) versions of this data, the Stripe API will be able to recognize them in testmode. As such, you shouldn't have to modify your existing PKPaymentAuthorizationViewControllerDelegate methods:

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                       didAuthorizePayment:(PKPayment *)payment
                                completion:(void (^)(PKPaymentAuthorizationStatus))completion {
                                
    [[STPAPIClient sharedClient] createTokenWithPayment:payment
        completion:^(STPToken *token, NSError *error) {
            [self createBackendChargeWithToken:token
                                    completion:^(STPBackendChargeResult status, NSError *error) {
                if (status == STPBackendChargeResultSuccess) {
                    completion(PKPaymentAuthorizationStatusSuccess);
                } else {
                    completion(PKPaymentAuthorizationStatusFailure);
                }
        }];
    }];
}

(Note: Stripe tokens created from Apple Pay work interchangeably with those created using manually-collected credit card details).

If you're not using Stripe, you can find the selected card information on the PKPayment's PKPaymentToken in the transactionIdentifier field, in the format "ApplePayStubs~{card_number}~{amount_in_cents}~{currency}~{uuid}".

Example App / Learn More

If you'd like to see more examples of how to use this, we use ApplePayStubs in the example app for our main iOS library.

If you'd like to learn more about accepting payments on iOS with Stripe in general, read our iOS tutorial.

applepaystubs's People

Contributors

jack-stripe avatar kishinmanglani avatar natecook1000 avatar sudeepy avatar coolbeet avatar

Watchers

tempbottle avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.