Giter VIP home page Giter VIP logo

affirm-ios-sdk's Introduction

Affirm iOS SDK

CocoaPods Carthage compatible Travis license

The Affirm iOS SDK allows you accept Affirm payments in your own app.

Installation

CocoaPods and Carthage are the recommended methods for installing the Affirm SDK.

CocoaPods

Add the following to your Podfile and run pod install

pod 'AffirmSDK'

Carthage

Add the following to your Cartfile and follow the setup instructions here.

github "Affirm/affirm-ios-sdk"

Manual

Alternatively, if you do not want to use CocoaPods or Carthage, you may clone our GitHub repository and simply drag and drop the AffirmSDK folder into your XCode project.

Usage Overview

An Affirm integration consists of two components: checkout and promotional messaging.

Before you can use these components, first you must set the AffirmSDK with your public API key from your Merchant Dashboard. You must set this key to the shared AffirmConfiguration once (preferably in your AppDelegate) as follows

AffirmConfiguration *config = [AffirmConfiguration configurationWithPublicAPIKey:@"PUBLIC_API_KEY" environment:AffirmEnvironmentSandbox];
[AffirmConfiguration setSharedConfiguration:config];

Checkout

Checkout creation

Checkout creation is the process in which a customer uses Affirm to pay for a purchase in your store. This process is governed by the AffirmCheckoutViewController object, which requires three parameters:

  • An AffirmCheckout object which contains details about the purchase itself
  • An AffirmCheckoutType which determines whether the checkout flow should use the SDK's built-in loading indicator and error modal to handle loading and error states in the checkout process or if the developer will handle these states manually.
  • An AffirmCheckoutDelegate object which receives messages at various stages in the checkout process

Once the AffirmCheckoutViewController has been constructed from the parameters above, you may present it as with any other view controller. This initiates the flow which guides the user through the Affirm checkout process. An example of how this is implemented is provided as follows

AffirmItem *item = [AffirmItem itemWithName:@"Affirm Test Item" SKU:@"test_item" unitPrice:price quantity:1 URL:[NSURL URLWithString:@"http://sandbox.affirm.com/item"]];
AffirmShippingDetail *shipping = [AffirmShippingDetail shippingDetailWithName:@"Chester Cheetah" addressWithLine1:@"633 Folsom Street" line2:@"" city:@"San Francisco" state:@"CA" zipCode:@"94107" countryCode:@"USA"];
AffirmCheckout *checkout = [AffirmCheckout checkoutWithItems:@[item] shipping:shipping taxAmount:[NSDecimalNumber zero] shippingAmount:[NSDecimalNumber zero]];
AffirmCheckoutViewController *checkoutVC = [AffirmCheckoutViewController startCheckout:checkout checkoutType:AffirmCheckoutTypeAutomatic delegate:self];
[self presentViewController:checkoutVC animated:true completion:nil];

The flow ends once the user has successfully confirmed the checkout, canceled the checkout, or encountered an error in the process. In each of these cases, Affirm will send a message to the AffirmCheckoutDelegate along with additional information about the result.

Charge authorization

Once the checkout has been successfully confirmed by the user, the AffirmCheckoutDelegate object will receive a checkout token. This token should be forwarded to your server, which should then use the token to authorize a charge on the user's account. For more details about the server integration, see our API documentation.

Promotional Messaging

Affirm Promotional Messaging allows you to display the monthly payment price to inform customers about the availability of installment financing.

To display Affirm As Low As promotional messaging, the SDK provides a custom AffirmAsLowAsButton that encapsulates the AffirmAsLowAs functionality in a button that handles all states and only requires the developer to add to their view and configure to implement. The AffirmALAButton is implemented as follows

AffirmAsLowAsButton *alaButton = [AffirmAsLowAsButton createButtonWithPromoID:@"promo_id" presentingViewController:self frame:frame];
[self.view addSubview:alaButton];
[alaButton configureWithAmount:amount affirmLogoType:AffirmLogoTypeName affirmColor:AffirmColorTypeBlue maxFontSize:18 callback:^(BOOL alaEnabled, NSError *error) {
    //alaEnabled specifies whether ALA text or a default message is being displayed
}];

Tapping on the ALA button automatically opens a product/site modal depending on whether ALA is enabled on the button

To display the AffirmPromoModal outside of tapping on the AffirmALAButton you may initialize and display an instance of the promo modal VC as follows

AffirmPromoModalViewController *promoVC = [AffirmPromoModalViewController promoModalControllerWithModalId:@"promo_id" amount:amount];
[self presentViewController:promoVC animated:YES completion:nil];

Example

A demo app that integrates Affirm is included in the repo. To run it, run pod install and then open AffirmSDKDemo.xcworkspace in Xcode.

affirm-ios-sdk's People

Contributors

curtiscook avatar ebabchick avatar jeffaburt avatar jianbai avatar md143rbh7f avatar sachinkesiraju avatar

Watchers

 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.