How to Integrate PayPal Gateway to a website using PHP

  • Language: PHP
  • Files: 0

Description

How to Integrate PayPal Gateway to a website using PHP with Source Code | PayPal Payments with PHP

Code Files Preview

.js
paypal
    .Buttons({
        createOrder: function (data, actions) {
            // Set the amount dynamically
            //const amount = $('#total_amount').val(); // Replace with your desired amount
            return actions.order.create({
                purchase_units: [
                    {
                        amount: {
                            value: 5.00, // Ensure 2 decimal places
                            currency_code: 'USD', // Change currency as needed
                        },
                    },
                ],
            });
        },
        onApprove: function (data, actions) {
            // Handle when the payment is approved
            return actions.order.capture().then(function (details) {
                // Show a success message to the user
paypal
    .Buttons({
        createOrder: function (data, actions) {
            // Set the amount dynamically
            //const amount = $('#total_amount').val(); // Replace with your desired amount
            return actions.order.create({
                purchase_units: [
                    {
                        amount: {
                            value: 5.00, // Ensure 2 decimal places
                            currency_code: 'USD', // Change currency as needed
                        },
                    },
                ],
            });
        },
        onApprove: function (data, actions) {
            // Handle when the payment is approved
            return actions.order.capture().then(function (details) {
                // Show a success message to the user
                const transaction = details.purchase_units[0].payments.captures[0];
                //alert(transaction.status + ' => ' + transaction.id)
                //How to insert the response into the database using PHP
                var user_id = 5; //can be dynamic incase of user sessions

                $.ajax({
                    method: "POST",
                    url: "paypal_processor.php",
                    data: {transaction_id: transaction.id, user: user_id, transaction_status: transaction.status},
                    success: function(response) {
                        if(response == 1) {
                            alert("Payment Successfull. Transaction ID is " + transaction.id)
                        } else {
                            alert('Failed to process payment')
                            console.log(response)
                        }
                    }
                })

                //alert('Transaction completed by ' + details.payer.name.given_name + '. Payment successful');
            });
        },
        onError: function (error) {
            // Handle errors and display an error message to the user
            console.log(error);
            alert(error + 'An error occurred while processing the payment. Please try again later.');
        },
    })
    .render('#paypal-button-container'); 
// Render the PayPal button in the specified container
.php
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<div class="center-wrapper">
	<div class="content">
		<nav>
			<a href="#" class="menu">Menu</a>
			<h1 class="logo">CanonCode</h1>
			<div class="icons">
				<i class="fas fa-search"></i>
				<i class="fas fa-shopping-bag"></i><span style="margin-left: 0.3rem;">1</span>
			</div>
		</nav>
		<div class="top-bar">
			<i class="fas fa-arrow-left"></i>
			<span>Continue shopping</span>
		</div>
		<div class="bag">
			<p class="bag-head"><span style="text-transform: uppercase">Your Bag</span> - 1 item</p>
		</div>
		<div class="bag-product">
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<div class="center-wrapper">
	<div class="content">
		<nav>
			<a href="#" class="menu">Menu</a>
			<h1 class="logo">CanonCode</h1>
			<div class="icons">
				<i class="fas fa-search"></i>
				<i class="fas fa-shopping-bag"></i><span style="margin-left: 0.3rem;">1</span>
			</div>
		</nav>
		<div class="top-bar">
			<i class="fas fa-arrow-left"></i>
			<span>Continue shopping</span>
		</div>
		<div class="bag">
			<p class="bag-head"><span style="text-transform: uppercase">Your Bag</span> - 1 item</p>
		</div>
		<div class="bag-product">
			<div class="image">
				<img src="1616499931_iph12pm.jpg" class="product-image">
			</div>
			<div class="description">
				<p class="product-code small muted">Product code: SS022592000</p>
				<h1>iPhone 12 Pro Max</h1>
				<p>Sun</p>
				<p class="description-text">iPhone 12 Pro Max 256GB......</p>
				<p style="margin-bottom: 0.5rem;">Blue</p>
				<h2>$825.00</h2>
				<input type="text" name="amount" id="amount" value="<?= 200 ?>">
				<div class="quantity-wrapper">
					<div>
						<label for="quantity" style="margin-right: 0.5rem;">Quantity:</label>
						<select name="quantity" style="margin-bottom: 1rem;">
							<option value disabled>Please select</option>
							<option value="1" selected>1</option>
							<option value="2">2</option>
							<option value="3">3</option>
						</select>
					</div>
					<button class="btn-remove">Remove</button>
				</div>
			</div>
		</div>
		<div class="bag-total">
			<div class="subtotal">
				<p class="small">Subtotal:</p>
				<p class="small">$825.00</p>
			</div>
			<div class="delivery">
				<p class="small">Delivery (Standard - 2 working days):<br>
					<span class="change-delivery">Change delivery method</span>
				</p>
				<p class="small">Free</p>
			</div>
			<div class="total">
				<h3>Total:</h3>
				<h3>$825.00</h3>
			</div>
            <form action="" method="post">
                <input type="text" name>
                <div class="promo-checkbox">
                    <input type="checkbox" name="promo-check" checked>
                    <label for="promo-check">I have a promo code</label>
                </div>
                <div class="promo-code">
                    <input type="text" name="promo-checkbox" placeholder="Enter your promo code here" checked>
                    <button class="apply">Apply</button>
                </div>
                <button class="btn-go-checkout">
                    <i class="fas fa-lock"></i>
                    <span>Go to Checkout</span>
                </button>
				<br>
                <div id="paypal-button-container"></div>
            </form>
			
		</div>
		<div class="help">
			<p>Need help? Call Canon Code via +254 741 575274</p>
		</div>
	</div>
</div>
<div class="bg"></div>

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
 <!-- Replace the "test" client-id value with your client-id -->
<script src="https://www.paypal.com/sdk/js?client-id=AZWFxhHf9ZJv9ApOxUTdtWWqzirAIPeXLa3BnBNyt8C94GYEoYj1H-3ohB2ARRIKrVSseVm91cD6Qb8o&currency=USD"></script>
<script src="app.js"></script>
 
.php
<?php

$user = $_POST['user'];
$transaction_id = $_POST['transaction_id'];
$transaction_status = $_POST['transaction_status'];


$sql = $connection -> query("INSERT INTO table_name() VALUES()");
.css
@import url("https://fonts.googleapis.com/css?family=Lato:400,700|Space+Mono:700");

* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}


html,
body {
	height: 100%;
	font-size: 16px;
	font-weight: 400;
}

body { padding: 0.5rem !important; }

html,
body,
@import url("https://fonts.googleapis.com/css?family=Lato:400,700|Space+Mono:700");

* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}


html,
body {
	height: 100%;
	font-size: 16px;
	font-weight: 400;
}

body { padding: 0.5rem !important; }

html,
body,
button,
input[type="text"] {
	font-family: font-txt;
}

.center-wrapper {
	padding: 0.5rem;
	display: -webkit-box;
	display: -ms-flexbox;
	display: flex;
	-webkit-box-orient: vertical;
	-webkit-box-direction: normal;
	-ms-flex-direction: column;
	flex-direction: column;
	-webkit-box-pack: center;
	-ms-flex-pack: center;
	justify-content: center;
	min-height: 100%;
}

.content {
	margin: 0 auto;
	max-width: 600px;
	border: 1px solid yellow;
	background: white;
}

nav,
.top-bar,
.bag,
.bag-total,
.help {
	padding: 0.5rem 1rem;
}

nav,
a,
.btn-go-checkout {
	color: white;
}

nav {
	display: -webkit-box;
	display: -ms-flexbox;
	display: flex;
	-webkit-box-orient: horizontal;
	-webkit-box-direction: normal;
	-ms-flex-flow: row nowrap;
	flex-flow: row nowrap;
	-webkit-box-pack: justify;
	-ms-flex-pack: justify;
	justify-content: space-between;
	-webkit-box-align: center;
	-ms-flex-align: center;
	align-items: center;
	background: blue;
}

a {
	padding: 0.2rem 0.5rem;
	border: 2px solid white;
	text-decoration: none;
}

.logo {
	font-family: font-head;
}

.logo,
button {
	text-transform: uppercase;
}

.fa-search,
.fa-arrow-left,
.fa-lock {
	margin-right: 1rem;
}

.top-bar,
.bag-head::after,
.bag-total::before {
	background: whitesmoke;
}

.bag-head::after,
.bag-total::before,
.btn-remove {
	display: block;
}

.bag-head::after,
.bag-total::before,
.description-text,
.promo-checkbox {
	margin: 0.5rem 0;
}

.bag-head::after,
.bag-total::before {
	content: "";
	width: 100%;
	height: 3px;
}

.muted {
	color: grey;
}

.change-delivery,
::placeholder,
.product-code,
.help {
	font-size: small;
}

h1 {
	font-size: 1.6rem;
}

h2 {
	font-size: 1.4rem;
}

.image {
	width: 40%;
	img.product-image {
		max-width: 100%;
	}
}

.description {
	padding-right: 1rem;
	width: 60%;
}

select {
	padding: 0.3rem;
	width: 60px;
}

select,
button,
input[type="text"] {
	height: 40px;
}

button {
	cursor: pointer;
	width: 100px;
	background: none;
	border: 2px solid violet;
	border-radius: 4px;
}

.quantity-wrapper {
	-webkit-box-align: start;
	-ms-flex-align: start;
	align-items: flex-start;
	-webkit-box-orient: horizontal;
	-webkit-box-direction: normal;
	-ms-flex-flow: row wrap;
	flex-flow: row wrap;
	margin: 1rem 0 0.5rem;
}

select {
	width: 50px;
	margin-right: 1rem;
}

.bag-product,
.quantity-wrapper,
.subtotal,
.delivery,
.total,
.promo-code {
	display: -webkit-box;
	display: -ms-flexbox;
	display: flex;
	-webkit-box-pack: justify;
	-ms-flex-pack: justify;
	justify-content: space-between;
}

.subtotal,
.delivery,
input[type="checkbox"],
.help {
	margin-bottom: 0.5rem;
}

.total {
	margin-bottom: 1rem;
}

button,
.change-delivery {
	color: violet;
}

input[type="text"],
.btn-go-checkout {
	font-size: 1rem;
}

input[type="text"] {
	width: calc(100% - 100px - 1rem);
	padding: 0.5rem;
}

.btn-go-checkout {
	margin-top: 1rem;
	width: 100%;
	height: 40px;
	background: violet;
	box-shadow: 0 3px 6px 2px darken(whitesmoke, 10%);
}

.help {
	text-align: center;
}

html {
	background: repeating-linear-gradient(
		-45deg,
		yellow,
		yellow 10px,
		darken(yellow, 10%) 10px,
		darken(yellow, 10%) 20px
	);
}
Veen Africa
Veen Africa

Stay Connected With
Cutting Edge IT