PayPal Express Checkout with React JS and PHP RESTful
PayPal Express Checkout with React JS and PHP RESTful
There are several payment options available in the market today. PayPal checkout option is one among them. In my previous articles, I have already discussed few payment options - Braintree PayPal using PHP, Payment system and PayPal Express Checkout with PHP and MySQL. These are the most viewed and trending articles till this day. Today, let’s see how the PayPal Express Checkout works with React JS.
Database Design
To build the PayPal checkout ordering system, you have to create the following tables. Social login user table has been present in the previous article.
Products
Product table contains all the product details. You can extend this table based on your needs.
Orders
Order table contains all the user order details with PayPal payment ids.
Live demo sandbox PayPal credentials for testing username: demos@9lessons.info password: 9lessons
Live Demo
Orders
Order table contains all the user order details with PayPal payment ids.
CREATE TABLE
orders(
oid int AUTO_INCREMENT PRIMARY KEY,
pid_fk int,
uid_fk int,
payerID varchar(500),
paymentID varchar(500),
token varchar(500),
created int(11)
);
Home.js
Let continue the provious article. Here the user session storage data is present, storing the data with setState. Using user id and token you can access other informaiton like products and order details.
import React, {Component} from 'react';
import './Home.css';
import {Redirect} from 'react-router-dom';
class Home extends Component {
constructor(props){
super(props);
this.state = {
name:'',
redirect: false,
};
}
componentDidMount() {
let data = JSON.parse(sessionStorage.getItem('userData'));
console.log(data);
this.setState({name: data.userData.name})
}
render() {
if(!sessionStorage.getItem('userData') || this.state.redirect){
return (<Redirect to={'/'}/>)
}
return (
<div >
Welcome {this.state.name}
</div>
);
}
}
export default Home;
Lets start
You have to create common components for multiple uses. In this project, we are commonly using project title and log out.
Title.js
This component contains project Title and log out functionality. Using the props you can disturbute the data. You can import this component in multiple pages.
import React, { Component } from "react";
import { Redirect } from "react-router-dom";
import "./Title.css";
class Title extends Component {
constructor(props) {
super(props);
this.logout = this.logout.bind(this);
this.state = {
redirect: false
};
}
logout() {
sessionStorage.setItem("userData", "");
sessionStorage.clear();
this.setState({ redirect: true });
}
render() {
if (this.state.redirect) {
return <Redirect to={"/"} />;
}
return (
<div className="row">
<div className="medium-12 columns">
<ul className="right">
<li>
<a href="/orders">Orders</a>
</li>
<li>
<a href="#" onClick={this.logout}>
Logout
</a>
</li>
</ul>
<h2>Welcome {this.props.name}</h2>
</div>
</div>
);
}
}
export default Title;
ProductsList.js
Component for displaying all of the product details. Here the product data is calling from React props constuctor. onClick button this.props.checkOut points to parent function, you will find this in next step.
import React, { Component } from "react";
import "./ProductsList.css";
class ProductsList extends Component {
constructor(props) {
super(props);
}
render() {
let productList = this.props.productsData.map(function(productData, index) {
return (
<div className="row list" key={index}>
<div className="medium-3 columns">
<img
src={
"https://demos.9lessons.info/PHP-PayPal-ExpressCheckout/img/" +
productData.product_img
}
/>
</div>
<div className="medium-3 columns">{productData.product}</div>
<div className="medium-3 columns">$ {productData.price}</div>
<div className="medium-3 columns">
<button className="button" value={productData.pid}
onClick={this.props.checkout}>
Order
</button>
</div>
</div>
);
}, this);
return <div>{productList}</div>;
}
}
export default ProductsList;
Importing Components and Data Binding with RESTful APIs
Home.js
Imported Title and ProductList components with PostData URL fetch provider. Fun


Comments
Post a Comment