Advertisement
maneesf

Hyperpay ios

Sep 8th, 2022
1,951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Hyperpay.m
  3. //  melkior
  4. //
  5. //  Created by Hyperpay on 15/06/1441 AH.
  6. //  Copyright © 1441 Facebook. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "Hyperpay.h"
  11. #import <React/RCTLog.h>
  12. @implementation Hyperpay
  13.  
  14. OPPPaymentProvider *provider;
  15.  
  16.  
  17.  
  18.  
  19. RCT_EXPORT_MODULE(Hyperpay)
  20.  
  21. -(instancetype)init
  22. {
  23.     self = [super init];
  24.     if (self) {
  25.       #ifdef DEBUG
  26.         provider = [OPPPaymentProvider paymentProviderWithMode:OPPProviderModeTest];
  27.      #else
  28.         provider = [OPPPaymentProvider paymentProviderWithMode:OPPProviderModeTest];
  29.      #endif
  30.     }
  31.    
  32.     return self;
  33. }
  34.  
  35. - (NSArray<NSString *> *)supportedEvents {
  36.     return @[@"onTransactionComplete"];
  37. }
  38.  
  39. /**
  40.  React Native functions
  41.  */
  42.  
  43. RCT_EXPORT_METHOD(transactionPayment: (NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  44.    
  45.     NSError * _Nullable error;
  46.    
  47.   for (NSString *key in options) {
  48.         NSLog(@"key: %@, value: %@ \n", key, [options valueForKey:key]);
  49.     }
  50.     OPPCardPaymentParams *params = [OPPCardPaymentParams cardPaymentParamsWithCheckoutID:[options valueForKey:@"checkoutID"]
  51.  
  52.                                                                         paymentBrand:[options valueForKey:@"paymentBrand"]
  53.                                                                               holder:[options valueForKey:@"holderName"]
  54.                                                                               number:[options valueForKey:@"cardNumber"]
  55.                                                                          expiryMonth:[options valueForKey:@"expiryMonth"]
  56.                                                                           expiryYear:[options valueForKey:@"expiryYear"]
  57.                                                                                  CVV:[options valueForKey:@"cvv"]
  58.                                                                                error:&error];
  59.  
  60.  
  61.  
  62.     if (error) {
  63.       NSLog(@"%s", "error");
  64.       RCTLog(@"iOS if block");
  65.       reject(@"transactionPayment",error.localizedDescription, error);
  66.     } else {
  67.        params.shopperResultURL = @"org.reactjs.native.example.melkior.payments://result";
  68.      
  69.       //params.tokenizationEnabled = YES;
  70.       OPPTransaction *transaction = [OPPTransaction transactionWithPaymentParams:params];
  71.  
  72.       [provider registerTransaction:transaction completionHandler:^(OPPTransaction * _Nonnull transaction, NSError * _Nullable error) {
  73.         NSDictionary *transactionResult;
  74.         if (transaction.type == OPPTransactionTypeAsynchronous) {
  75.           // Open transaction.redirectURL in Safari browser to complete the transaction
  76. //
  77. //          [self sendEventWithName:@"onSessionConnect" body:@{@"redirectURL": transaction.redirectURL.absoluteString, @"status":@"pending",
  78. //                                                             @"checkoutID":transaction.paymentParams.checkoutID
  79. //          }];
  80.           RCTLog(@"%s", "aysnc");
  81.           RCTLog(@"%@", transaction.redirectURL.absoluteString);
  82.          
  83.            transactionResult = @{
  84.           @"redirectURL":transaction.redirectURL.absoluteString,
  85.           @"status":@"pending",
  86.           @"checkoutId":transaction.paymentParams.checkoutID
  87.           };
  88.           resolve(transactionResult);
  89.         }  else if (transaction.type == OPPTransactionTypeSynchronous) {
  90.           RCTLog(@"%s", "sync");
  91.           transactionResult = @{
  92.             @"checkoutId": transaction.paymentParams.checkoutID
  93.           };
  94.          
  95.          resolve(transactionResult);
  96.         } else {
  97.           RCTLog(@"%s", "error payment");
  98.           RCTLog(error.localizedDescription);
  99.  
  100.           reject(@"transactionPayment",error.localizedDescription, error);
  101.           // Handle the error
  102.         }
  103.       }];
  104.     }
  105. }
  106.  
  107. RCT_EXPORT_METHOD(tokenizedTransaction: (NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  108.   NSError * _Nullable error;
  109.  
  110.  
  111.   OPPTokenPaymentParams *tokenParams = [OPPTokenPaymentParams tokenPaymentParamsWithCheckoutID:[options valueForKey:@"checkoutID"] tokenID:[options valueForKey:@"tokenID"] paymentBrand:[options valueForKey:@"paymentBrand"] error:&error];
  112.   if (error) {
  113.     RCTLog(@"%s", "error tokenized payment");
  114.     RCTLog(error.localizedDescription);
  115.       // See error.code (OPPErrorCode) and error.localizedDescription to identify the reason of failure.
  116.     reject(@"tokenizedTransaction",error.localizedDescription, error);
  117.   }else{
  118.     tokenParams.shopperResultURL = @"melkiorapp://product";
  119.     OPPTransaction *transaction = [OPPTransaction transactionWithPaymentParams:tokenParams];
  120.    
  121.    
  122.     [provider submitTransaction:transaction completionHandler:^(OPPTransaction * _Nonnull transaction, NSError * _Nullable error) {
  123.       NSDictionary *result;
  124.       if (transaction.type == OPPTransactionTypeAsynchronous) {
  125.         // Open transaction.redirectURL in Safari browser to complete the transaction
  126.         result = @{
  127.           @"checkoutId": transaction.paymentParams.checkoutID,
  128.           @"status": @"pending",
  129.           @"redirectUrl": transaction.redirectURL.absoluteString
  130.         };
  131.         RCTLog(@"Sucess async");
  132.         resolve(result);
  133.       }  else if (transaction.type == OPPTransactionTypeSynchronous) {
  134.         RCTLog(@"Sucess sync");
  135.        resolve(transaction);
  136.       } else {
  137.         RCTLog(error.localizedDescription);
  138.         reject(@"tokenizedTransaction",error.localizedDescription, error);
  139.         // Handle the error
  140.       }
  141.     }];
  142.    
  143.    
  144.   }
  145.  
  146.  
  147.  
  148.  
  149. }
  150.  
  151.  
  152. RCT_EXPORT_METHOD(validateCardInfo:(NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  153.   if(![OPPCardPaymentParams isHolderValid:[options valueForKey:@"cardNumber"]]){
  154.     reject(@"validateCardInfo", @"invalid card number",nil);
  155.   }else if(![OPPCardPaymentParams isCvvValid:[options valueForKey:@"cvv"]]){
  156.     reject(@"validateCardInfo", @"invalid CVV", nil);
  157.   }else if(![OPPCardPaymentParams isExpiryYearValid:[options valueForKey:@"expiryYear"]]){
  158.              reject(@"validateCardInfo", @"invalid expiry Year", nil);
  159.   }else if(![OPPCardPaymentParams isExpiryMonthValid:[options valueForKey:@"expiryMonth"]]){
  160.     reject(@"validateCardInfo", @"invalid expiry month", nil);
  161.   }else if(![OPPCardPaymentParams isExpiredWithExpiryMonth:[options valueForKey:@"expiryMonth"] andYear:[options valueForKey:@"expiryYear"]]){
  162.     reject(@"validateCardInfo", @"Card is expiried", nil);
  163.   }else{
  164.     resolve([NSNull null]);
  165.   }
  166. }
  167.  
  168. @end
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement