Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: EDD PayFast Payment Gateway
- Plugin URI: http://www.mesaneweb,com
- Description: Accept payments through PayFast for your Digital Store powered by Easy Digital Downloads, a payment gateway for South Africans.
- Version: 1.0.1
- Author: Dane A. Mesane
- Author URI: http://www.mesaneweb.com
- License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
- */
- /* Copyright 2014 Dane A. Mesane (www.mesaneweb.com) ( email : support@mesaneweb.com )
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License, version 2, as
- published by the Free Software Foundation.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- //check the existensi of our class and Easy_Digital_Downloads,
- if( ! class_exists( 'EDD_Payfast' ) ) :
- class EDD_Payfast {
- /**
- * @var instance The one true EDD_Payfast class
- * singleton
- */
- private static $instance;
- // Our file of this plugin
- public $file;
- // Our plugin path
- public $plugin_path;
- // Our plugin url
- public $plugin_url;
- // the version of this plugin
- public $version;
- /**
- * Main EDD_Payfast Instance
- * singleton implementation
- */
- public static function instance() {
- if ( ! isset( self::$instance ) ) {
- self::$instance = new EDD_Payfast(__FILE__);
- }
- return self::$instance;
- }
- /**
- * Constructor
- * @since 1.0.0
- * @return void
- */
- private function __construct( $file ) {
- //set up our data
- $this->version = '1.0.0';
- $this->file = $file;
- $this->plugin_url = trailingslashit( plugins_url( '', $plugin = $file ) );
- $this->plugin_path = trailingslashit( dirname( $file ) );
- if ( ! function_exists( 'json_decode' ) ) {
- if ( is_admin() )
- add_action( 'admin_notices', array( &$this, 'initialization_warning' ) );
- return;
- }
- //hooks for admin
- if( is_admin() )
- add_filter( 'edd_settings_gateways', array( &$this, 'add_settings_gateways' ) );
- // hooks
- add_filter( 'edd_currencies', array( &$this, 'rand_currencies' )); //add South African currency, R or ZAR
- add_filter( 'edd_accepted_payment_icons', array( &$this, 'payfast_payment_icon' ) );
- add_filter( 'edd_payment_gateways', array( &$this, 'register_gateway' ) );
- add_action( 'edd_payfast_cc_form', array( &$this, 'gateway_cc_form' ) );
- add_action( 'edd_gateway_payfast', array( &$this, 'process_payment' ) );
- add_action( 'init', array( &$this, 'validate_report_back' ) ); // trying to get notify from payfast
- add_action( 'edd_payfast_check', array( &$this, 'process_payfast_notify' ) );
- } //end __construct
- /**
- * Yeah, Because we need json_docode() function to retrieve session ID from Payfast
- * So if the function don't exists we should throw a warning
- *
- * @since 1.0
- */
- public function initialization_warning() {
- echo '<div id="edd-payfast-warning" class="updated fade"><p><strong>' . sprintf( __( '%s PHP library not installed.', 'edd-payfast' ), 'JSON' ) . '</strong> ';
- echo sprintf( __('EDD Payfast Payment Gateway plugin will not function without <a href="%s">PHP JSON functions</a> enabled. Please update your version of WordPress for improved compatibility and/or enable native JSON support for PHP.'), 'http://php.net/manual/book.json.php' );
- echo '</p></div>';
- }
- /* in order to disable credit card form that registered by Easy Digital Downloads by default. I return to blank value
- * on other words, we just registered that. we don't need that for our gateway..
- */
- public static function gateway_cc_form() {
- return;
- } //end gateway_cc_form()
- /*
- * add our icon on checkout page
- *
- * @return void
- * @access public
- *
- */
- public function payfast_payment_icon( $icons ) {
- $icons[$this->plugin_url . 'assets/images/PayFast.png'] = 'Payfast';
- return $icons;
- } //end payfast_payment_icon()
- /*
- * add our currency, sadly South African Rand ( R ) not support on the core of Easy Digital Downloads
- * but the plugin allow to filter that function
- *
- * I don't know why other people called our currency is ZAR, but we familiar with R as our currency
- * because of that, I set both here...
- */
- function rand_currencies( $currencies ) {
- $currencies['R'] = __('South African Rand ( R )', 'edd_payfast');
- return $currencies;
- } //end rand_currencies()
- // Remote get and retrieve respon body..
- private function remote_get( $url, $headers = array() ) {
- $response = wp_remote_get( $url,
- array(
- 'redirection' => 1,
- 'httpversion' => '1.1',
- 'user-agent' => 'EDD Payfast' . $this->version . '; WordPress (' . home_url( '/' ) . ')',
- 'timeout' => 15,
- 'headers' => $headers
- )
- );
- if ( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
- return $response['body'];
- } else {
- return false;
- }
- } //end remote_get()
- // Lets register our gateway, we can use $gateways object because this run during hooks
- public function register_gateway( $gateways ) {
- $gateways['payfast'] = array( 'admin_label' => __( 'Payfast', 'edd_payfast' ), 'checkout_label' => __('Payfast', 'edd_payfast'));
- return $gateways;
- }
- /*
- * Easy Digital Downloads have settings method that allow other developer filter that to add additional settings
- * So because this plugin is an extension/add on for EDD, we don't need create settings from scratch or create
- * tradional setting on Wordpress, beautifull..
- *
- * @access public
- * @return void
- * @since 0.0.1
- */
- public function add_settings_gateways( $settings ) {
- $edd_payfast_settings = array(
- array(
- 'id' => '_edd_payfast_gateway_settings',
- 'name' => '<strong>' . __('Payfast Gateway Settings', 'edd_payfast') . '</strong>',
- 'desc' => __('Configure the gateway settings', 'edd_payfast'),
- 'type' => 'header'
- ),
- array(
- 'id' => 'edd_payfast_merchant_id',
- 'name' => __('Merchant ID', 'pw_edd'),
- 'desc' => __('Enter your Payfast Merchant ID, you can finf it under settings in your PayFast account. Please click <a https://www.payfast.co.za/acc/integration>here</a>.', 'edd_payfast'),
- 'type' => 'text',
- 'size' => 'regular'
- ),
- array(
- 'id' => 'edd_payfast_merchant_key',
- 'name' => __('Merchant Key', 'pw_edd'),
- 'desc' => __('Enter your Payfast Merchant Key, you can finf it under settings in your PayFast account. Please click <a https://www.payfast.co.za/acc/integration >here</a>.', 'edd_payfast'),
- 'type' => 'text',
- 'size' => 'regular'
- )
- );
- return array_merge( $settings, $edd_payfast_settings );
- } //end add_settings_gateways()
- public function process_payment( $purchase_data ) {
- global $edd_options;
- // Check there is a gateway name
- if ( ! isset( $purchase_data['post_data']['edd-gateway'] ) )
- return;
- $errors = edd_get_errors();
- if( !$errors ) {
- $payment_data = array(
- 'price' => $purchase_data['price'],
- 'date' => $purchase_data['date'],
- 'user_email' => $purchase_data['user_email'],
- 'purchase_key' => $purchase_data['purchase_key'],
- 'currency' => $edd_options['currency'],
- 'downloads' => $purchase_data['downloads'],
- 'user_info' => $purchase_data['user_info'],
- 'cart_details' => $purchase_data['cart_details'],
- 'status' => 'pending'
- );
- // record the pending payment
- $payment = edd_insert_payment( $payment_data );
- if ( ! $payment ) {
- // Record the error
- edd_record_gateway_error( __( 'Payment Error', 'edd_payfast' ), sprintf( __( 'Payment creation failed before sending buyer to Payfast. Payment data: %s', 'edd_payfast' ), json_encode( $payment_data ) ), $payment );
- // Problems? send back
- edd_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['edd-gateway'] );
- } else {
- $return_url = add_query_arg( 'payment-confirmation', 'payfast', get_permalink( $edd_options['success_page'] ) );
- $listener_url = trailingslashit( home_url() ).'?payfast=notify';
- $cancel_url = add_query_arg( 'payment-cancel', 'payfast', edd_get_failed_transaction_uri() );
- $summary = edd_get_purchase_summary( $purchase_data, false );
- $quantity = edd_get_cart_quantity();
- function edd_get_payfast_redirect( $ssl_check = false ) {
- global $edd_options;
- if ( is_ssl() || ! $ssl_check) {
- $protocal = 'https://';
- } else {
- $protocal = 'http://';
- }
- if( edd_is_test_mode() ) {
- $url_to_send = $protocal . 'http://sandbox.payfast.co.za/eng/process';
- } else {
- $url_to_send = $protocal . 'http://www.payfast.co.za/eng/process';
- }
- $payfast_args = array(
- 'key' => $edd_options['edd_payfast_api_key'], // API Key Merchant / Penjual
- 'action' => 'payment',
- 'product' => stripslashes_deep( html_entity_decode( wp_strip_all_tags( $summary ), ENT_COMPAT, 'UTF-8' ) ),
- 'price' => round( $purchase_data['price'] - $purchase_data['tax'], 2 ), //
- 'quantity' => $quantity,
- 'comments' => $payment, // Optional for Payfast, but this is the payment ID, we need it to verify payment
- 'ureturn' => $return_url,
- 'unotify' => $listener_url,
- 'ucancel' => $cancel_url, //if cancel back to check out
- 'format' => 'json' // Format: xml / json. Default: xml
- );
- //var_dump( add_query_arg( $payfast_args, $url_to_send ) ); exit;
- $url_to_send .= http_build_query( $payfast_args );
- //remote get and retrieve a session id from payfast
- $response = $this->remote_get( $url_to_send );
- if ( $response == null || $response == 'null' )
- return false;
- $payfast = json_decode( $response );
- //get the session from payfast, we are ready to send the buyer
- $session_id = $payfast->sessionID;
- //save to database first, we will need it
- add_post_meta( $payment, '_payfast_session_id', $session_id );
- //get rid of cart contents
- edd_empty_cart();
- //build an query againt, but we just need session id
- $params = array( 'sessionID' => $session_id, );
- $redirecting_to = add_query_arg( $params, 'https://www.payfast.co.za/' );
- //redirect to payfast
- wp_redirect( $redirecting_to );
- exit;
- } //end statement payment
- } //end statement error
- else {
- $fail = true; // errors were detected
- } //end statement
- if( $fail !== false ) {
- // if errors are present, send the user back to the purchase page so they can be corrected
- edd_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['edd-gateway']);
- } //end statement
- } //end process_payment()
- public function validate_report_back() {
- global $edd_options;
- // Regular Payfast notify
- if ( isset( $_GET['payfast'] ) && $_GET['payfast'] == 'notify' ) {
- do_action( 'edd_payfast_check' );
- } //end statement
- } //end validate_report_back()
- /**
- * Extract the site's host domain for referer notify from payfast.
- *
- * @since 1.0
- * @param string $url URL to extract
- * @return host of url that given on param, or false if failed extract url
- * @access private
- */
- public function check_referer_notify( $url ) {
- if ( ! ( is_string( $url ) && $url ) )
- return false;
- if ( ! function_exists('parse_url') )
- return false;
- // PHP 5.3.3 or newer can throw a warning
- try {
- if ( version_compare( PHP_VERSION, '5.1.2', '>=') ) {
- $ref = parse_url ( $url, PHP_URL_HOST );
- } else {
- $parse_ref = parse_url( $url );
- if ( $parse_ref !== false && isset( $parse_ref['host'] ) )
- $ref = $parse_ref['host'];
- }
- } catch (Exception $e){}
- // Check $ref is not empty or null, is so return that.
- if( empty( $ref ) || $ref = null )
- return false;
- else
- return $ref;
- }
- /*
- * process_payfast_notify() function
- * process notify that send by payfast after we send the buyer on payment
- *
- * if buyer complete the payment, we need update the payment to complete too
- * @since 1.0
- */
- public function process_payfast_notify() {
- global $edd_options;
- // Check the request method is POST
- if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] != 'POST' ) {
- return;
- } //end statement
- if ( isset( $_POST['status'] ) && isset( $_POST['trx_id'] ) && isset( $_POST['sid'] ) && isset( $_POST['product'] ) && isset( $_POST['quantity'] ) && isset( $_POST['total'] ) && isset( $_POST['comments'] ) ) {
- // setup each of the variables from payfast
- $payment_id = $_POST['comments'] ? $_POST['comments'] : null;
- $payfast_status = strtolower( $_POST['status'] ) ? strtolower( $_POST['status'] ) : null;
- $payfast_session = $_POST['sid'] ? $_POST['sid'] : null;
- $payfast_product = $_POST['product'] ? $_POST['product'] : null;
- $payfast_quantity = $_POST['quantity'] ? $_POST['quantity'] : null;
- $payfast_amount = $_POST['total'] ? $_POST['total'] : null;
- // retrieve the meta info for this payment
- $payment_meta = get_post_meta( $payment_id, '_edd_payment_meta', true );
- $payment_session = get_post_meta( $payment_id, '_payfast_session_id', true );
- $payment_amount = edd_format_amount( $payment_meta['amount'] );
- // check url referrer..
- if( ! function_exists( 'wp_get_referer' ) )
- include_once( ABSPATH . 'wp-includes/functions.php' );
- $referer = wp_get_referer();
- if( empty( $referer ) )
- return;
- $ref = $this->check_referer_notify( $referer );
- if ( $ref != 'payfast.co.za' )
- return; // referrer from payfast? if not return that.
- if( get_post_status( $payment_id ) == 'complete' )
- return; // Only complete payments once
- if ( edd_get_payment_gateway( $payment_id ) != 'payfast' )
- return; // this isn't from payfast
- if( $payfast_amount != $payment_amount )
- return; // the prices don't match
- // check the session id, this session is an unique key. generating by payfast
- // if this don't match, this is not from payfast or other payment
- if( $payfast_session != $payment_session )
- return;
- /* everything has been verified, update the payment to "complete"
- * berhasil is Indonesian language that mean success.
- */
- if( $payfast_status == 'berhasil' ) :
- edd_update_payment_status( $payment_id, 'publish' );
- delete_post_meta( $payment_id, '_payfast_session_id', $payment_session );
- endif;
- }//end statement
- } //end process_payfast_notify()
- } // end EDD_Payfast Class
- endif; // end check
- /**
- * Throw an error if Easy Digital Download is not installed.
- *
- * @since 0.2
- */
- function syaiful_payfast_missing_error_edd() {
- echo '<div class="error"><p>' . sprintf( __( 'Please %sinstall & activate Easy Digital Downloads%s to allow this plugin to work.' ), '<a href="' . admin_url( 'plugin-install.php?tab=search&type=term&s=easy+digital+downloads&plugin-search-input=Search+Plugins' ) . '">', '</a>' ) . '</p></div>';
- } // end syaiful_payfast_missing_error_edd()
- // Throw an error if Wordpress version is below 3.4
- function syaiful_missing_error_wordpress_version() {
- echo '<div class="error"><p>' . __( 'Please upgrade WordPress to the latest version to allow WordPress and this plugin to work properly.', 'edd_payfast' ) . '</p></div>';
- } // end syaiful_missing_error_wordpress_version()
- // the instance of our plugin,
- function edd_payfast() {
- return EDD_Payfast::instance();
- }
- // Loader function for the plugin
- function syaiful_edd_payfast_init() {
- global $wp_version;
- if ( !version_compare( $wp_version, '3.4', '>=' ) ) {
- add_action( 'all_admin_notices', 'syaiful_missing_error_wordpress_version' );
- } else if ( class_exists( 'Easy_Digital_Downloads') ) {
- edd_payfast(); //load our plugin
- } else {
- add_action( 'all_admin_notices', 'syaiful_payfast_missing_error_edd' );
- }
- } // end syaiful_edd_payfast_init()
- // tap... tap .... hi Wordpress, load our plugin please...
- add_action( 'plugins_loaded', 'syaiful_edd_payfast_init', 20 ); //lower because waiting Easy Digital Downloads running..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement