Skip to content

Commit fef38f1

Browse files
committed
[chore] cleaner sample code
1 parent d376878 commit fef38f1

3 files changed

Lines changed: 49 additions & 40 deletions

File tree

Example/Paystack iOS Example/Base.lproj/Main.storyboard

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<fontDescription key="fontDescription" type="system" pointSize="10"/>
5757
<nil key="textColor"/>
5858
<nil key="highlightedColor"/>
59+
<color key="shadowColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
5960
</label>
6061
</subviews>
6162
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
@@ -91,7 +92,7 @@
9192
</scene>
9293
</scenes>
9394
<resources>
94-
<image name="caps" width="840" height="525"/>
95+
<image name="caps" width="479" height="307"/>
9596
</resources>
9697
<simulatedMetricsContainer key="defaultSimulatedMetrics">
9798
<simulatedStatusBarMetrics key="statusBar"/>

Example/Paystack iOS Example/ViewController.swift

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ If you choose the `chargeCard` route, we charge cards you send using parameters
239239
// transactionParams.reference = "ChargedFromiOSSDK@"
240240

241241
PSTCKAPIClient.shared().chargeCard(cardParams, forTransaction: transactionParams, on: viewController,
242-
didEndWithError: { (error) -> Void in
242+
didEndWithError: { (error, reference) -> Void in
243243
handleError(error)
244244
}, didRequestValidation: { (reference) -> Void in
245245
// an OTP was requested, transaction has not yet succeeded

0 commit comments

Comments
 (0)