DEV Community

Cover image for How to Integrate Paystack payment system

How to Integrate Paystack payment system

Jeremy Ikwuje on December 02, 2018

In this guide, you'll learn how to integrate Paystack payment system on your website. This guide is comprehensive, so you should go through every...
Collapse
 
knightsgram profile image
Knights Gram

I want to add paystack to my site. It got to a point where I don't know what's next.
I don't know where to add the files

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Did you follow the steps? Explain your situation.

Collapse
 
knightsgram profile image
Knights Gram

The problem is I don't even know how to start.
Can you give me a tutorial on how to start from scratch?. I already created a button to redirect to paystack but I don't know what to add and where to place the yabacon php files

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje

If you know how to set up a PHP project, then simply follow the tutorial from the beginning.

Ignore the yabacon PHP files for now, and follow the tutorial.

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
knightsgram profile image
Knights Gram • Edited

How do I know my site callback url
I want the money to be added to the user wallet after successful payment. How do I add that?
In this case where I want user wallet to be updated, which method is best. Inline or standard. Or both

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje

Both are fine.

The inline method is great for user experience if you're comfortable with AJAX.

The callback_url is for the standard method. This is the PHP file paystack will redirect the user to after making a payment.

In this page, you confirm the user payment and update the user wallet.

Thread Thread
 
knightsgram profile image
Knights Gram

How do I update the wallet?
Is there a special code for that or I'll be the one to write it,
I think my site already has d callback but I don't know where it will be.

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje • Edited

You'll have to do that yourself, or if you need help, you can message me on telegram, I actually wrote a wallet example.

Thread Thread
 
knightsgram profile image
Knights Gram

Username not found on telegram

Let's chat via Twitter then @knightsgram

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje

Oh, I made a typo, You can try it t.me/ijsucceed

Collapse
 
patabah profile image
PatAbah • Edited

You said $email is my customer's email. So I tried getting my user's email from $_POST but each time I refer to $_POST anywhere in initialize.php I get a redirection error from browser "Page isn't redirecting properly"

This also means I can't pass the amount user is willing to pay via post in initialize.php

This is super weird and annoying. I'll appreciate any help please. Thanks

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

"Page isn't redirecting properly"? Uncommon. Try clearing your cache and cookie(important).

Each time you refer to $_POST, what do you mean?

Try isseting if the $_POST request was sent to see where the problem is coming from.

if( isset( $_POST['submit'] ) { 

You can engage with me live on twitter, that will be faster.

Collapse
 
patabah profile image
PatAbah • Edited

Your prompt response already makes me feel better :)

BTW, I was using Firefox on Android, I switched to Chrome... It worked. Back to Firefox and it worked too!

I don't know how, but I think the issue is resolved. Thanks.

One more thing...
For production, after replacing test_key with my real secret key, would PayStack override my callback_url? Or is it compulsory to set the callback_url in the field provided the Developer Dashboard?

I passed a $_GET parameter to the callback url containing specific subscription plans I offer so I could update the my DB with the parameter (plan) on success.

Is there a better way to do this?

Thanks once again

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje • Edited

No, Pat, as long as you set the callback_url in your code, then you're fine.

Thread Thread
 
patabah profile image
PatAbah

Thanks man. You're za best!

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje

Pleasure, thanks.

Collapse
 
ezuruikechinedu profile image
Chinedu • Edited

same here..but the values for $email and $amount in stored in sessions.
I get undefined _session variables..
Do i have to session_start()in both initialize.php and callback.php?

Thread Thread
 
ezuruikechinedu profile image
Chinedu

Don't worry...i've DONE IT

Thread Thread
 
elouisramsey profile image
elouisramsey

yo, please how did you solve this
using JS and I keep getting undefined

Collapse
 
okewater_1 profile image
Nedu

I have successfully added paystack to my plugin but i cannot update the plugin with payment information.
How can i add this code to the paystack file to be able to increase cause with payment details?

Collapse
 
holynation profile image
Alatise Oluwaseun • Edited

Good afternoon @ijsucceed .
I really love your code for it simplicity. However, i did love to make some corrections starting with the initialize.php.
// this part of the code is wrong since an array is returned, so it should not be
if(!$tranx->status){
// there was an error from the API
print_r('API returned error: ' . $tranx['message']);
}
// the corrected version
if(!$tranx['status']){
print_r('API returned error: ' . $tranx['message']);
}
Thank you.

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

You're right @holynation .

I just ran some few tests. The actual issue came from the Paystack documentation.

Though it will be hard for one to notice ( including Paystack developers) since the page gets redirected to the checkout form.

header('Location: ' . $tranx['data']['authorization_url']);

Thanks.

Collapse
 
holynation profile image
Alatise Oluwaseun

That is true. I have actually been looking for that too in their documentation.
Also i wanna ask again, how do i send phone number and fullname along side the initialize.php api

Collapse
 
oyeh20 profile image
oyeh20

thanks very much jeremiah, you provide what i have been looking for. please i still have a little problem. the code work successfully, but know, i need php code to insert the figure to the user wallet or account. so that he can confirm the transaction he made. regards

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Which user wallet are you referring to? is it that of Paystack or your own platform. Please specify.

Collapse
 
oyeh20 profile image
oyeh20 • Edited

I'm talking about my platform. Take for example, a user login to his/her account in my platform, and deposit money, using the paystack payment gateway.. Now I'm looking for the php script to give the user value, (e.g. he/she deposits #500 using paystack, and it was successful,, when he/she come back to my platform, he/she has to see the #500 in her account, to confirm that the payment he/she made was successful)

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje • Edited

You're to write the script yourself. You do that in the callback file.

if('success' == $tranx->data->status){
// transaction was successful...
// send the value the user made to the database and redirect back to any
// file you want.
}

Hope you get it?

Thread Thread
 
oyeh20 profile image
oyeh20

that is my issue now. have write many php script code with the little knowledge i have, but nothing happen. please i need a sample of already working php code, to learn from. i will be glad if anyone can share with me. regards

Thread Thread
 
kodnificent profile image
Victor Mbamara 🇳🇬

you need to create a webhook url where paystack sends a charge.success event to, if the payment was made successfully. It's in that webhook file you can update the users wallet if a charge.success event was received. Visit paystack documentation page for a better understanding.

Collapse
 
ibukunsoremekun profile image
Ibukun Soremekun

For those that will like to test these on local host server
you should follow the steps below

  1. ADD CURLOPT_SSL_VERIFYPEER => false, code to your curl_setopt_array()
  2. then set your callback url to the full your callback.php eg: localhost/PaystackProject/callback.php
Collapse
 
chuma_nd_ok profile image
Chuma Ndubuisi Okeke

Wow, Love this detailed tutorials @ijsucceed . Please am faced with a challenge.
i want to make for recurring debit but i was not able to retrieve "authentication code and card last four digits" after the successful payment. Help me out sir.

please see developers.paystack.co/reference#c...
.....
"gateway_response": "Successful",
"authorization": {
"authorization_code": "AUTH_72btv547",
"card_type": "mastercard",
"last4": "1839",
........

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Sorry, bro. I have no experience working on the recurring payment API.

Collapse
 
chuma_nd_ok profile image
Chuma Ndubuisi Okeke

Thanks for your prompt reply sir.

Collapse
 
adejumosamson profile image
AdejumoSamson

Hi Jeremiah.

Thanks for the detailed instructions.
I've been able to implement Paystack gateway successfully and gets a redirect to the specified callback URL.

But I'm having issues transferring data from the payment page to the webook URL specified on the Paystack dashboard.

Any hint on how that can be achieved?

Thank you.

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Welcome! Have you taken a look at this?

Collapse
 
kelvin070 profile image
kelvin070

Please I have an ecomerce site and I want a paystack pay button but I want the script that you will take any sum from the total cost on the cart and show to the customer as what to pay rather than me adding the price please

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

You'll have to do that yourself, or if you need help, you can message me on telegram.

Collapse
 
stevebrain_dev profile image
Godwin Stephen • Edited

Hi, @ijsucceed , Thanks for the tuts. It really helped a lot as it is simple and detailed.
I have been able to integrate and I can successfully pay. Thumbs Up to you!.

but I want to be able to add other specific information to the API endpoint and also retrieve it at callback.php for further details verification before giving value to the customer.

I'm giving multiple values to different tables in the DB. e.g after successful payment, a customer is getting point value, product value, and an additional bonus value.

All these will have to verify the transaction match with their tables respectively before I can give value to the customer...

I tried adding extra form data to the form that is sent to initialize.php. but I'm not able to get those values, and I can't retrieve them from the API as well since it wasn't sent in the first place.

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Okay, getting you clearly, you want to pass some extra but important data to the API. And you want to get this extra data during the verification.

If that's the case:

Kindly contact me on Telegram or WhatsApp so we do a live session.

Collapse
 
gottfried profile image
Gottfried

Hi IJ
I would like to know if there is a way of adding customer details to this specific way of integrating. I have tried several times but i cannot seem to get it right.

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Late reply, but you can pass customer details through the metadata:

  'callback_url' => $callback_url,
  'metadata' => [
      'custom_fields' => [
        "first_name" => "John",
        "last_name" => "Micheal",
        'cart_id' => 84920392,
        "custom" => "any thing here"
      ]
  ]

I hope you got it.

Collapse
 
naijanewsworld profile image
Nigeria News World

Is there a way to create a form to enable customers input their details instead of having a predefined details on the dashboard?

Collapse
 
ezuruikechinedu profile image
Chinedu

got this error:
API returned error: Authorization URL createdArray ( [status] => 1 [message] => Authorization URL created [data] => Array ( [authorization_url] => checkout.paystack.com/...............

Whats the problem

Collapse
 
ijsucceed profile image
Jeremy Ikwuje • Edited

Be sure your secret key is used in both initialization and verification. And if you're on live mode, then you have to be on a live server too.

Collapse
 
mghq_yt profile image
MGHQ-MobileGamerzHQ[YT]

has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
please I keep getting this error, what can I do, I get it when it goes to the
header('Location: ' . $tranx['data']['authorization_url']);

Collapse
 
1delcab profile image
1delcab

Nice and clear explanation.

My problem is that the call back function does not provide the payment details (I.e the customer email) so as to insert in it MySQLi data base.
Or I'm to use session to keep the record

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

The payment details are usually sent (as JSON) to your webhook url.

But, just like you said, you should use the user session, then make sure you verify the session with the user details in your database.

Collapse
 
segzyboiy profile image
SEGUN ISREAL (codeAdept)

Thanks for this post, really helpful, but please do you have any idea on how to integrate Paystack into Django WebApps? I have been looking for a way around this for days now, any help will be appreciated. Thanks in anticipation.

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Not really. I never built anything with Django before. Thank you.

Collapse
 
sirabdull profile image
Abdullahi Akintayo

th Api IS giving an error

: Invalid keyArray ( [status] => [message] => Invalid key )
Warning: Undefined array key "data" in C:\Users\ABDUL\Desktop\exam\nms-online-compedium-1\backend\server\phppages\subscription\initialize.php on line 47

Collapse
 
abbatronics profile image
Abbatronics • Edited

Jeremy pls help

Hello, I'm new here, I need help on how to integrate paystack standard to my ecommerce website which was written in django. I want to know which folder to place the initialize.php file in the django, also can i use include command to include it to checkout.HTML file that already exit, also do I need to create a model.py for the php file. I need your help thanks

Collapse
 
billyjeem123 profile image
Billyhadiat

Hi Jeremiah.

Thanks for the detailed instructions.
I've been able to implement Paystack gateway successfully and gets a redirect to the specified callback URL.

But i have question.... if i want to use my custom form for the payment checkout in the sense that remember that if the -> *initialise *<-endpoint is sucessful, we direct a user to a {paystack checkout page}..

let assume that i am not using paystack checkout if i want to send user account details to the initialise endpoint, what parameter would i send for the account details or can i just declare a parameter of my choice the details and

Thank you.

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

If you're looking to pass custom information, what you do is to pass those information in a property called meta.

Collapse
 
miss_my_pals profile image
Gbenga • Edited

@ijsucceed Thanks for the tutorial. Its very detailed. But am unable to get it redirect back to my site(the callback url). I don't know why. What version is the tutorial based on(Mine is version2+)

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Did you set the callback_url?

Collapse
 
miss_my_pals profile image
Gbenga

Yes, I did

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje

I don't suggest version 2, it's currently in Beta. Money here, hackers everywhere.

Thread Thread
 
cosmasmary6 profile image
cosmasmary6@gmail.com

Good day sir. please i really need your help. Am using Paystack Inline but the list of banks i need are not in the list already made in the Api. please is there anyway i can add mine.

Collapse
 
legaciespanda profile image
Ernest Obot

How do I retrieve list of transaction or transaction history with paystack API

Collapse
 
afomaverick profile image
💲 Maverick💲

Can you help me please? I want to be able to submit a form on the basis of a successful paystack payment, is there any way to go about this>

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Sure, you can do that by passing those form data through the metadata array during initialization:

  'callback_url' => $callback_url,
  'metadata' => [
      'custom_fields' => [
        "first_name" => "John",
        "last_name" => "Micheal",
        'cart_id' => 84920392,
        "custom" => "any thing here"
      ]
   ]
Collapse
 
niel_som profile image
justchisom_☁

Are you using laravel??

Collapse
 
abayomi01 profile image
Abayomi01

Hi Jeremiah,

i am getting this error in test mode after initiating a transaction "API returned error: Authorization URL created" am I doing something wrong.

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Are you on a Live server or Local? The callback.php should be on a live server.

Collapse
 
abayomi01 profile image
Abayomi01

I am on a live server and the callback.php is also on the live server.

Thread Thread
 
ijsucceed profile image
Jeremy Ikwuje

Since the error is from the API, I suggest you do it again.

Collapse
 
_king_salu profile image
JAs

well detailed analogy... God bless you man!

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Amen, thanks.

Collapse
 
kezzyj profile image
karachi david

thanks so much for your explanation but can i use the inline method with php or only the standard method

Collapse
 
naijanewsworld profile image
Nigeria News World

The inline code actually works well. Except that the payer can't generate their details. It pays with details already generated.

Collapse
 
aookunola profile image
Oluwaseun Okunola

Thank you very much

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Oluwaseun, thanks for reading.

Collapse
 
christaiwo profile image
christaiwo

Hello bro, thanks for this post please is there a way to get the transaction details, i mean the email, amount and transaction id?

Collapse
 
ettagideonxx profile image
etta-gideonxx

pls help me out here, whenever i press the html button i get this
"The requested URL was not found on this server."
what does this mean

Collapse
 
joeberetta profile image
Joe Beretta

Very good intro for me. Thank you for this cool post! :)

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Thank you.

Collapse
 
patabah profile image
PatAbah

Is it necessary I purchase SSL for my site when using standard?

Collapse
 
ijsucceed profile image
Jeremy Ikwuje

Sure! Will you want to make a payment on a site that shows "site not secure"?.

As stated in the Paystack Standard Prerequisites:

Confirm that your server can conclude a TLSv1.2 connection to Paystack's servers. Most up-to-date software have this capability. Contact your service provider for guidance if you have any SSL errors.
Don't disable SSL peer verification!

Collapse
 
websaint profile image
Websaint

hi is there a way we can redefine the "var handler" line as wordpress is flagging it. I am trying to edit a plugin to include the paystack button

Collapse
 
ezecodes profile image
Elijah

Followed the procedures as outlined and got the expected result.
But found out that the paystack popui takes time to load. Is there a way I can make it load faster?

Collapse
 
emmyjay profile image
emmy-jay

please it's not redirecting me to the callback.php page, and I specified the url in the initialize.php page can you please help me

Collapse
 
lucignation_olu profile image
Gerald Olumide

How can I send buyer a file they bought immediately and also allow them to download it immediately the payment goes through.

Thanks

Collapse
 
lucignation_olu profile image
Gerald Olumide

That thank you paragraph yyou echo in your sample is not working mine. It stopped when paystack marked that the payment is successful. What could be the problem please?

Collapse
 
maccido profile image
Maccido Shehu

Pls sir i need some help on this topic, my php ecommerce website had product page each product contain different price, pls sir how can i use above code to get payment on each product

Collapse
 
emmyjay profile image
emmy-jay • Edited

I keep getting "Curl returned error: SSL certificate problem: unable to get local issuer certificate" please what do I do. I'm using WAMP Server

Collapse
 
hardsoftmac profile image
HardSoftMac

Can you please help with "Ensure CORS response headers are valid". It's not redirecting to checkout.paystack.com/...

I am using the Standard paystack