@jimrising/easymerchantsdk-react-native 1.3.2 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -55,6 +55,11 @@ class OTPVerificationVC: BaseVC {
55
55
  private var timeRemaining = 180 // 3 minutes
56
56
  private var timer: Timer?
57
57
 
58
+ //GrailPay Params
59
+ var grailPayAccountID: String?
60
+ var selectedGrailPayAccountType: String?
61
+ var selectedGrailPayAccountName: String?
62
+
58
63
  override func viewDidLoad() {
59
64
  super.viewDidLoad()
60
65
 
@@ -215,18 +220,6 @@ class OTPVerificationVC: BaseVC {
215
220
  func otpVerificationApi() {
216
221
  showLoadingIndicator()
217
222
 
218
- // var components = URLComponents()
219
- // components.scheme = "https"
220
- // components.host = "stage-api.stage-easymerchant.io"
221
- // components.path = "/api/v1/customer/verify_otp"
222
- //
223
- // guard let serviceURL = components.url else {
224
- // print("Invalid URL")
225
- // hideLoadingIndicator()
226
- // return
227
- // }
228
-
229
- // Construct the full URL using baseURL from EnvironmentConfig and path from the endpoint
230
223
  let fullURL = EnvironmentConfig.baseURL + EnvironmentConfig.Endpoints.verifyOtp.path()
231
224
 
232
225
  guard let serviceURL = URL(string: fullURL) else {
@@ -243,6 +236,13 @@ class OTPVerificationVC: BaseVC {
243
236
  print("Setting clientToken header: \(token ?? "None")")
244
237
  request.addValue(token ?? "", forHTTPHeaderField: "Client-Token")
245
238
 
239
+ // Add API headers
240
+ if let apiKey = EnvironmentConfig.apiKey,
241
+ let apiSecret = EnvironmentConfig.apiSecret {
242
+ request.addValue(apiKey, forHTTPHeaderField: "x-api-key")
243
+ request.addValue(apiSecret, forHTTPHeaderField: "x-api-secret")
244
+ }
245
+
246
246
  let params: [String: Any] = [
247
247
  "card_search_value": email ?? "",
248
248
  "card_search_key": "email",
@@ -322,6 +322,17 @@ class OTPVerificationVC: BaseVC {
322
322
  }
323
323
 
324
324
  }
325
+ else if self.selectedPaymentMethod == "GrailPay" {
326
+ if let dataSection = responseObject["data"] as? [String: Any],
327
+ let innerData = dataSection["data"] as? [String: Any],
328
+ let customerId = innerData["customer_id"] as? String {
329
+ print("Extracted customer_id: \(customerId)")
330
+ self.grailPayAccountChargeApi(customerId: customerId)
331
+ } else {
332
+ print("customer_id not found. Sending empty customerId.")
333
+ self.grailPayAccountChargeApi(customerId: nil)
334
+ }
335
+ }
325
336
  } else {
326
337
  print("Invalid JSON format")
327
338
  }
@@ -341,18 +352,7 @@ class OTPVerificationVC: BaseVC {
341
352
  //MARK: - Send OTP Email Verification Api
342
353
  func emailVerificationApi() {
343
354
  showLoadingIndicator()
344
- // var components = URLComponents()
345
- // components.scheme = "https"
346
- // components.host = "stage-api.stage-easymerchant.io"
347
- // components.path = "/api/v1/customer/send_otp"
348
- //
349
- // guard let serviceURL = components.url else {
350
- // print("Invalid URL")
351
- // hideLoadingIndicator()
352
- // return
353
- // }
354
-
355
- // Construct the full URL using baseURL from EnvironmentConfig and path from the endpoint
355
+
356
356
  let fullURL = EnvironmentConfig.baseURL + EnvironmentConfig.Endpoints.emailVerification.path()
357
357
 
358
358
  guard let serviceURL = URL(string: fullURL) else {
@@ -369,6 +369,13 @@ class OTPVerificationVC: BaseVC {
369
369
  print("Setting clientToken header: \(token ?? "None")")
370
370
  request.addValue(token ?? "", forHTTPHeaderField: "Client-Token")
371
371
 
372
+ // Add API headers
373
+ if let apiKey = EnvironmentConfig.apiKey,
374
+ let apiSecret = EnvironmentConfig.apiSecret {
375
+ request.addValue(apiKey, forHTTPHeaderField: "x-api-key")
376
+ request.addValue(apiSecret, forHTTPHeaderField: "x-api-secret")
377
+ }
378
+
372
379
  let params: [String: Any] = [
373
380
  "card_search_value": email ?? "",
374
381
  "card_search_key": "email"
@@ -428,19 +435,7 @@ class OTPVerificationVC: BaseVC {
428
435
  //MARK: - Card Charge Api
429
436
  func paymentIntentApi(customerId: String?) {
430
437
  showLoadingIndicator()
431
-
432
- // var components = URLComponents()
433
- // components.scheme = "https"
434
- // components.host = "stage-api.stage-easymerchant.io"
435
- // components.path = "/api/v1/charges"
436
- //
437
- // guard let serviceURL = components.url else {
438
- // print("Invalid URL")
439
- // hideLoadingIndicator()
440
- // return
441
- // }
442
-
443
- // Construct the full URL using baseURL from EnvironmentConfig and path from the endpoint
438
+
444
439
  let fullURL = EnvironmentConfig.baseURL + EnvironmentConfig.Endpoints.charges.path()
445
440
 
446
441
  guard let serviceURL = URL(string: fullURL) else {
@@ -457,6 +452,13 @@ class OTPVerificationVC: BaseVC {
457
452
  print("Setting clientToken header: \(token ?? "None")")
458
453
  request.addValue(token ?? "", forHTTPHeaderField: "Client-Token")
459
454
 
455
+ // Add API headers
456
+ if let apiKey = EnvironmentConfig.apiKey,
457
+ let apiSecret = EnvironmentConfig.apiSecret {
458
+ request.addValue(apiKey, forHTTPHeaderField: "x-api-key")
459
+ request.addValue(apiSecret, forHTTPHeaderField: "x-api-secret")
460
+ }
461
+
460
462
  guard let billingInfoData = billingInfoData else {
461
463
  print("Billing info data is nil")
462
464
  return
@@ -566,7 +568,13 @@ class OTPVerificationVC: BaseVC {
566
568
  self.presentPaymentErrorVC(errorMessage: "No data received")
567
569
  }
568
570
  } else {
569
- self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
571
+ if let data = serviceData,
572
+ let responseObj = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
573
+ let message = responseObj["message"] as? String {
574
+ self.presentPaymentErrorVC(errorMessage: message)
575
+ } else {
576
+ self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
577
+ }
570
578
  }
571
579
  }
572
580
  task.resume()
@@ -576,18 +584,6 @@ class OTPVerificationVC: BaseVC {
576
584
  func paymentIntentWithSavedCardApi(customerId: String?) {
577
585
  showLoadingIndicator()
578
586
 
579
- // var components = URLComponents()
580
- // components.scheme = "https"
581
- // components.host = "stage-api.stage-easymerchant.io"
582
- // components.path = "/api/v1/charges"
583
- //
584
- // guard let serviceURL = components.url else {
585
- // print("Invalid URL")
586
- // hideLoadingIndicator()
587
- // return
588
- // }
589
-
590
- // Construct the full URL using baseURL from EnvironmentConfig and path from the endpoint
591
587
  let fullURL = EnvironmentConfig.baseURL + EnvironmentConfig.Endpoints.charges.path()
592
588
 
593
589
  guard let serviceURL = URL(string: fullURL) else {
@@ -604,6 +600,13 @@ class OTPVerificationVC: BaseVC {
604
600
  print("Setting clientToken header: \(token ?? "None")")
605
601
  request.addValue(token ?? "", forHTTPHeaderField: "Client-Token")
606
602
 
603
+ // Add API headers
604
+ if let apiKey = EnvironmentConfig.apiKey,
605
+ let apiSecret = EnvironmentConfig.apiSecret {
606
+ request.addValue(apiKey, forHTTPHeaderField: "x-api-key")
607
+ request.addValue(apiSecret, forHTTPHeaderField: "x-api-secret")
608
+ }
609
+
607
610
  let emailPrefix = email?.components(separatedBy: "@").first ?? ""
608
611
 
609
612
  var params: [String: Any] = [
@@ -689,7 +692,13 @@ class OTPVerificationVC: BaseVC {
689
692
  self.presentPaymentErrorVC(errorMessage: "No data received")
690
693
  }
691
694
  } else {
692
- self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
695
+ if let data = serviceData,
696
+ let responseObj = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
697
+ let message = responseObj["message"] as? String {
698
+ self.presentPaymentErrorVC(errorMessage: message)
699
+ } else {
700
+ self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
701
+ }
693
702
  }
694
703
  }
695
704
  task.resume()
@@ -708,19 +717,7 @@ class OTPVerificationVC: BaseVC {
708
717
  //MARK: - Account Charge Api
709
718
  func accountChargeApi(customerId: String?) {
710
719
  showLoadingIndicator()
711
-
712
- // var components = URLComponents()
713
- // components.scheme = "https"
714
- // components.host = "stage-api.stage-easymerchant.io"
715
- // components.path = "/api/v1/ach/charge"
716
- //
717
- // guard let serviceURL = components.url else {
718
- // print("Invalid URL")
719
- // hideLoadingIndicator()
720
- // return
721
- // }
722
-
723
- // Construct the full URL using baseURL from EnvironmentConfig and path from the endpoint
720
+
724
721
  let fullURL = EnvironmentConfig.baseURL + EnvironmentConfig.Endpoints.achCharge.path()
725
722
 
726
723
  guard let serviceURL = URL(string: fullURL) else {
@@ -737,6 +734,13 @@ class OTPVerificationVC: BaseVC {
737
734
  print("Setting clientToken header: \(token ?? "None")")
738
735
  request.addValue(token ?? "", forHTTPHeaderField: "Client-Token")
739
736
 
737
+ // Add API headers
738
+ if let apiKey = EnvironmentConfig.apiKey,
739
+ let apiSecret = EnvironmentConfig.apiSecret {
740
+ request.addValue(apiKey, forHTTPHeaderField: "x-api-key")
741
+ request.addValue(apiSecret, forHTTPHeaderField: "x-api-secret")
742
+ }
743
+
740
744
  guard let billingInfoData = billingInfoData else {
741
745
  print("Billing info data is nil")
742
746
  return
@@ -845,7 +849,13 @@ class OTPVerificationVC: BaseVC {
845
849
  self.presentPaymentErrorVC(errorMessage: "No data received")
846
850
  }
847
851
  } else {
848
- self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
852
+ if let data = serviceData,
853
+ let responseObj = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
854
+ let message = responseObj["message"] as? String {
855
+ self.presentPaymentErrorVC(errorMessage: message)
856
+ } else {
857
+ self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
858
+ }
849
859
  }
850
860
  }
851
861
  task.resume()
@@ -854,19 +864,7 @@ class OTPVerificationVC: BaseVC {
854
864
  //MARK: - Account Charge Api if billing info is nil and saved account
855
865
  func accountChargeWithSavedAccountApi(customerId: String?) {
856
866
  showLoadingIndicator()
857
-
858
- // var components = URLComponents()
859
- // components.scheme = "https"
860
- // components.host = "stage-api.stage-easymerchant.io"
861
- // components.path = "/api/v1/ach/charge"
862
- //
863
- // guard let serviceURL = components.url else {
864
- // print("Invalid URL")
865
- // hideLoadingIndicator()
866
- // return
867
- // }
868
-
869
- // Construct the full URL using baseURL from EnvironmentConfig and path from the endpoint
867
+
870
868
  let fullURL = EnvironmentConfig.baseURL + EnvironmentConfig.Endpoints.achCharge.path()
871
869
 
872
870
  guard let serviceURL = URL(string: fullURL) else {
@@ -883,6 +881,13 @@ class OTPVerificationVC: BaseVC {
883
881
  print("Setting clientToken header: \(token ?? "None")")
884
882
  request.addValue(token ?? "", forHTTPHeaderField: "Client-Token")
885
883
 
884
+ // Add API headers
885
+ if let apiKey = EnvironmentConfig.apiKey,
886
+ let apiSecret = EnvironmentConfig.apiSecret {
887
+ request.addValue(apiKey, forHTTPHeaderField: "x-api-key")
888
+ request.addValue(apiSecret, forHTTPHeaderField: "x-api-secret")
889
+ }
890
+
886
891
  let emailPrefix = email?.components(separatedBy: "@").first ?? ""
887
892
 
888
893
  var params: [String: Any] = [
@@ -967,7 +972,123 @@ class OTPVerificationVC: BaseVC {
967
972
  self.presentPaymentErrorVC(errorMessage: "No data received")
968
973
  }
969
974
  } else {
970
- self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
975
+ if let data = serviceData,
976
+ let responseObj = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
977
+ let message = responseObj["message"] as? String {
978
+ self.presentPaymentErrorVC(errorMessage: message)
979
+ } else {
980
+ self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
981
+ }
982
+ }
983
+ }
984
+ task.resume()
985
+ }
986
+
987
+ //MARK: - GrailPay Account Charge Api if user saved account
988
+ func grailPayAccountChargeApi(customerId: String?) {
989
+ showLoadingIndicator()
990
+
991
+ let fullURL = EnvironmentConfig.baseURL + EnvironmentConfig.Endpoints.achCharge.path()
992
+
993
+ guard let serviceURL = URL(string: fullURL) else {
994
+ print("Invalid URL")
995
+ hideLoadingIndicator()
996
+ return
997
+ }
998
+
999
+ var request = URLRequest(url: serviceURL)
1000
+ request.httpMethod = "POST"
1001
+ request.addValue("application/json", forHTTPHeaderField: "Content-Type")
1002
+
1003
+ let token = UserStoreSingleton.shared.clientToken
1004
+ print("Setting clientToken header: \(token ?? "None")")
1005
+ request.addValue(token ?? "", forHTTPHeaderField: "Client-Token")
1006
+
1007
+ if let apiKey = EnvironmentConfig.apiKey {
1008
+ request.addValue(apiKey, forHTTPHeaderField: "X-Api-Key")
1009
+ }
1010
+ if let apiSecret = EnvironmentConfig.apiSecret {
1011
+ request.addValue(apiSecret, forHTTPHeaderField: "X-Api-Secret")
1012
+ }
1013
+
1014
+ let params: [String: Any] = [
1015
+ "account_id": self.grailPayAccountID ?? "",
1016
+ "account_type": self.selectedGrailPayAccountType ?? "",
1017
+ "name": self.selectedGrailPayAccountName ?? "",
1018
+ "description": "payment checkout",
1019
+ "save_account": 1,
1020
+ "is_default": 1,
1021
+ "customer_id": customerId ?? ""
1022
+ ]
1023
+
1024
+ print(params)
1025
+
1026
+ do {
1027
+ let jsonData = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
1028
+ request.httpBody = jsonData
1029
+ if let jsonString = String(data: jsonData, encoding: .utf8) {
1030
+ print("JSON Payload: \(jsonString)")
1031
+ }
1032
+ } catch let error {
1033
+ print("Error creating JSON data: \(error)")
1034
+ hideLoadingIndicator()
1035
+ return
1036
+ }
1037
+
1038
+ let session = URLSession.shared
1039
+ let task = session.dataTask(with: request) { (serviceData, serviceResponse, error) in
1040
+
1041
+ DispatchQueue.main.async {
1042
+ self.hideLoadingIndicator() // Stop loader when response is received
1043
+ }
1044
+
1045
+ if let error = error {
1046
+ self.presentPaymentErrorVC(errorMessage: error.localizedDescription)
1047
+ return
1048
+ }
1049
+
1050
+ guard let httpResponse = serviceResponse as? HTTPURLResponse else {
1051
+ self.presentPaymentErrorVC(errorMessage: "Invalid response")
1052
+ return
1053
+ }
1054
+
1055
+ if httpResponse.statusCode == 200 || httpResponse.statusCode == 201 {
1056
+ if let data = serviceData {
1057
+ do {
1058
+ if let responseObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
1059
+ print("Response Data: \(responseObject)")
1060
+
1061
+ // Check if status is 0 and handle the error
1062
+ if let status = responseObject["status"] as? Int, status == 0 {
1063
+ let errorMessage = responseObject["message"] as? String ?? "Unknown error"
1064
+ self.presentPaymentErrorVC(errorMessage: errorMessage)
1065
+ } else {
1066
+ DispatchQueue.main.async {
1067
+ if let paymentDoneVC = self.storyboard?.instantiateViewController(withIdentifier: "PaymentDoneVC") as? PaymentDoneVC {
1068
+ paymentDoneVC.chargeData = responseObject
1069
+ paymentDoneVC.selectedPaymentMethod = self.selectedPaymentMethod
1070
+ paymentDoneVC.easyPayDelegate = self.easyPayDelegate
1071
+ self.navigationController?.pushViewController(paymentDoneVC, animated: true)
1072
+ }
1073
+ }
1074
+ }
1075
+ } else {
1076
+ self.presentPaymentErrorVC(errorMessage: "Invalid JSON format")
1077
+ }
1078
+ } catch let jsonError {
1079
+ self.presentPaymentErrorVC(errorMessage: "Error parsing JSON: \(jsonError)")
1080
+ }
1081
+ } else {
1082
+ self.presentPaymentErrorVC(errorMessage: "No data received")
1083
+ }
1084
+ } else {
1085
+ if let data = serviceData,
1086
+ let responseObj = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
1087
+ let message = responseObj["message"] as? String {
1088
+ self.presentPaymentErrorVC(errorMessage: message)
1089
+ } else {
1090
+ self.presentPaymentErrorVC(errorMessage: "HTTP Status Code: \(httpResponse.statusCode)")
1091
+ }
971
1092
  }
972
1093
  }
973
1094
  task.resume()
@@ -18,7 +18,10 @@ class PaymentDoneVC: UIViewController {
18
18
  @IBOutlet weak var lblHeadingDate: UILabel!
19
19
  @IBOutlet weak var lblHeadingTransactionID: UILabel!
20
20
  @IBOutlet weak var lbHeadingPaymentMethod: UILabel!
21
- @IBOutlet weak var lblHeadingTotal: UILabel!
21
+ @IBOutlet weak var lblHeadingTotal: UILabel!
22
+
23
+ @IBOutlet weak var viewSubscriptionId: UIStackView!
24
+ @IBOutlet weak var lblSubscriptionId: UILabel!
22
25
 
23
26
  var chargeData: [String: Any]?
24
27
  var selectedPaymentMethod: String?