Configurer une carte virtuelle sur iOS

Apprenez à configurer la carte virtuelle Affirm sur votre application mobile iOS.

Conditions préalables

Étapes

1. Configurer Affirm sur iOS

Avec CocoaPods

  1. Install the latest version of CocoaPods.
  2. Si vous n'avez pas de Podfile existant, exécutez cette commande pour en créer un :
pod init
  1. Add this line to your Podfile:
pod 'AffirmSDK'
  1. Exécutez cette commande :
pod install
  1. Use the .xcworkspace file to open your project in Xcode, instead of the .xcodeproj file, from here on out.
  2. À l'avenir, pour mettre à jour vers la dernière version du SDK, vous pouvez exécuter ceci :
pod update AffirmSDK

Avec Carthage

  1. Installez la dernière version de Carthage.
  2. Add this line to your Cartfile:
github "Affirm/affirm-merchant-sdk-ios"
  1. Follow the Carthage installation instructions.
  2. À l'avenir, pour passer à la dernière version du SDK, exécutez cette commande :
carthage update affirm-merchant-sdk-ios --platform ios

🚧

Versions du SDK

For details on the latest SDK release and past versions, see the Releases page on GitHub. Watch releases for the repository to receive notifications when a new release is published.


2. Initialiser Affirm

Configure the SDK with your Affirm public API key so that it can make requests to the Affirm server.

ffirmConfiguration.shared.configure(publicKey: "YOUR PUBLIC KEY", environment: .sandbox, merchantName: "Affirm Example Swift")
[[AffirmConfiguration sharedInstance] configureWithPublicKey:@"YOUR PUBLIC KEY" environment:AffirmEnvironmentSandbox merchantName:@"Affirm Example"];

3. Rendu du paiement Affirm

La création d'une caisse est le moment où un client utilise Affirm pour payer un achat intégré à une application. Vous pouvez créer un objet de paiement et lancer le paiement Affirm en utilisant la fonction Checkout :

let dollarPrice = NSDecimalNumber(string: self.amountTextField.text)
        let item = AffirmItem(name: "Affirm Test Item", sku: "test_item", unitPrice: dollarPrice, quantity: 1, url: URL(string: "http://sandbox.affirm.com/item")!)
        let shipping = AffirmShippingDetail.shippingDetail(name: "Chester Cheetah", line1: "633 Folsom Street", line2: "", city: "San Francisco", state: "CA", zipCode: "94107", countryCode: "USA")
        let checkout = AffirmCheckout(items: [item], shipping: shipping, taxAmount: NSDecimalNumber.zero, shippingAmount: NSDecimalNumber.zero, discounts: nil, metadata: nil, financingProgram: nil, orderId: "JKLMO4321")

        let controller = AffirmCheckoutViewController.start(checkout: checkout, useVCN: true, delegate: self)
        present(controller, animated: true, completion: nil)
// initialize an AffirmItem with item details
AffirmItem *item = [AffirmItem itemWithName:@"Affirm Test Item" SKU:@"test_item" unitPrice:price quantity:1 URL:[NSURL URLWithString:@"http://sandbox.affirm.com/item"]];

// initialize an AffirmShippingDetail with the user's shipping address
AffirmShippingDetail *shipping = [AffirmShippingDetail shippingDetailWithName:@"Chester Cheetah" addressWithLine1:@"633 Folsom Street" line2:@"" city:@"San Francisco" state:@"CA" zipCode:@"94107" countryCode:@"USA"];

// initialize an AffirmCheckout object with the item(s), shipping details, tax amount, shipping amount, discounts, financing program, and order ID
AffirmCheckout *checkout = [[AffirmCheckout alloc] initWithItems:@[item] shipping:shipping taxAmount:[NSDecimalNumber zero] shippingAmount:[NSDecimalNumber zero] discounts:nil metadata:nil financingProgram:nil orderId:@"JKLMO4321"];

// The minimum requirements are to initialize the AffirmCheckout object with the item(s), shipping details, and payout Amount
AffirmCheckout *checkout = [AffirmCheckout checkoutWithItems:@[item] shipping:shipping payoutAmount:price];

// initialize an UINavigationController with the checkout object and present it
UINavigationController *nav = [AffirmCheckoutViewController startCheckoutWithNavigation:checkout useVCN:YES getReasonCodes:NO delegate:self];
[self presentViewController:nav animated:YES completion:nil];

// It is recommended that you round the total in the checkout request to two decimal places. Affirm SDK converts the float total to integer cents before initiating the checkout, so may round up or down depending on the decimal places. Ensure that the rounding in your app uses the same calculation across your other backend systems, otherwise, it may cause an error of 1 cent or more in the total validation on your end.

4. Gérer les rappels

L'objet AffirmCheckoutDelegate reçoit des messages à différentes étapes du processus de paiement. Il renvoie l'objet creditCard contenant les détails de la carte Affirm. Vous pouvez également choisir de ne récupérer les détails de la carte que depuis votre serveur si vous avez des préoccupations en matière de conformité PCI.

func vcnCheckout(_ checkoutViewController: AffirmCheckoutViewController, completedWith creditCard: AffirmCreditCard) {
        resultLabel.text = "Received credit card:\ncredit card id: \(creditCard.creditCardId)\ncheckout token: \(creditCard.checkoutToken)\ncard holder name: \(creditCard.cardholderName)\nnumber:\(creditCard.number)\ncvv: \(creditCard.cvv)\nexpiration: \(creditCard.expiration)\ncallback id: \(creditCard.callbackId)"
        checkoutViewController.dismiss(animated: true, completion: nil)
    }
- (void)vcnCheckout:(AffirmCheckoutViewController *)checkoutViewController completedWithCreditCard:(AffirmCreditCard *)creditCard

Pour plus de rappels, consultez la référence du code.

👍

Une fois que le paiement a été confirmé par l'utilisateur, l'objet AffirmCheckoutDelegate reçoit un objet creditCard. Les détails de la carte doivent être transmis à votre serveur afin d'être utilisés avec vos systèmes de paiement par carte existants.


Quelle est la prochaine étape?