How to Integrate PayStack into Your HTML & CSS Website: A Step-by-Step Guide

Introduction: Integrating a reliable payment platform into your website is essential if you want to offer a seamless and secure payment experience to your customers. PayStack is a popular payment platform that provides easy integration for websites, making it a top choice for many businesses. In this step-by-step guide, we will walk you through the process of integrating PayStack into your website, allowing you to accept payments smoothly and efficiently.

***** You can also check out this tutorial video on youtube to follow along: https://www.youtube.com/watch?v=oc5aAZDjKVI

Prerequisites: Before we begin, ensure you have the following:

  1. A PayStack account (You can sign up for a free account if you don't have one).

  2. Basic knowledge of HTML, CSS, and JavaScript.

  3. A code editor (such as Visual Studio Code) is installed on your computer.

Step 1: Setting up the Project

Clone the starter code:

  1. Open the project in your preferred code editor.

  2. Familiarize yourself with the project structure, which includes an HTML file and a CSS file.

Step 2: Styling the Form

  1. Open the CSS file and customize the styles according to your website's design.

  2. Save the CSS file and link it to the HTML file using the appropriate "link" tag.

Step 3: Obtaining PayStack API Keys

  1. Log in to your PayStack account.

  2. If you don't have a business set up, create a new business and configure it for test mode.

  3. Access the business settings and obtain the test API key provided by PayStack.

Step 4: Integrating PayStack API into Your Website

  1. Create a new folder named "JS" in your project directory.

  2. Inside the "JS" folder, create a new file named "payment.js".

  3. Visit the PayStack API documentation and find the JavaScript integration code.

     const paymentForm = document.getElementById('paymentForm');
     2 paymentForm.addEventListener("submit", payWithPaystack, false);
     3 function payWithPaystack(e) {
     4  e.preventDefault();
     5
     6  let handler = PaystackPop.setup({
     7    key: 'pk_test_xxxxxxxxxx', // Replace with your public key
     8    email: document.getElementById("email-address").value,
     9    amount: document.getElementById("amount").value * 100,
     10   ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
     11    // label: "Optional string that replaces customer email"
     12    onClose: function(){
     13      alert('Window closed.');
     14    },
     15    callback: function(response){
     16      let message = 'Payment complete! Reference: ' + response.reference;
     17      alert(message);
     18    }
     19  });
     20
     21  handler.openIframe();
     22}
    
  4. Copy the provided code and paste it into the "payment.js" file.

  5. Replace the placeholder API key in the code with your test API key obtained from PayStack.

  6. Save the "payment.js" file.

Step 5: Linking the JavaScript File

  1. Open the HTML file.

  2. Add a script tag within the HTML body and set the source attribute to "JS/payment.js".

  3. Save the HTML file.

Step 6: Testing the Payment Integration

  1. Start a local development server (e.g., Live Server) to view your website.

  2. Fill in the required details (email, amount, etc.) in the form.

  3. Click the "Pay" button.

  4. If everything is set up correctly, you should see the test payment process being simulated.

  5. After the payment process completes, you'll receive a "Payment Successful" message along with a reference ID.

Conclusion

Congratulations! You have successfully integrated the PayStack payment platform into your website. By following this step-by-step guide, you can now offer your customers a secure and hassle-free payment experience. Remember, if you want to switch from test mode to live mode, you'll need to provide additional details and follow the necessary steps to legitimize your business.

I hope this guide has helped assist you with the PayStack integration. If you have any questions or encounter any issues along the way, feel free to reach out to PayStack's support team for further assistance. Happy integrating!

0
Subscribe to my newsletter

Read articles from Sherigabia Pambo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sherigabia Pambo
Sherigabia Pambo

Human 🙋| Dagbana 🦁| Son😊| Brother 😏|Coolest Uncle Ever😎| Coffee lover❤️| Software Dev💻| Gamer🎮