Skip to content

Commit 22877bc

Browse files
committed
2 parents e3ec2ad + b1dbe92 commit 22877bc

2 files changed

Lines changed: 249 additions & 58 deletions

File tree

GUIDE.md

Lines changed: 237 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,21 @@
22

33
If you want to build a mobile app like [Afro](http://www.getafrocab.com) and enable people to make purchases directly in your app, our iOS and [Android](https://github.com/PaystackHQ/paystack-android) libraries can help.
44

5-
Accepting payments in your app involves 4 steps, which we'll cover in this guide:
5+
Accepting payments in your app after collecting card information can be acieved in either of two ways, which we'll cover in this guide. The `authorization code` from either option can be used on your server in future to charge the cards.
66

7-
- Collecting credit card information from your customer
8-
- Converting the credit card information to a _**single-use**_ token
9-
- Sending this token to your server to create a charge which provides an authorizaton code if successful
10-
- Using the returned authorization code to make future charge requests
7+
#### Option 1 - Charge the card directly from App
8+
- Charging the credit card and get the transaction `reference`
9+
- Verifying the transaction on your server which provides an `authorizaton code` if successful
1110

12-
## Getting Started
13-
14-
### Step 1: Install the library
15-
16-
#### Using [CocoaPods](https://cocoapods.org/)
17-
18-
We recommend using [CocoaPods](https://cocoapods.org/) to install the Paystack iOS library, since it makes it easy to keep your app's dependencies up to date.
19-
20-
If you haven't set up Cocoapods before, their site has installation instructions. Then, add pod 'Paystack' to your Podfile, and run pod install.
11+
or
2112

22-
(Don't forget to use the .xcworkspace file to open your project in Xcode, instead of the .xcodeproj file, from here on out.)
13+
#### Option 2 - Tokenize on App, charge on server
14+
- Converting the credit card information to a _**single-use**_ `token`
15+
- Sending this token to your server to create a charge which provides an `authorizaton code` if successful
2316

24-
#### Using Carthage
17+
## Getting Started
2518

26-
We also support installing our SDK using Carthage. You can simply add github "paystackhq/paystack-ios" to your Cartfile, and follow the Carthage installation instructions.
19+
### Step 1: Install the library
2720

2821
#### Manual installation
2922

@@ -37,6 +30,18 @@ We also publish our SDK as a static framework that you can copy directly into yo
3730
- Click 'Add'.
3831
- In your project settings, go to the "Build Settings" tab, and make sure -ObjC is present under "Other Linker Flags".
3932

33+
#### Using [CocoaPods](https://cocoapods.org/)
34+
35+
We recommend using [CocoaPods](https://cocoapods.org/) to install the Paystack iOS library, since it makes it easy to keep your app's dependencies up to date.
36+
37+
If you haven't set up Cocoapods before, their site has installation instructions. Then, add pod 'Paystack' to your Podfile, and run pod install.
38+
39+
(Don't forget to use the .xcworkspace file to open your project in Xcode, instead of the .xcodeproj file, from here on out.)
40+
41+
#### Using Carthage
42+
43+
We also support installing our SDK using Carthage. You can simply add github "paystackhq/paystack-ios" to your Cartfile, and follow the Carthage installation instructions.
44+
4045
### Step 2: Configure API keys
4146

4247
First, you'll want to configure Paystack with your publishable API key. We recommend doing this in your `AppDelegate`'s `application:didFinishLaunchingWithOptions:` method so that it'll be set for the entire lifecycle of your app.
@@ -153,19 +158,133 @@ func paymentCardTextFieldDidChange(textField: PSTCKPaymentCardTextField) {
153158

154159
#### Building your own form
155160

156-
If you build your own payment form, you'll need to collect at least your customers' card numbers, CVC and expiration dates.
161+
If you build your own payment form, you'll need to collect at least your customers' card numbers, CVC and expiration dates.
162+
163+
### Step 4: Assembling Card information into `PSTCKCardParams`
164+
165+
If you're using `PSTCKPaymentCardTextField`, simply call its `cardParams` property to get the assembled card data.
166+
167+
```Swift
168+
let cardParams = paymentTextField.cardParams as PSTCKCardParams
169+
```
170+
171+
```Objective-C
172+
PSTCKCardParams cardParams = [paymentTextField cardParams];
173+
```
174+
175+
If you are using your own form, you can assemble the data into an `PSTCKCardParams` object thus:
176+
177+
```Swift
178+
let cardParams = PSTCKCardParams.init();
179+
180+
// then set parameters thus from card
181+
cardParams.number = card.number
182+
cardParams.cvc = card.cvc
183+
cardParams.expYear = card.expYear
184+
cardParams.expMonth = card.expMonth
185+
186+
// or directly
187+
cardParams.number = "2963781976222"
188+
cardParams.cvc = "289"
189+
cardParams.expYear = 2018
190+
cardParams.expMonth = 9
191+
```
192+
193+
```Objective-C
194+
PSTCKCardParams cardParams = [[PSTCKCardParams alloc] init];
195+
196+
// then set parameters thus from card
197+
cardParams.number = [card number];
198+
cardParams.cvc = [card cvc];
199+
cardParams.expYear = [card expYear];
200+
cardParams.expMonth = [card expMonth];
201+
202+
// or directly
203+
cardParams.number = "2963781976222";
204+
cardParams.cvc = "289";
205+
cardParams.expYear = 2018;
206+
cardParams.expMonth = 9;
207+
```
208+
209+
### Step 4: Getting payments
210+
211+
Our libraries shoulder the burden of PCI compliance by helping you avoid the need to send card data directly to your server. Instead, our libraries send credit card data directly to our servers, where we can charge them or create tokens which you charge on your server.
212+
213+
214+
#### Step 4 Option 1: Charge Card
215+
216+
If you choose the `chargeCard` route, we charge cards you send using parameters provided in your `PSTCKTransactionParams`. Assemble Transaction parameters into `PSTCKTransactionParams`, and send them along with the `cardParams` to get a charge.
217+
218+
```Swift
219+
@IBAction func charge(sender: UIButton) {
220+
// cardParams already fetched from our view or assembled by you
221+
let transactionParams = PSTCKTransactionParams.init();
157222

158-
### Step 4: Creating Tokens
223+
transactionParams.amount = 1390;
224+
transactionParams.metadata = "{\"custom_fields\":[{\"display_name\":\"Via\",\"variable_name\":\"via\",\"value\":\"iOS\"}]}";
225+
transactionParams.email = "e@ma.il";
159226

160-
Our libraries shoulder the burden of PCI compliance by helping you avoid the need to send card data directly to your server. Instead, our libraries send credit card data directly to our servers, where we can convert them to tokens. You should charge these tokens later in your server-side code to get an authorization code.
227+
// check https://developers.paystack.co/docs/split-payments-overview for details on how these work
228+
// transactionParams.subaccount = "ACCT_80d907euhish8d";
229+
// transactionParams.bearer = "subaccount";
230+
// transactionParams.transaction_charge = 280;
161231

162-
#### Using `PSTCKCardParams`
232+
// if a reference is not supplied, we will give one
233+
// transactionParams.reference = "ChargedFromiOSSDK@"
163234

164-
If you're using `PSTCKPaymentCardTextField` or your own form, you can assemble the data into an `PSTCKCardParams` object. Once you've collected the card number, expiration, and CVC, package them up in an `PSTCKCardParams` object and invoke the `createTokenWithCard:` method on the `PSTCKAPIClient` class, instructing the library to send off the credit card data to Paystack and return a token.
235+
PSTCKAPIClient.shared().chargeCard(cardParams, forTransaction: transactionParams, on: viewController,
236+
didEndWithError: { (error) -> Void in
237+
handleError(error)
238+
}, didRequestValidation: { (reference) -> Void in
239+
// an OTP was requested, transaction has not yet succeeded
240+
}, didTransactionSuccess: { (reference) -> Void in
241+
// transaction may have succeeded, please verify on server
242+
})
243+
}
244+
```
245+
246+
```Objective-C
247+
- (IBAction)charge:(UIButton *)sender {
248+
// cardParams already fetched from our view or assembled by you
249+
250+
PSTCKTransactionParams transactionParams = [[PSTCKTransactionParams alloc] init];
251+
252+
transactionParams.amount = 1390;
253+
transactionParams.metadata = @"{\"custom_fields\":[{\"display_name\":\"Via\",\"variable_name\":\"via\",\"value\":\"iOS\"}]}";
254+
transactionParams.email = @"e@ma.il";
255+
256+
// check https://developers.paystack.co/docs/split-payments-overview for details on how these work
257+
// transactionParams.subaccount = @"ACCT_80d907euhish8d";
258+
// transactionParams.bearer = @"subaccount";
259+
// transactionParams.transaction_charge = 280;
260+
261+
// if a reference is not supplied, we will give one
262+
// transactionParams.reference = "ChargedFromiOSSDK@"
263+
264+
[[PSTCKAPIClient sharedClient] chargeCard:cardParams
265+
forTransaction:transactionParams
266+
on: viewController,
267+
didEndWithError:^(NSError *error){
268+
[self handleError:error];
269+
}
270+
didRequestValidation: ^(NSString *reference){
271+
// an OTP was requested, transaction has not yet succeeded
272+
}
273+
didTransactionSuccess: ^(NSString *reference){
274+
// transaction may have succeeded, please verify on server
275+
}];
276+
277+
}
278+
```
279+
280+
#### Step 4 Option 2: Using Tokens
281+
282+
If you choose the `createToken` route, we convert cards sent to tokens. You should charge these tokens later in your server-side code to get an authorization code.
165283

166284
```Swift
167285
@IBAction func save(sender: UIButton) {
168-
if let card = paymentTextField.cardParams as? PSTCKCardParams {
286+
// cardParams Fetched from our view or built by you
287+
if let card = cardParams as? PSTCKCardParams {
169288
PSTCKAPIClient.sharedClient().createTokenWithCard(card) { (token, error) -> Void in
170289
if let error = error {
171290
handleError(error)
@@ -180,14 +299,15 @@ If you're using `PSTCKPaymentCardTextField` or your own form, you can assemble t
180299

181300
```Objective-C
182301
- (IBAction)save:(UIButton *)sender {
302+
// cardParams Fetched from our view or built by you
183303
[[PSTCKAPIClient sharedClient]
184-
createTokenWithCard:self.paymentTextField.cardParams
304+
createTokenWithCard:cardParams
185305
completion:^(PSTCKToken *token, NSError *error) {
186306
if (error) {
187307
[self handleError:error];
188308
} else {
189309
// call your createBackendChargeWithToken function
190-
// A sample is presented in step 5
310+
// A sample is presented in step 6
191311
}
192312
}];
193313
}
@@ -197,7 +317,72 @@ In the example above, we're calling `createTokenWithCard:` when a save button is
197317

198318
Handling error messages and showing activity indicators while we're creating the token is up to you.
199319

200-
### Step 5: Sending the token to your server
320+
### Step 6 Option 1: Sending the reference to your server
321+
322+
The blocks you gave to `chargeCard` will be called whenever Paystack returns with a reference (or error). You'll need to send the reference off to your server so you can verify the transactions.
323+
324+
Here's how it looks:
325+
326+
```Swift
327+
// ViewController.swift
328+
329+
func verifyCharge(reference: String) {
330+
let url = NSURL(string: "https://example.com/verify")!
331+
let request = NSMutableURLRequest(URL: url)
332+
request.HTTPMethod = "POST"
333+
let postBody = "reference=reference"
334+
let postData = postBody.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
335+
session.uploadTaskWithRequest(request, fromData: postData, completionHandler: { data, response, error in
336+
let successfulResponse = (response as? NSHTTPURLResponse)?.statusCode == 200
337+
if successfulResponse && error == nil && data != nil{
338+
// All was well
339+
let newStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
340+
print(newStr) // All we did here is log it to the output window
341+
} else {
342+
if let e=error {
343+
print(e.description)
344+
} else {
345+
// There was no error returned though status code was not 200
346+
print("There was an error communicating with your payment backend.")
347+
// All we did here is log it to the output window
348+
}
349+
350+
}
351+
}).resume()
352+
}
353+
```
354+
355+
```Objective-C
356+
// ViewController.m
357+
358+
- (void)verifyCharge:(String *)reference
359+
{
360+
NSURL *url = [NSURL URLWithString:@"https://example.com/verify"];
361+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
362+
request.HTTPMethod = @"POST";
363+
NSString *body = [NSString stringWithFormat:@"reference=%@", reference];
364+
request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
365+
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
366+
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
367+
NSURLSessionDataTask *task =
368+
[session dataTaskWithRequest:request
369+
completionHandler:^(NSData *data,
370+
NSURLResponse *response,
371+
NSError *error) {
372+
if (error) {
373+
...
374+
} else {
375+
...
376+
}
377+
}];
378+
[task resume];
379+
}
380+
381+
```
382+
383+
On the server, you just need to implement an endpoint that will accept the parameter: `reference`. Make sure any communication with your server is SSL secured to prevent eavesdropping.
384+
385+
### Step 6 Option 2: Sending the token to your server
201386

202387
The block you gave to `createToken` will be called whenever Paystack returns with a token (or error). You'll need to send the token off to your server so you can, for example, charge the card.
203388

@@ -266,16 +451,38 @@ On the server, you just need to implement an endpoint that will accept the param
266451

267452
--------------------
268453

269-
### Step 6: Implement payment on your server
454+
### Step 6 Option 1: Implement verification on your server
455+
Verify a charge by calling our REST API. An `authorization_code` will be returned once the card has been charged successfully. You can learn more about our API [here](https://developers.paystack.co/docs/getting-started).
456+
457+
**Endpoint:** GET: https://api.paystack.co/transaction/verify
458+
459+
**Documentation:** https://developers.paystack.co/docs/verify-transaction
460+
461+
**Parameters:**
462+
463+
- reference - the transaction reference
464+
465+
**Example**
466+
467+
```bash
468+
$ curl https://api.paystack.co/transaction/verify/ChargedFromiOSSDK%40 \
469+
-H "Authorization: Bearer SECRET_KEY" \
470+
-H "Content-Type: application/json" \
471+
-X GET
472+
473+
```
474+
### Step 6 Option 2: Implement payment on your server
270475
Create a charge by calling our REST API. An `authorization_code` will be returned once the _single-use_ token has been charged successfully. You can learn more about our API [here](https://developers.paystack.co/docs/getting-started).
271476

272-
**Endpoint:** https://api.paystack.co/transaction/charge_token
477+
**Endpoint:** POST: https://api.paystack.co/transaction/charge_token
478+
479+
**Documentation:** https://developers.paystack.co/docs/charge-token
273480

274481
**Parameters:**
275482

276-
- token - the token you want to charge
483+
- token - the token you want to charge (required)
484+
- reference - unique reference
277485
- email - customer's email address (required)
278-
- reference - unique reference (required)
279486
- amount - Amount in Kobo (required)
280487

281488
**Example**

0 commit comments

Comments
 (0)