Codice PHP:
<?php
/*
Plugin Name: Postepay Getaway per Woocommerce
Plugin URI: http://plugins.svn.wordpress.org/postepay-woocommerce-gateway/
Description: Permette la scelta di Postepay come metodo di pagamento, fornendo il mumero di carta al momento del checkout.
Version: 0.1
Author: Martino Stenta
Author URI: http://www.noiza.com
License: GPL2
*/
add_action('plugins_loaded', 'woocommerce_ppay_init', 0);
function woocommerce_ppay_init() {
class WC_PPAY extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'ppay';
$this->icon = apply_filters('woocommerce_bacs_icon', '');
$this->has_fields = false;
$this->method_title = __( 'Ppay', 'woocommerce' );
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Define user set variables
$this->title = $this->settings['title'];
$this->description = $this->settings['description'];
$this->ppay_number = $this->settings['ppay_number'];
$this->ppay_user = $this->settings['ppay_user'];
// Actions
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
add_action('woocommerce_thankyou_ppay', array(&$this, 'thankyou_page'));
// Customer Emails
add_action('woocommerce_email_before_order_table', array(&$this, 'email_instructions'), 10, 2);
}
/**
* Initialise Gateway Settings Form Fields
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Abilita/Disabilita', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Abilita Postepay', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Titolo', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Definisci il titolo del sistema di pagamento.', 'woocommerce' ),
'default' => __( 'Postepay', 'woocommerce' )
),
'description' => array(
'title' => __( 'Messaggio personalizzato', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Spiega al cliente come procedere con la ricarica Postepay. Segnala che la merce non sarà inviata fino al ricevimento della ricarica.', 'woocommerce' ),
'default' => __('Paga con una ricarica Postepay. Mandaci una mail appena hai fatto il versamento indicando il numero d\'ordine e provvederemo all\'invio della merce.', 'woocommerce')
),
'ppay_number' => array(
'title' => __( 'Numero Postepay', 'woocommerce' ),
'type' => 'text',
'description' => '',
'default' => ''
),
'ppay_user' => array(
'title' => __( 'Intestatario Carta', 'woocommerce' ),
'type' => 'text',
'description' => '',
'default' => ''
),
);
} // End init_form_fields()
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country-by-country basis
*
* @since 1.0.0
*/
public function admin_options() {
?>
<h3><?php _e('Postepay', 'woocommerce'); ?></h3>
<p><?php _e('Permetti pagamenti attraverso Postepay', 'woocommerce'); ?></p>
<table class="form-table">
<?php
// Generate the HTML For the settings form.
$this->generate_settings_html();
?>
</table><!--/.form-table-->
<?php
} // End admin_options()
/**
* There are no payment fields for bacs, but we want to show the description if set.
**/
function payment_fields() {
if ($this->description) echo wpautop(wptexturize($this->description));
}
function thankyou_page() {
if ($this->description) echo wpautop(wptexturize($this->description));
?><h2><?php _e('Our Details', 'woocommerce') ?></h2><ul class="order_details ppay_details"><?php
$fields = array(
'ppay_number'=> __('Numero Postepay', 'woocommerce')
$fields = array(
'ppay_user'=> __('Intestatario Carta', 'woocommerce')
);
foreach ($fields as $key=>$value) :
if(!empty($this->$key)) :
echo '<li class="'.$key.'">'.$value.': <strong>'.wptexturize($this->$key).'</strong></li>';
endif;
endforeach;
?></ul><?php
}
/**
* Add text to user email
**/
function email_instructions( $order, $sent_to_admin ) {
if ( $sent_to_admin ) return;
if ( $order->status !== 'on-hold') return;
if ( $order->payment_method !== 'ppay') return;
if ($this->description) echo wpautop(wptexturize($this->description));
?><h2><?php _e('Informazioni', 'woocommerce') ?></h2><ul class="order_details ppay_details"><?php
$fields = array(
'ppay_number'=> __('Numero Postepay', '')
);
$fields = array(
'ppay_user'=> __('Intestatario Carta', '')
);
foreach ($fields as $key=>$value) :
if(!empty($this->$key)) :
echo '<li class="'.$key.'">'.$value.': <strong>'.wptexturize($this->$key).'</strong></li>';
endif;
endforeach;
?></ul><?php
}
/**
* Process the payment and return the result
**/
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
// Mark as on-hold (we're awaiting the payment)
$order->update_status('on-hold', __('In attesa di versamento Postepay', 'woocommerce'));
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
$woocommerce->cart->empty_cart();
// Empty awaiting payment session
unset($_SESSION['order_awaiting_payment']);
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order->id, get_permalink(woocommerce_get_page_id('thanks'))))
);
}
}
/**
* Add the Gateway to WooCommerce
**/
function woocommerce_add_ppay_gateway($methods) {
$methods[] = 'WC_PPAY';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'woocommerce_add_ppay_gateway' );
}