Skip to content

Commit af394dd

Browse files
committed
Integrate Stripe Payment Gateway using PHP
0 parents  commit af394dd

15 files changed

+938
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Instructions
2+
Learn how to Integrate Stripe Payment Gateway using PHP
3+
1. Download Stripe Payment Gateway Integration using PHP
4+
2. Import Stripe PHP SDK using composer
5+
3. Create Table in DB
6+
4. Create your account on Stripe and get Stripe API and Publishable Keys
7+
5. Update them in config.php
8+
6. Test the application
9+
10+
All step by step guide and information is available on the [tutorial link](https://www.allphptricks.com/integrate-stripe-payment-gateway-using-php/).

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"stripe/stripe-php": "^10.11"
4+
}
5+
}

composer.lock

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/*
3+
Author: Javed Ur Rehman
4+
Website: https://www.allphptricks.com
5+
*/
6+
//Stripe Credentials Configuration
7+
define("STRIPE_SECRET_API_KEY", "YOUR STRIPE SECRET KEY");
8+
define("STRIPE_PUBLISHABLE_KEY", "YOUR STRIPE PUBLISHABLE KEY");
9+
10+
//Sample Product Details
11+
define('CURRENCY', 'USD');
12+
define('AMOUNT', '10');
13+
define('DESCRIPTION', 'Laptop Bag');
14+
15+
// Database Credentials Configuration
16+
define('DB_HOST', 'localhost');
17+
define('DB_NAME', 'Your Database Name');
18+
define('DB_USERNAME', 'Your Database Username');
19+
define('DB_PASSWORD', 'Your Database Password');
20+
?>

create_customer.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/*
3+
Author: Javed Ur Rehman
4+
Website: https://www.allphptricks.com
5+
*/
6+
require_once 'stripe_header.php';
7+
8+
$payment_intent_id = !empty($jsonObj->payment_intent_id)?$jsonObj->payment_intent_id:'';
9+
$fullname = !empty($jsonObj->fullname)?$jsonObj->fullname:'';
10+
$email = !empty($jsonObj->email)?$jsonObj->email:'';
11+
12+
// Add new customer fullname and email to stripe
13+
try {
14+
$customer = \Stripe\Customer::create(array(
15+
'name' => $fullname,
16+
'email' => $email
17+
));
18+
}catch(Exception $e) {
19+
$error = $e->getMessage();
20+
}
21+
22+
if(empty($error) && !empty($customer)){
23+
try {
24+
// Attach Customer Data with PaymentIntent using customer ID
25+
\Stripe\PaymentIntent::update($payment_intent_id, [
26+
'customer' => $customer->id
27+
]);
28+
} catch (Exception $e) {
29+
$error = $e->getMessage();
30+
}
31+
$output = [
32+
'customer_id' => $customer->id
33+
];
34+
echo json_encode($output);
35+
36+
}else{
37+
http_response_code(500);
38+
echo json_encode(['error' => $error]);
39+
}
40+
?>

create_payment_intent.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/*
3+
Author: Javed Ur Rehman
4+
Website: https://www.allphptricks.com
5+
*/
6+
require_once 'stripe_header.php';
7+
8+
// Define the product item price and convert it to cents
9+
$product_price = round(AMOUNT*100);
10+
11+
try {
12+
// Create PaymentIntent with amount, currency and description
13+
$paymentIntent = \Stripe\PaymentIntent::create([
14+
'amount' => $product_price,
15+
'currency' => CURRENCY,
16+
'description' => DESCRIPTION,
17+
'payment_method_types' => [
18+
'card'
19+
]
20+
]);
21+
22+
$output = [
23+
'paymentIntentId' => $paymentIntent->id,
24+
'clientSecret' => $paymentIntent->client_secret
25+
];
26+
27+
echo json_encode($output);
28+
} catch (Error $e) {
29+
http_response_code(500);
30+
echo json_encode(['error' => $e->getMessage()]);
31+
}
32+
?>

css/style.css

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
line-height: 1.6;
4+
}
5+
.pay {
6+
text-transform: uppercase;
7+
background: #F68B1E;
8+
border: 1px solid #F68B1E;
9+
cursor: pointer;
10+
color: #fff;
11+
padding: 8px 40px;
12+
margin-top: 10px;
13+
}
14+
.pay:hover {
15+
background: #f17e0a;
16+
border-color: #f17e0a;
17+
}
18+
.form-control {
19+
display: block;
20+
padding: 0.375rem 0.75rem;
21+
font-size: 1rem;
22+
font-weight: 400;
23+
line-height: 1.5;
24+
color: #212529;
25+
border: 1px solid #ced4da;
26+
border-radius: 0.375rem;
27+
margin-bottom: 10px;
28+
29+
}
30+
#stripe-payment-message{
31+
padding: 20px;
32+
border-radius: 5px;
33+
padding-left: 30px;
34+
margin-bottom: 10px;
35+
color: #842029;
36+
background-color: #f8d7da;
37+
border: 1px solid #f5c2c7;
38+
}
39+
.btn-primary {
40+
font-weight: 600;
41+
color: rgb(255, 255, 255);
42+
text-align: center;
43+
background-color: rgb(0, 103, 171);
44+
border: 0 !important;
45+
border-radius: 6px !important;
46+
padding: 10px;
47+
cursor: pointer;
48+
}
49+
.hidden{
50+
display:none;
51+
}
52+
/* spinner/processing state, errors */
53+
.spinner,
54+
.spinner:before,
55+
.spinner:after {
56+
border-radius: 50%;
57+
}
58+
.spinner {
59+
color: #ffffff;
60+
font-size: 22px;
61+
text-indent: -99999px;
62+
margin: 0px auto;
63+
position: relative;
64+
width: 20px;
65+
height: 20px;
66+
box-shadow: inset 0 0 0 2px;
67+
-webkit-transform: translateZ(0);
68+
-ms-transform: translateZ(0);
69+
transform: translateZ(0);
70+
}
71+
.spinner:before,
72+
.spinner:after {
73+
position: absolute;
74+
content: "";
75+
}
76+
.spinner:before {
77+
width: 10.4px;
78+
height: 20.4px;
79+
background: #F68B1E;
80+
border-radius: 20.4px 0 0 20.4px;
81+
top: -0.2px;
82+
left: -0.2px;
83+
-webkit-transform-origin: 10.4px 10.2px;
84+
transform-origin: 10.4px 10.2px;
85+
-webkit-animation: loading 2s infinite ease 1.5s;
86+
animation: loading 2s infinite ease 1.5s;
87+
}
88+
.spinner:after {
89+
width: 10.4px;
90+
height: 10.2px;
91+
background: #F68B1E;
92+
border-radius: 0 10.2px 10.2px 0;
93+
top: -0.1px;
94+
left: 10.2px;
95+
-webkit-transform-origin: 0px 10.2px;
96+
transform-origin: 0px 10.2px;
97+
-webkit-animation: loading 2s infinite ease;
98+
animation: loading 2s infinite ease;
99+
}
100+
101+
@-webkit-keyframes loading {
102+
0% {
103+
-webkit-transform: rotate(0deg);
104+
transform: rotate(0deg);
105+
}
106+
100% {
107+
-webkit-transform: rotate(360deg);
108+
transform: rotate(360deg);
109+
}
110+
}
111+
@keyframes loading {
112+
0% {
113+
-webkit-transform: rotate(0deg);
114+
transform: rotate(0deg);
115+
}
116+
100% {
117+
-webkit-transform: rotate(360deg);
118+
transform: rotate(360deg);
119+
}
120+
}
121+
122+
@media only screen and (max-width: 600px) {
123+
form {
124+
width: 80vw;
125+
min-width: initial;
126+
}
127+
}
128+
.loader {
129+
display: block;
130+
margin: 20px;
131+
border: 14px solid #f3f3f3;
132+
border-radius: 50%;
133+
border-top: 14px solid #3498db;
134+
width: 80px;
135+
height: 80px;
136+
-webkit-animation: spin 2s linear infinite; /* Safari */
137+
animation: spin 2s linear infinite;
138+
}
139+
/* Safari */
140+
@-webkit-keyframes spin {
141+
0% { -webkit-transform: rotate(0deg); }
142+
100% { -webkit-transform: rotate(360deg); }
143+
}
144+
@keyframes spin {
145+
0% { transform: rotate(0deg); }
146+
100% { transform: rotate(360deg); }
147+
}
148+
#payment_processing{
149+
color: #078e29;
150+
font-weight: bold;
151+
}

0 commit comments

Comments
 (0)