@@ -60,9 +60,9 @@ class ViewController: UIViewController, PSTCKPaymentCardTextFieldDelegate {
6060 }
6161
6262 @IBAction func chargeCard( _ sender: UIButton ) {
63-
63+
6464 dismissKeyboardIfAny ( )
65-
65+
6666 // Make sure public key has been set
6767 if ( paystackPublicKey == " " || !paystackPublicKey. hasPrefix ( " pk_ " ) ) {
6868 showOkayableMessage ( " You need to set your Paystack public key. " , message: " You can find your public key at https://dashboard.paystack.co/#/settings/developer . " )
@@ -71,9 +71,9 @@ class ViewController: UIViewController, PSTCKPaymentCardTextFieldDelegate {
7171 }
7272
7373 Paystack . setDefaultPublicKey ( paystackPublicKey)
74-
74+
7575 if cardDetailsForm. isValid {
76-
76+
7777 if backendURLString != " " {
7878 fetchAccessCodeAndChargeCard ( )
7979 return
@@ -82,7 +82,7 @@ class ViewController: UIViewController, PSTCKPaymentCardTextFieldDelegate {
8282
8383
8484 }
85-
85+
8686 }
8787
8888 func outputOnLabel( str: String ) {
@@ -97,37 +97,10 @@ class ViewController: UIViewController, PSTCKPaymentCardTextFieldDelegate {
9797
9898 func fetchAccessCodeAndChargeCard( ) {
9999 if let url = URL ( string: backendURLString + " /new-access-code " ) {
100- let session = URLSession ( configuration: URLSessionConfiguration . default)
101- self . outputOnLabel ( str: " Requesting access code " )
102- self . chargeCardButton. isEnabled = false ;
103- self . chargeCardButton. setTitle ( " Charging card... " , for: UIControlState . disabled)
104- session. dataTask ( with: url, completionHandler: { data, response, error in
105- let successfulResponse = ( response as? HTTPURLResponse ) ? . statusCode == 200
106- if successfulResponse && error == nil && data != nil {
107- // All was well
108- if let newCode = NSString ( data: data!, encoding: String . Encoding. utf8. rawValue) {
109- self . outputOnLabel ( str: " access code: " + ( newCode as String ) )
110- self . chargeWithSDK ( newCode: newCode)
111- } else {
112- print ( " <Unable to read response> " )
113- }
114- } else {
115- if let e= error {
116- print ( e. localizedDescription)
117- self . outputOnLabel ( str: e. localizedDescription)
118- } else {
119- // There was no error returned though status code was not 200
120- print ( " There was an error communicating with your payment backend. " )
121- self . outputOnLabel ( str: " There was an error communicating with your payment backend. " )
122- }
123- self . chargeCardButton. isEnabled = true ;
124- self . chargeCardButton. setTitle ( " Charge card " , for: UIControlState . disabled)
125-
126-
127- }
128- } ) . resume ( )
129-
130- return
100+ self . makeBackendRequest ( url: url, message: " fetching access code " , completion: { str in
101+ self . outputOnLabel ( str: " Fetched access code: " + str)
102+ self . chargeWithSDK ( newCode: str as NSString )
103+ } )
131104 }
132105 }
133106
@@ -144,6 +117,7 @@ class ViewController: UIViewController, PSTCKPaymentCardTextFieldDelegate {
144117 if let reference= reference {
145118 self . showOkayableMessage ( " An error occured while completing " + reference, message: errorString)
146119 self . outputOnLabel ( str: reference + " : " + errorString)
120+ self . verifyTransaction ( reference: reference)
147121 } else {
148122 self . showOkayableMessage ( " An error occured " , message: errorString)
149123 self . outputOnLabel ( str: errorString)
@@ -156,9 +130,43 @@ class ViewController: UIViewController, PSTCKPaymentCardTextFieldDelegate {
156130 } , didTransactionSuccess: { ( reference) -> Void in
157131 self . outputOnLabel ( str: " succeeded: " + reference)
158132 self . chargeCardButton. isEnabled = true ;
133+ self . verifyTransaction ( reference: reference)
159134 } )
160135 return
161136 }
162-
163-
137+
138+ func verifyTransaction( reference: String ) {
139+ if let url = URL ( string: backendURLString + " /verify/ " + reference) {
140+ makeBackendRequest ( url: url, message: " verifying " + reference, completion: { ( str) -> Void in
141+ self . outputOnLabel ( str: " Message from paystack on verifying " + reference + " : " + str)
142+ } )
143+ }
144+ }
145+
146+ func makeBackendRequest( url: URL , message: String , completion: @escaping ( _ result: String ) -> Void ) {
147+ let session = URLSession ( configuration: URLSessionConfiguration . default)
148+ self . outputOnLabel ( str: " Backend: " + message)
149+ session. dataTask ( with: url, completionHandler: { data, response, error in
150+ let successfulResponse = ( response as? HTTPURLResponse ) ? . statusCode == 200
151+ if successfulResponse && error == nil && data != nil {
152+ if let str = NSString ( data: data!, encoding: String . Encoding. utf8. rawValue) {
153+ completion ( str as String )
154+ } else {
155+ self . outputOnLabel ( str: " <Unable to read response> while " + message)
156+ print ( " <Unable to read response> " )
157+ }
158+ } else {
159+ if let e= error {
160+ print ( e. localizedDescription)
161+ self . outputOnLabel ( str: e. localizedDescription)
162+ } else {
163+ // There was no error returned though status code was not 200
164+ print ( " There was an error communicating with your payment backend. " )
165+ self . outputOnLabel ( str: " There was an error communicating with your payment backend while " + message)
166+ }
167+ }
168+ } ) . resume ( )
169+ }
170+
171+
164172}
0 commit comments