Checkout V1
Table of contents
Checkout version 1 requires you to manage the loading of a checkout URL yourself and provide it to the SDK. This version of checkout only supports standard
mode and completes on receiving a redirect.
The Web Login is a UIViewController
that can be presented modally over the view controller of your choosing.
When creating a checkout token, both
redirectConfirmUrl
andredirectCancelUrl
must be set. Failing to do so will cause undefined behavior. The SDK’s example merchant server sets the parameters here. See more by checking the api reference.
Swift (UIKit)
import Afterpay
import UIKit
final class CheckoutViewController: UIViewController {
// ...
@objc func didTapPayWithAfterpay() {
/**
* `presentCheckoutModally` can take a `shouldLoadRedirectUrls` which is
* a boolean for whether the redirect urls set when generating
* the checkout url should load. Default and recommended value is false
*/
Afterpay.presentCheckoutModally(over: self, loading: self.checkoutUrl) { result in
switch result {
case .success(let token):
// Handle successful Afterpay checkout
case .cancelled(let reason):
// Handle checkout cancellation
}
}
}
}
Objective-C (UIKit)
#import "ViewController.h"
#import <Afterpay/Afterpay-Swift.h>
#import <UIKit/UIKit.h>
@implementation ViewController
// ...
- (void)didTapPayWithAfterpay {
void (^completion)(APCheckoutResult *) = ^(APCheckoutResult *result) {
if ([result isKindOfClass:[APCheckoutResultSuccess class]]) {
// Handle success with [(APCheckoutResultSuccess *)result token]
} else {
// Handle cancellation with [(APCheckoutResultCancelled *)result reason]
}
};
[APAfterpay presentCheckoutModallyOverViewController:self
loadingCheckoutURL:self.checkoutUrl
animated:true
completion:completion];
}
@end
SwiftUI
struct MyView: View {
// Updating this state with a retrieved checkout URL will present the afterpay sheet
@State private var checkoutURL: URL?
var body: some View {
SomeView()
.afterpayCheckout(url: $checkoutURL) { result in
switch result {
case .success(let token):
// Handle successful Afterpay checkout
case .cancelled(let reason):
// Handle checkout cancellation
}
}
}
}