A code is a plugin for a website that uses the WooCommerce plugin for e-commerce. It creates a custom payment gateway for PayPal, so that customers can use PayPal to pay for the products or services they purchase on the website.
The plugin includes functions for validating the payment with PayPal, storing the transaction ID, and logging system processes. Additionally, it adds the custom PayPal payment gateway to the WooCommerce payment options so that it appears as an available payment method during the checkout process.
Here is a basic example of how to create a PayPal payment gateway plugin in PHP for Wordpress:
<?php
/**
* Plugin Name: PayPal Payment Gateway for Wordpress
* Plugin URI: http://your-website.com
* Description: A simple PayPal payment gateway for Wordpress.
* Version: 1.0
* Author: Your Name
* Author URI: http://your-website.com
* License: GPL2
*/
add_action( 'plugins_loaded', 'paypal_payment_gateway_init' );
function paypal_payment_gateway_init() {
class PayPal_Payment_Gateway extends WC_Payment_Gateway {
/**
* Constructor for the gateway.
*/
public function __construct() {
$this->id = 'paypal_payment_gateway';
$this->icon = apply_filters( 'woocommerce_paypal_icon', '' );
$this->has_fields = false;
$this->method_title = __( 'PayPal', 'woocommerce' );
$this->method_description = __( 'Pay via PayPal; you can pay with your credit card if you don\'t have a PayPal account.', 'woocommerce' );
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->testmode = 'yes' === $this->get_option( 'testmode' );
$this->debug = 'yes' === $this->get_option( 'debug' );
$this->email = $this->get_option( 'email' );
// Actions
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
// Customer Emails
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
}
/**
* Initialize Gateway Settings Form Fields
*/
public function init_form_fields() {
$this->form_fields = apply_filters( 'woocommerce_paypal_form_fields', array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable PayPal Payment', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'PayPal', 'woocommerce' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
'default' => __( 'Pay via PayPal; you can pay with your credit card if you don\'t have a PayPal account.', 'woocommerce' ),
'desc_tip' => true,
),
'email' => array(
'title' => __( 'PayPal Email', 'woocommerce' ),
'type' => 'email',
'description' => __( 'Please enter your PayPal email address; this is needed in order to take payment.', 'woocommerce' ),
'default' => get_option( 'admin_email' ),
'desc_tip' => true,
'placeholder' => 'you@youremail.com',
),
'testmode' => array(
'title' => __( 'Test mode', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Test Mode', 'woocommerce' ),
'default' => 'yes',
'description' => sprintf( __( 'Place the payment gateway in test mode using test PayPal accounts. Test PayPal accounts can be created <a href="%s">here</a>.', 'woocommerce' ), 'https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/' ),
),
'debug' => array(
'title' => __( 'Debug Log', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable logging', 'woocommerce' ),
'default' => 'no',
'description' => sprintf( __( 'Log PayPal events, such as IPN requests, inside <code>woocommerce/logs/paypal-%s.txt</code>', 'woocommerce' ), sanitize_file_name( wp_hash( $this->id ) ) ),
),
) );
}
/**
* Payment process
*
* @param int $order_id
* @return array
*/
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
// Redirect to PayPal
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);
}
/**
* Output for the order received page.
*/
public function thankyou_page() {
if ( $this->description ) {
echo wpautop( wptexturize( $this->description ) );
}
}
/**
/**
* Check for PayPal IPN Response
*/
public function check_ipn_response() {
@ob_clean();
$order_id = isset( $_GET['order_id'] ) ? absint( $_GET['order_id'] ) : 0;
$order = wc_get_order( $order_id );
if ( $order->get_id() === 0 || $order->get_status() !== 'pending' ) {
wp_die( __( 'Transaction invalid.', 'woocommerce' ), '', array( 'response' => 500 ) );
}
// Get request data
$request = stripslashes_deep( $_REQUEST );
$this->log( 'PayPal IPN Request: ' . print_r( $request, true ) );
// Validate request with PayPal
$response = $this->paypal_validate_request( $request );
$this->log( 'PayPal IPN Response: ' . print_r( $response, true ) );
if ( $response === true ) {
// Payment completed
if ( $request['payment_status'] === 'Completed' ) {
// Check if transaction ID already exists
if ( $this->get_transaction_id( $order_id ) ) {
$this->log( 'Transaction ID already exists. Aborting...' );
wp_die( __( 'Transaction invalid.', 'woocommerce' ), '', array( 'response' => 500 ) );
}
// Update order status
$order->update_status( 'completed', __( 'Payment completed by PayPal', 'woocommerce' ) );
// Store transaction ID
$this->store_transaction_id( $order_id, $request['txn_id'] );
// Reduce stock levels
wc_reduce_stock_levels( $order_id );
// Remove cart
WC()->cart->empty_cart();
// Return thank you page redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
}
wp_die( __( 'Transaction invalid.', 'woocommerce' ), '', array( 'response' => 500 ) );
}
/**
* Check if request is a valid PayPal IPN
*
* @param array $request
* @return bool
*/
private function paypal_validate_request( $request ) {
if ( ! isset( $request['txn_id'] ) || ! isset( $request['receiver_email'] ) || ! isset( $request['mc_gross'] ) ) {
return false;
}
// Get PayPal settings
$email = $this->get_option( 'email' );
$testmode = $this->get_option( 'testmode' ) === 'yes' ? true : false;
// Check if testmode is enabled
if ( $testmode ) {
$paypal_adr = $this->testurl;
} else {
$paypal_adr = $this->liveurl;
}
// Send request to PayPal for validation
$params = array(
'cmd' => '_notify-validate',
'body' => http_build_query( $request ),
'sslverify' => false,
'timeout' => 60,
'httpversion' => '1.1',
'user-agent' => 'WooCommerce/' . WC()->version
);
$response = wp_remote_post( $paypal_adr, $params );
if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) {
if ( $request['receiver_email'] == $email ) {
return true;
} else {
$this->log( 'Receiver email does not match. Response: ' . print_r( $response['body'], true ) );
}
} else {
$this->log( 'PayPal IPN Request failure. Response: ' . print_r( $response, true ) );
}
return false;
}
/**
* Get transaction ID
*
* @param int $order_id
* @return bool|string
*/
private function get_transaction_id( $order_id ) {
return get_post_meta( $order_id, '_transaction_id', true );
}
/**
* Store transaction ID
*
* @param int $order_id
* @param string $transaction_id
*/
private function store_transaction_id( $order_id, $transaction_id ) {
update_post_meta( $order_id, '_transaction_id', $transaction_id );
}
/**
* Log system processes
*
* @param string $message
*/
private function log( $message ) {
if ( $this->debug ) {
$log = new WC_Logger();
$log->add( 'paypal', $message );
}
}
}
}
// Add custom payment gateway to WooCommerce
function wc_add_paypal_gateway( $methods ) {
$methods[] = 'WC_Paypal_Gateway';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'wc_add_paypal_gateway' );
endif;
This code will create a basic PayPal payment gateway for WooCommerce that can be added to the checkout page of your Wordpress website. You will need to customize the code according to your needs, but it should serve as a good starting point.