#import <AMSFoundation/AMSFoundation-Swift.h>
#import <AMSElement/AMSElement-Swift.h>
@interface PaymentViewController () <AMSPaymentProtocol>
@end
@implementation PaymentViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1. Create configuration
AMSPaymentElementConfiguration *config = [AMSPaymentElementConfiguration new];
config.locale = @"en_US";
config.options = @{
@"sandbox": @YES,
@"showLoading": @YES
};
// 2. Initialize SDK (with callback)
[[AMSPaymentElement shared] initConfiguration:config completion:^(AMSStatusResult * _Nonnull result) {
if (result.status == AMSStatusResultTypeSUCCESS) {
NSLog(@"Initialization successful");
} else {
NSLog(@"Initialization failed: %@", result.error.message);
}
}];
// 3. Set delegate
[AMSPaymentElement shared].paymentDelegate = self;
// 4. (Optional) Preload resources
[[AMSPaymentElement shared] preload];
}
- (void)startPaymentWithSessionData:(NSString *)sessionData {
// 5. Create payment component (with callback)
[[AMSPaymentElement shared] createComponent:sessionData completion:^(AMSStatusResult * _Nonnull result) {
if (result.status == AMSStatusResultTypeSUCCESS) {
NSLog(@"Component created successfully");
}
}];
}
#pragma mark - AMSPaymentProtocol
- (void)onSubmitPayCallback:(AMSStatusResult * _Nullable)eventResult {
switch (eventResult.status) {
case AMSStatusResultTypeSUCCESS:
NSLog(@"Payment successful");
break;
case AMSStatusResultTypeFAIL:
NSLog(@"Payment failed: %@", eventResult.error.message);
break;
case AMSStatusResultTypePROCESSING:
NSLog(@"Payment processing");
break;
case AMSStatusResultTypeCANCELLED:
NSLog(@"Payment cancelled");
break;
default:
break;
}
}
- (void)onEventCallback:(NSString * _Nonnull)eventCode eventResult:(AMSEventResult * _Nonnull)eventResult {
NSLog(@"Event callback: %@", eventCode);
}
- (void)dealloc {
[[AMSPaymentElement shared] onDestroy];
}
@end