Advertisement
Guest User

index.php

a guest
Mar 20th, 2014
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.19 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WooCommerce Perfect Money Payment Gateway
  4. Plugin URI: http://perfectmoney.is
  5. Description: Perfect Money Payment gateway for woocommerce
  6. Version: 1.0
  7. Author: SR & I
  8. Author URI: http://perfectmoney.is
  9. */
  10. add_action('plugins_loaded', 'woocommerce_gateway_perfectmoney_init', 0);
  11. function woocommerce_gateway_perfectmoney_init(){
  12.   if(!class_exists('WC_Payment_Gateway')) return;
  13.  
  14.   class WC_Gateway_PerfectMoney extends WC_Payment_Gateway{
  15.     public function __construct(){
  16.       $this -> id = 'perfectmoney';
  17.       $this -> medthod_title = 'Perfect Money';
  18.       $this -> icon = get_site_url().'/wp-content/plugins/perfectmoney/perfectmoney.png';
  19.       $this -> has_fields = false;
  20.  
  21.       $this -> init_form_fields();
  22.       $this -> init_settings();
  23.  
  24.       $this -> title = $this -> settings['title'];
  25.       $this -> description = $this -> settings['description'];
  26.       $this -> payee_account = $this -> settings['payee_account'];
  27.       $this -> payee_name = $this -> settings['payee_name'];
  28.       $this -> alternate_phrase = $this -> settings['alternate_phrase'];
  29.       $this -> redirect_page_id = $this -> settings['redirect_page_id'];
  30.       $this -> liveurl = 'https://perfectmoney.is/api/step1.asp';
  31.       define('CALLBACK_URL', get_site_url().'/?wc-api=WC_Gateway_PerfectMoney&perfectmoney=callback');
  32.  
  33.       $this -> msg['message'] = "";
  34.       $this -> msg['class'] = "";
  35.  
  36.       add_action('woocommerce_api_wc_gateway_perfectmoney', array($this, 'check_perfectmoney_response'));
  37.         if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) ) {
  38.                 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options' ) );
  39.              } else {
  40.                 add_action( 'woocommerce_update_options_payment_gateways', array($this, 'process_admin_options' ) );
  41.             }
  42.       add_action('woocommerce_receipt_perfectmoney', array($this, 'receipt_page'));
  43.     }
  44.  
  45.     function init_form_fields(){
  46.  
  47.        $this -> form_fields = array(
  48.                 'enabled' => array(
  49.                     'title' => __('Enable/Disable', 'pm'),
  50.                     'type' => 'checkbox',
  51.                     'label' => __('Enable Perfect Money Payment Module.', 'pm'),
  52.                     'default' => 'no'),
  53.                 'title' => array(
  54.                     'title' => __('Title:', 'pm'),
  55.                     'type'=> 'text',
  56.                     'description' => __('This controls the title which the user sees during checkout.', 'pm'),
  57.                     'default' => __('Perfect Money', 'pm')),
  58.                 'description' => array(
  59.                     'title' => __('Description:', 'pm'),
  60.                     'type' => 'textarea',
  61.                     'description' => __('This controls the description which the user sees during checkout.', 'pm'),
  62.                     'default' => __('Pay securely by Perfect Money through Secure Servers.', 'pm')),
  63.                 'payee_account' => array(
  64.                     'title' => __('Payee Account', 'pm'),
  65.                     'type' => 'text',
  66.                     'description' => __('Your Perfect Money account you want to get payments to (like U12345)')),
  67.                 'payee_name' => array(
  68.                     'title' => __('Payee Name', 'pm'),
  69.                     'type' => 'text',
  70.                     'default' => __('Shop', 'pm')),
  71.                 'alternate_phrase' => array(
  72.                     'title' => __('Alternate PassPhrase', 'pm'),
  73.                     'type' => 'text',
  74.                     'description' =>  __('Alternate PassPhrase can be found and set under Settings section in your
  75. PM account.', 'pm'),
  76.                 ),
  77.                 'redirect_page_id' => array(
  78.                     'title' => __('Return Page'),
  79.                     'type' => 'select',
  80.                     'options' => $this -> get_pages('Select Page'),
  81.                     'description' => "URL of success page"
  82.                 )
  83.             );
  84.     }
  85.  
  86.        public function admin_options(){
  87.         echo '<h3>'.__('Perfect Money Payment Gateway', 'pm').'</h3>';
  88.         echo '<p>'.__('Perfect Money popular payment gateway for online shopping.').'</p>';
  89.         echo '<table class="form-table">';
  90.         // Generate the HTML For the settings form.
  91.         $this -> generate_settings_html();
  92.         echo '</table>';
  93.  
  94.     }
  95.  
  96.     /**
  97.      *  There are no payment fields for perfectmoney, but we want to show the description if set.
  98.      **/
  99.     function payment_fields(){
  100.         if($this -> description) echo wpautop(wptexturize($this -> description));
  101.     }
  102.     /**
  103.      * Receipt Page
  104.      **/
  105.     function receipt_page($order){
  106.         echo '<p>'.__('Thank you for your order, please click the button below to pay with Perfect Money.', 'pm').'</p>';
  107.         echo $this -> generate_perfectmoney_form($order);
  108.     }
  109.     /**
  110.      * Generate perfectmoney button link
  111.      **/
  112.     public function generate_perfectmoney_form($order_id){
  113.  
  114.        global $woocommerce;
  115.         $order = new WC_Order( $order_id );
  116.         $txnid = $order_id.'_'.date("ymds");
  117.  
  118.         $redirect_url = ($this -> redirect_page_id=="" || $this -> redirect_page_id==0)?get_site_url() . "/":get_permalink($this -> redirect_page_id);
  119.  
  120.         $productinfo = "Order $order_id";
  121.  
  122.         $str = "$this->payee_account|$txnid|$order->order_total|$productinfo|$order->billing_first_name|$order->billing_email|||||||||||$this->alternate_phrase";
  123.         $hash = hash('sha512', $str);
  124.  
  125.         $perfectmoney_args = array(
  126.           'key' => $this -> payee_account,
  127.           'txnid' => $txnid,
  128.           'amount' => $order -> order_total,
  129.           'productinfo' => $productinfo,
  130.           'firstname' => $order -> billing_first_name,
  131.           'lastname' => $order -> billing_last_name,
  132.           'address1' => $order -> billing_address_1,
  133.           'address2' => $order -> billing_address_2,
  134.           'city' => $order -> billing_city,
  135.           'state' => $order -> billing_state,
  136.           'country' => $order -> billing_country,
  137.           'zipcode' => $order -> billing_zip,
  138.           'email' => $order -> billing_email,
  139.           'phone' => $order -> billing_phone,
  140.           'surl' => $redirect_url,
  141.           'furl' => $redirect_url,
  142.           'curl' => $redirect_url,
  143.           'hash' => $hash,
  144.           'pg' => 'NB'
  145.           );
  146.  
  147.           $currs=array('U'=>'USD', 'E'=>'EUR');
  148.           $cur_l=substr($this -> payee_account, 0,1);
  149.           $currency=$currs[$cur_l];
  150.  
  151.         $perfectmoney_args_array = array();
  152.         foreach($perfectmoney_args as $key => $value){
  153.           $perfectmoney_args_array[] = "<input type='hidden' name='$key' value='$value'/>";
  154.         }
  155.         return '<form action="'.$this -> liveurl.'" method="post" id="perfectmoney_payment_form">
  156. <input type="hidden" name="SUGGESTED_MEMO" value="'.$productinfo.'">
  157.  
  158. <input type="hidden" name="PAYMENT_ID" value="'.$order_id.'" />
  159. <input type="hidden" name="PAYMENT_AMOUNT" value="'.$order -> order_total.'" />
  160. <input type="hidden" name="PAYEE_ACCOUNT" value="'.$this -> payee_account.'" />
  161. <input type="hidden" name="PAYMENT_UNITS" value="'.$currency.'" />
  162. <input type="hidden" name="PAYEE_NAME" value="'.$this -> payee_name.'" />
  163. <input type="hidden" name="PAYMENT_URL" value="'.$redirect_url.'" />
  164. <input type="hidden" name="PAYMENT_URL_METHOD" value="LINK" />
  165. <input type="hidden" name="NOPAYMENT_URL" value="'.$redirect_url.'" />
  166. <input type="hidden" name="NOPAYMENT_URL_METHOD" value="LINK" />
  167. <input type="hidden" name="STATUS_URL" value="'.CALLBACK_URL.'" />
  168.            <input type="submit" class="button-alt" id="submit_perfectmoney_payment_form" value="'.__('Pay via Perfect Money', 'pm').'" /> <a class="button cancel" href="'.$order->get_cancel_order_url().'">'.__('Cancel order &amp; restore cart', 'pm').'</a>
  169.            <script type="text/javascript">
  170. jQuery(function(){
  171. jQuery("body").block(
  172.        {
  173.            message: "<img src=\"'.$woocommerce->plugin_url().'/assets/images/ajax-loader.gif\" alt=\"Redirecting…\" style=\"float:left; margin-right: 10px;\" />'.__('Thank you for your order. We are now redirecting you to Payment Gateway to make payment.', 'pm').'",
  174.                overlayCSS:
  175.        {
  176.            background: "#fff",
  177.                opacity: 0.6
  178.    },
  179.    css: {
  180.        padding:        20,
  181.            textAlign:      "center",
  182.            color:          "#555",
  183.            border:         "3px solid #aaa",
  184.            backgroundColor:"#fff",
  185.            cursor:         "wait",
  186.            lineHeight:"32px"
  187.    }
  188.    });
  189.    jQuery("#submit_perfectmoney_payment_form").click();});</script>
  190.            </form>';
  191.  
  192.  
  193.     }
  194.     /**
  195.      * Process the payment and return the result
  196.      **/
  197.     function process_payment($order_id){
  198.         global $woocommerce;
  199.         $order = new WC_Order( $order_id );
  200.         return array('result' => 'success', 'redirect' => add_query_arg('order',
  201.             $order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))))
  202.         );
  203.     }
  204.  
  205.     /**
  206.      * Check for valid perfectmoney server callback
  207.      **/
  208.     function check_perfectmoney_response(){
  209.         global $woocommerce;
  210.  
  211.         define('ALTERNATE_PHRASE_HASH', strtoupper(md5($this->alternate_phrase)));
  212.  
  213.         // Path to directory to save logs. Make sure it has write permissions.
  214.         //define('PATH_TO_LOG',  '/somewhere/out/of/document_root/');
  215.  
  216.         $string=
  217.               $_POST['PAYMENT_ID'].':'.$_POST['PAYEE_ACCOUNT'].':'.
  218.               $_POST['PAYMENT_AMOUNT'].':'.$_POST['PAYMENT_UNITS'].':'.
  219.               $_POST['PAYMENT_BATCH_NUM'].':'.
  220.               $_POST['PAYER_ACCOUNT'].':'.ALTERNATE_PHRASE_HASH.':'.
  221.               $_POST['TIMESTAMPGMT'];
  222.  
  223.         $hash=strtoupper(md5($string));
  224.  
  225.         if($hash==$_POST['V2_HASH']){ // proccessing payment if only hash is valid
  226.  
  227.             $order = new WC_Order($_POST['PAYMENT_ID']);
  228.  
  229.             if($_POST['PAYMENT_AMOUNT']==$order->order_total && $_POST['PAYEE_ACCOUNT']==$this->payee_account){
  230.  
  231.                 $order -> payment_complete();
  232.                 $order -> add_order_note('Perfect Money payment successful<br/>Unnique Id from Perfect Money: '.$_REQUEST['mihpayid']);
  233.                 $order -> add_order_note($this->msg['message']);
  234.                 $woocommerce -> cart -> empty_cart();
  235.  
  236.                 /*f=fopen(PATH_TO_LOG."good.log", "ab+");
  237.                 fwrite($f, date("d.m.Y H:i")."; POST: ".serialize($_POST)."; STRING: $string; HASH: $hash\n");
  238.                 fclose($f);*/
  239.  
  240.            }else{ // you can also save invalid payments for debug purposes
  241.  
  242.               /*$f=fopen(PATH_TO_LOG."bad.log", "ab+");
  243.               fwrite($f, date("d.m.Y H:i")."; REASON: fake data; POST: ".serialize($_POST)."; STRING: $string; HASH: $hash\n");
  244.               fclose($f);*/
  245.  
  246.            }
  247.  
  248.  
  249.         }else{ // you can also save invalid payments for debug purposes
  250.  
  251.            // uncomment code below if you want to log requests with bad hash
  252.            /*$f=fopen(PATH_TO_LOG."bad.log", "ab+");
  253.            fwrite($f, date("d.m.Y H:i")."; REASON: bad hash; POST: ".serialize($_POST)."; STRING: $string; HASH: $hash\n");
  254.            fclose($f);*/
  255.  
  256.         }
  257.  
  258.         wp_die('done');
  259.  
  260.     }
  261.  
  262.     function showMessage($content){
  263.             return '<div class="box '.$this -> msg['class'].'-box">'.$this -> msg['message'].'</div>'.$content;
  264.         }
  265.      // get all pages
  266.     function get_pages($title = false, $indent = true) {
  267.         $wp_pages = get_pages('sort_column=menu_order');
  268.         $page_list = array();
  269.         if ($title) $page_list[] = $title;
  270.         foreach ($wp_pages as $page) {
  271.             $prefix = '';
  272.             // show indented child pages?
  273.             if ($indent) {
  274.                 $has_parent = $page->post_parent;
  275.                 while($has_parent) {
  276.                     $prefix .=  ' - ';
  277.                     $next_page = get_page($has_parent);
  278.                     $has_parent = $next_page->post_parent;
  279.                 }
  280.             }
  281.             // add to page list array array
  282.             $page_list[$page->ID] = $prefix . $page->post_title;
  283.         }
  284.         return $page_list;
  285.     }
  286. }
  287.    /**
  288.      * Add the Gateway to WooCommerce
  289.      **/
  290.     function woocommerce_add_perfectmoney_gateway($methods) {
  291.         $methods[] = 'WC_Gateway_PerfectMoney';
  292.         return $methods;
  293.     }
  294.  
  295.     add_filter('woocommerce_payment_gateways', 'woocommerce_add_perfectmoney_gateway' );
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement