Skip to content

Instantly share code, notes, and snippets.

@dayitv89
Last active September 6, 2022 16:49
Show Gist options
  • Save dayitv89/ce6855720b86aa800900c22f05989979 to your computer and use it in GitHub Desktop.
Save dayitv89/ce6855720b86aa800900c22f05989979 to your computer and use it in GitHub Desktop.
//
// AurusViewController.m
// AurusPayment
//
// Created by Gaurav D. Sharma on 06/09/22.
//
//
// replace <subdomain_provided_by_aurus>,<MerchantIdentifier>,<StoreId>,<TerminalId>,<AlternatePaymentMatrix>,<CorpID> with given credential
// use card details: 4111 1111 1111 1111, any future expire, any security code, any cardholder name
// debug WKWebKit => Safari -> Develop -> Simulator -> Open page
// docs link: https://docs.aurusinc.com/client_releases/mobilewebkitintegration
#import "AurusViewController.h"
#import <WebKit/WebKit.h>
@interface AurusViewController ()<WKNavigationDelegate,WKScriptMessageHandler> {
}
@property (weak) IBOutlet WKWebView *webView;
@end
@implementation AurusViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.webView.configuration.userContentController addScriptMessageHandler:self name:@"observe"];
[self.webView.configuration.userContentController addScriptMessageHandler:self name:@"error"];
[self loadData];
}
- (void)loadData{
NSURLSessionConfiguration *defaultSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultSessionConfiguration];
// Setup the request with URL
NSURL *url = [NSURL URLWithString:@"https://<subdomain_provided_by_aurus>.auruspay.com/aurus-adsdk-web-service/auruspay/adsdk/sessionId"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
// Convert POST string parameters to data using UTF8 Encoding
NSString *MerchantIdentifier = @"<MerchantIdentifier>";
NSString *StoreId = @"<StoreId>";
NSString *TerminalId= @"<TerminalId>";
NSString *AlternatePaymentMatrix = @"<AlternatePaymentMatrix>";
NSString *CorpID= @"<CorpID>";
NSString *postParams = [NSString stringWithFormat:@"{\"SessionRequest\":{\"MerchantIdentifier\":\"%@\",\"StoreId\":\"%@\",\"TerminalId\":\"%@\",\"ADSDKSpecVer\":\"6.13.8\",\"DomainId\":\"1\",\"TemplateId\":\"1\",\"URLType\":\"1\",\"CardType\":\"\",\"TokenType\":\"102\",\"AlternatePaymentMatrix\":\"%@\",\"CardTypeSupport\":\"\",\"CorpID\":\"%@\",\"LanguageIndicator\":\"00\"}}", MerchantIdentifier, StoreId, TerminalId, AlternatePaymentMatrix, CorpID];
NSData *postData = [postParams dataUsingEncoding:NSUTF8StringEncoding];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
// Convert POST string parameters to data using UTF8 Encoding
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:postData];
// Create dataTask
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//JSON Parsing....
NSString *url = results[@"SessionResponse"][@"IFrameUrl"];
[self loadViewFromURL:url];
}];
// Fire the request
[dataTask resume];
}
- (void)loadViewFromURL:(NSString*)url{
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
[self.webView loadRequest:nsrequest];
}
- (void)userContentController:(nonnull WKUserContentController*)userContentController
didReceiveScriptMessage:(nonnull WKScriptMessage*)message {
@try {
NSLog(@"message name:%@", message.name);
NSDictionary *infoDictionary = (NSDictionary*)message.body;
NSLog(@"Received Token One_Time_Token: %@",infoDictionary);
NSString *oneTimeToken = [infoDictionary objectForKey:@"one_time_token"];
NSLog(@"One_Time_Token:%@",oneTimeToken);
} @catch (NSException *exception) {
NSLog(@"NSException %@", exception);
}
}
- (IBAction)btnTapped:(id)sender {
NSLog(@"btn tapped");
NSString *script = @"window.isMobileIframe=true;window.saveAsDefault='';window.postMessage('aurus-token', '*')";
[self.webView evaluateJavaScript:script
completionHandler:^(id _Nullable data, NSError * _Nullable error) {
if (error != nil) {
NSLog(@"error %@", error);
} else if (data != nil) {
NSLog(@"data coming %@",data);
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment