Simple guide to using the paystack package for the R programming language

ebinabo
21/02/2019

Installation

To install this package run

devtools::install_github("ebinabo/paystack")

I understand newer versions of RStudio don't handle downloading from github properly so I've included a manual_installation script as well.

All the lines are commented by default so Ctrl + A, Ctrl + C, Ctrl + Enter. After it's done, delete that script and go to tabs -> Build -> Install and Restart

Please unzip the file to your downloads or anywhere but the reserved folder in ~Documents/R/win-lib/version folder, took me some time to figure that out

Set Keys

set_keys("", "sk_test_s3cr3tk3y")$secret # is equivalent to -H "Authorization: Bearer SECRET_KEY"
<request>
Headers:
* Authorization: Bearer sk_test_s3cr3tk3y

Notice how the commented line has an empty chr string and $ to subset the secret key. The first arg is the public key and could be accessed by filling the first arg and subset by replacing secret with public.

Set Keys contd.

Please keep your keys secure

  • Don't upload .Rhistory or .RData files to github public repos
  • If you're building a Shiny app, you could save them in .rda files and read from them. This could be done locally or internationally(lol), I mean on a secure server but you'll most likely have to do that manually.
set_keys("pk_test_pub1ickey", "")$public %>% saveRDS(file = "path_to_file")
  • Using the keyring package, you could read and write your keys to a secure location on your computer and still lock them with passwords, cool right?

Pay with Bank

The banks that currently support the pay with bank feature and their codes are shown on the right.

Access dataframe by running:

paystack::pay_with_banks
name code
Access Bank 044
ALAT by WEMA 035A
Fidelity Bank 070
First City Monument Bank 214
Guaranty Trust Bank 058
Sterling Bank 232
Union Bank of Nigeria 032
Unity Bank 215
Zenith Bank 057

General Function Structure

_function_name(authorization, path_param, ...)

The general function structure is shown above,

  • most of the functions require authorization which is the result of running set_keys() from the previous slides

  • path_param is any string that should be concatenated with the root api

paste(urls$transaction$base, "19822", sep = "/") # to request a transaction with id 19822
[1] "https://api.paystack.co/transaction/19822"

General Function Structure Contd

  • … are for you to pass in any other body parameters you might want to use, however, required parameters must be passed in so as to avoid “status”: false

eg of how to enter them is

_function_name(authorization, path_param, reference = "28374", amount = 500000)

for as many query parameters you might want to include.

if you would need a nested json file returned, just wrap the inner details in a list.

list(email = "customer@email.com", amount = 500000)

Resources