Untitled diff
316 removals
417 lines
209 additions
315 lines
<?php
<?php
/**
/**
* Plugin Name: WooCommerce 2Checkout Payment Gateway Free
* Plugin Name: Steempay.io
* Plugin URI: http://www.najeebmedia.com/2checkout-payment-gateway-for-woocommerce/
* Plugin URI: https://steempay.io
* Description: 2Checkout is payment gateway for WooCommerce allowing you to take payments via 2Checkout.
* Description: steempay is a payment gateway for WooCommerce allowing you to accept STEEM/SBD.
* Version: 1.7
* Version: 0.2
* Author: Najeeb Ahmad
* Author: Steve Walschot
* Author URI: http://www.najeebmedia.com/
* Author URI: https://steemit.com/@steve-walschot
*/
*/
if ( ! defined( 'ABSPATH' ) )
exit;
add_action( 'plugins_loaded', 'init_nm_woo_gateway', 0);
add_action( 'plugins_loaded', 'init_nm_woo_gateway', 0);
function nm_2co_settings( $links ) {
function nm_2co_settings( $links ) {
$settings_link = '<a href="'.admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_nm_twocheckout' ).'">Setup</a>';
$settings_link = '<a href="'.admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_nm_steempay' ).'">Setup</a>';
array_push( $links, $settings_link );
array_push( $links, $settings_link );
return $links;
return $links;
}
}
$plugin = plugin_basename( __FILE__ );
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin", 'nm_2co_settings' );
add_filter( "plugin_action_links_$plugin", 'nm_2co_settings' );
function use_woocommerce_custom_css() {
wp_enqueue_style(
'woocommerce-custom',
WP_PLUGIN_URL . DIRECTORY_SEPARATOR . '/steempay-woo-plugin/assets/css/steempay.css'
);
}
add_action('wp_enqueue_scripts', 'use_woocommerce_custom_css', 15);
function init_nm_woo_gateway(){
function init_nm_woo_gateway(){
if ( ! class_exists( 'WC_Payment_Gateway' ) ) return;
if ( ! class_exists( 'WC_Payment_Gateway' ) ) return;
class WC_Gateway_NM_TwoCheckout extends WC_Payment_Gateway {
class WC_Gateway_NM_steempay extends WC_Payment_Gateway {
var $seller_id;
var $receiver_id;
var $demo;
var $demo;
var $plugin_url;
var $plugin_url;
public function __construct(){
public function __construct(){
global $woocommerce;
global $woocommerce;
$this -> plugin_url = WP_PLUGIN_URL . DIRECTORY_SEPARATOR . 'woocommerce-2checkout-payment';
$this -> plugin_url = WP_PLUGIN_URL . '/steempay-woo-plugin';
$this->id = 'nmwoo_2co';
$this->id = 'nmwoo_stpy';
$this->has_fields = false;
$this->has_fields = false;
$this->checkout_url = 'https://www.2checkout.com/checkout/purchase';
$this->checkout_url = 'https://steempay.io/WOO/';
$this->checkout_url_sandbox = 'https://sandbox.2checkout.com/checkout/purchase';
$this->checkout_url_sandbox = 'https://steempay.io/WOO/';
$this->icon = $this -> plugin_url.'/images/2co_logo.png';
$this->icon = $this -> plugin_url.'/assets/images/l01.png';
$this->method_title = '2Checkout';
$this->method_title = 'steempay';
$this->method_description = 'This plugin add 2checkout payment gateway with Woocommerce based shop. Make sure you have set your 2co account according <a href="http://najeebmedia.com/2checkout-payment-gateway-for-woocommerce/" target="_blank">these setting</a>';
$this->method_description = 'This plugin add steempay payment gateway with Woocommerce based shop.';
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->receiver_id = $this->get_option( 'receiver_id' );
$this->seller_id = $this->get_option( 'seller_id' );
$this -> steem_currency = $this -> get_option('steem_currency');
$this->secret_word = $this->get_option( 'secret_word' );
$this -> demo = $this -> get_option('demo');
$this->title = '<span>' . $this->get_option( 'title' ) . ' (' . $this -> steem_currency . ')</span>';
$this -> pay_method = $this -> get_option('pay_method');
$this->init_form_fields();
$this->init_form_fields();
$this->init_settings();
$this->init_settings();
// Save options
// Save options
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action('process_2co_ipn_request', array( $this, 'successful_request' ), 1 );
// Payment listener/API hook
// Payment listener/API hook
add_action( 'woocommerce_api_wc_gateway_nm_twocheckout', array( $this, 'twocheckout_response' ) );
add_action( 'woocommerce_api_wc_gateway_nm_steempay', array( $this, 'steempay_response' ) );
// Check if the gateway can be used
if ( ! $this->is_valid_for_use() ) {
$this->enabled = false;
}
}
public function is_valid_for_use() {
if( ! in_array( get_woocommerce_currency(), array( 'EUR', 'USD' ) ) ) {
$this->msg = 'Steempay doesn\'t support your store currency, set it to Euro (EUR) or United State Dollars (USD)';
return false;
}
return true;
}
public function is_available() {
if ( $this->enabled == "yes" ) {
if ( ! $this->receiver_id ) {
return false;
}
return true;
}
return false;
}
}
public function admin_options() {
echo '<h3>SteemPay</h3>';
echo '<p>Steempay is a payment gateway for WooCommerce allowing you to accept STEEM/SBD.</p>';
if ( $this->is_valid_for_use() ){
echo '<table class="form-table">';
$this->generate_settings_html();
echo '</table>';
}
else{ ?>
<div class="inline error"><p><strong>SteemPay Payment Gateway Disabled</strong>: <?php echo $this->msg ?></p></div>
<?php }
}
function init_form_fields(){
function init_form_fields(){
$this->form_fields = array(
$this->form_fields = array(
'enabled' => array(
'enabled' => array(
'title' => __( 'Enable', 'woocommerce' ),
'title' => __( 'Enable', 'woocommerce' ),
'type' => 'checkbox',
'type' => 'checkbox',
'label' => __( 'Yes', 'woocommerce' ),
'label' => __( 'Yes', 'woocommerce' ),
'default' => 'yes'
'default' => 'yes'
),
),
'inline' => array(
'steem_currency' => array(
'title' => __( 'Enable Inline Checkout', 'woocommerce' ),
'title' => __( 'Steem Currency', 'woocommerce' ),
'type' => 'checkbox',
'type' => 'select',
'label' => __( 'Yes - It is PRO Feature get <a href="http://www.najeebmedia.com/2checkout-payment-gateway-for-woocommerce/" target="_blank">Pro Version</a>', 'woocommerce' ),
'default' => 'SBD', // WooCommerce >= 2.0
'default' => 'yes'
'description' => __( 'Select if you want to receive STEEM or Steem Dollars (SBD).', 'woocommerce' ),
'desc_tip' => true,
'options' => array(
'STEEM' => __( 'STEEM', 'woocommerce' ),
'SBD' => __( 'Steem Dollars', 'woocommerce' ),
),
),
),
'seller_id' => array(
'receiver_id' => array(
'title' => __( '2CO Account #', 'woocommerce' ),
'title' => __( 'Receiver account', 'woocommerce' ),
'type' => 'text',
'type' => 'text',
'description' => __( 'This Seller ID issued by 2Checkout', 'woocommerce' ),
'description' => __( 'Username to receive payments (without @)', 'woocommerce' ),
'default' => '',
'default' => '',
'desc_tip' => true,
'desc_tip' => true,
),
),
'secret_word' => array(
'title' => __( 'Secret Word', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Please enter your 2Checkout Secret Word.', 'woocommerce' ),
'default' => '',
'desc_tip' => true,
'placeholder' => ''
),
'title' => array(
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( '2Checkout Payment', 'woocommerce' ),
'default' => __( 'Pay using SteemPay', 'woocommerce' ),
'desc_tip' => true,
'desc_tip' => true,
),
),
'description' => array(
'cancel_url' => array(
'title' => __( 'Customer Message', 'woocommerce' ),
'title' => __( 'Cancel URL', 'woocommerce' ),
'type' => 'textarea',
'default' => ''
),
'demo' => array(
'title' => __( 'Enable Demo Mode', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Yes', 'woocommerce' ),
'default' => 'yes'
),
'pay_method' => array(
'title' => __( 'Payment Method', 'woocommerce' ),
'type' => 'text',
'type' => 'text',
'description' => __( 'CC for Credit Card, PPI for PayPal. This will set the default selection on the payment method step during the checkout process.', 'woocommerce' ),
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'CC', 'woocommerce' ),
'default' => __( 'steempay Payment', 'woocommerce' ),
'desc_tip' => true,
'desc_tip' => true,
),
)
);
);
}
}
/**
* Process the payment and return the result
*
* @access public
* @param int $order_id
* @return array
*/
function process_payment( $order_id ) {
function process_payment( $order_id ) {
$order = new WC_Order( $order_id );
$order = new WC_Order( $order_id );
$twoco_args = $this->get_twoco_args( $order );
$twoco_args = $this->get_twoco_args( $order );
/*echo '<pre>';
print_r($twoco_args);
echo '</pre>';
exit;*/
$twoco_args = http_build_query( $twoco_args, '', '&' );
$twoco_args = http_build_query( $twoco_args, '', '&' );
$checkout_url = $this->checkout_url;
//if demo is enabled
$checkout_url = '';
if ($this -> demo == 'yes'){
$checkout_url = $this->checkout_url_sandbox;
}else{
$checkout_url = $this->checkout_url;
}
return array(
return array(
'result' => 'success',
'result' => 'success',
'redirect' => $checkout_url.'?'.$twoco_args
'redirect' => $checkout_url.'?'.$twoco_args
);
);
}
}
/**
* Get 2Checkout Args for passing to PP
*
* @access public
* @param mixed $order
* @return array
*/
function get_twoco_args( $order ) {
function get_twoco_args( $order ) {
global $woocommerce;
global $woocommerce;
$order_id = $order->id;
$order_id = $order->id;
// 2Checkout Args
// steempay Args
$twoco_args = array(
$twoco_args = array(
'sid' => $this -> seller_id,
'sid' => $this -> receiver_id,
'mode' => '2CO',
'merchant_order_id' => $order_id,
'merchant_order_id' => $order_id,
'currency_code' => get_woocommerce_currency(),
'currency_code' => get_woocommerce_currency(),
'receiver' => $this -> receiver_id,
// Billing Address info
'amount' => $order->get_total(),
'first_name' => $order->billing_first_name,
'currency' => $order->get_order_currency(),
'last_name' => $order->billing_last_name,
'output' => $this->steem_currency,
'street_address' => $order->billing_address_1,
'payid' => $this->generatePaymentID()
'street_address2' => $order->billing_address_2,
'city' => $order->billing_city,
'state' => $order->billing_state,
'zip' => $order->billing_postcode,
'country' => $order->billing_country,
'email' => $order->billing_email,
'phone' => $order->billing_phone,
);
);
// Shipping
$twoco_args['x_receipt_link_url'] = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'WC_Gateway_NM_steempay', home_url( '/' ) ) );
$twoco_args['ship_name'] = $order->shipping_first_name.' '.$order->shipping_last_name;
$twoco_args['company'] = $order->shipping_company;
$twoco_args['ship_street_address'] = $order->shipping_address_1;
$twoco_args['ship_street_address2'] = $order->shipping_address_2;
$twoco_args['ship_city'] = $order->shipping_city;
$twoco_args['ship_state'] = $order->shipping_state;
$twoco_args['ship_zip'] = $order->shipping_country;
$twoco_args['ship_country'] = $order->shipping_postcode;
$twoco_args['x_receipt_link_url'] = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'WC_Gateway_NM_TwoCheckout', home_url( '/' ) ) );
$twoco_args['return_url'] = str_replace('https', 'http', $order->get_cancel_order_url());
$twoco_args['return_url'] = str_replace('https', 'http', $order->get_cancel_order_url());
//setting payment method
if ($this -> pay_method)
$twoco_args['pay_method'] = $this -> pay_method;
//if demo is enabled
if ($this -> demo == 'yes'){
$twoco_args['demo'] = 'Y';
}
$item_names = array();
if ( sizeof( $order->get_items() ) > 0 ){
$twoco_product_index = 0;
foreach ( $order->get_items() as $item ){
if ( $item['qty'] )
$item_names[] = $item['name'] . ' x ' . $item['qty'];
/*echo '<pre>';
print_r($item);
echo '</pre>';
exit;*/
/**
* since version 1.6
* adding support for both WC Versions
*/
$_sku = '';
if ( function_exists( 'get_product' ) ) {
// Version 2.0
$product = $order->get_product_from_item($item);
// Get SKU or product id
if ( $product->get_sku() ) {
$_sku = $product->get_sku();
} else {
$_sku = $product->id;
}
} else {
// Version 1.6.6
$product = new WC_Product( $item['id'] );
// Get SKU or product id
if ( $product->get_sku() ) {
$_sku = $product->get_sku();
} else {
$_sku = $item['id'];
}
}
if ( $product->is_virtual() || $product->is_downloadable() ) :
$tangible = "N";
else :
$tangible = "Y";
endif;
$item_formatted_name = $item['name'] . ' (Product SKU: '.$item['product_id'].')';
$twoco_args['li_'.$twoco_product_index.'_type'] = 'product';
$twoco_args['li_'.$twoco_product_index.'_name'] = sprintf( __( 'Order %s' , 'woocommerce'), $order->get_order_number() ) . " - " . $item_formatted_name;
$twoco_args['li_'.$twoco_product_index.'_quantity'] = $item['qty'];
$twoco_args['li_'.$twoco_product_index.'_price'] = number_format( $order->get_item_total( $item, false ), 2, '.', '' );
$twoco_args['li_'.$twoco_product_index.'_product_id'] = $_sku;
$twoco_args['li_'.$twoco_product_index.'_tangible'] = $tangible;
$twoco_product_index++;
}
// Shipping Cost
if ( $order -> get_total_shipping() > 0 ) {
$twoco_product_index++;
$twoco_args['li_'.$twoco_product_index.'_type'] = 'shipping';
$twoco_args['li_'.$twoco_product_index.'_name'] = __( 'Shipping charges', 'woocommerce' );
$twoco_args['li_'.$twoco_product_index.'_quantity'] = 1;
$twoco_args['li_'.$twoco_product_index.'_price'] = number_format( $order -> get_total_shipping() , 2, '.', '' );
}
// Taxes (shipping tax too)
if ( $order -> get_total_tax() > 0 ) {
$twoco_product_index++;
$twoco_args['li_'.$twoco_product_index.'_type'] = 'tax';
$twoco_args['li_'.$twoco_product_index.'_name'] = __( 'Tax', 'woocommerce' );
$twoco_args['li_'.$twoco_product_index.'_quantity'] = 1;
$twoco_args['li_'.$twoco_product_index.'_price'] = number_format( $order->get_total_tax() , 2, '.', '' );
}
$twoco_args = apply_filters( 'woocommerce_twoco_args', $twoco_args );
}
return $twoco_args;
return $twoco_args;
}
}
/**
function generatePaymentID() {
* this function is return product object for two
$length = 18;
* differetn version of WC
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
*/
$charactersLength = strlen($characters);
$payID = '';
for ($i = 0; $i < $length; $i++) {
$payID .= $characters[rand(0, $charactersLength - 1)];
}
return $payID;
}
function get_product_object(){
function get_product_object(){
return $product;
return $product;
}
}
function steempay_response() {
/**
* Check for 2Checkout IPN Response
*
* @access public
* @return void
*/
function twocheckout_response() {
/**
* source code: https://github.com/craigchristenson/woocommerce-2checkout-api
* Thanks to: https://github.com/craigchristenson
*/
global $woocommerce;
global $woocommerce;
@ob_clean();
@ob_clean();
$wc_order_id = $_REQUEST['merchant_order_id'];
$wc_order_id = $_REQUEST['merchant_order_id'];
$wc_order = new WC_Order( absint( $wc_order_id ) );
if ($this -> demo == 'yes' || (isset($_REQUEST['demo']) && $_REQUEST['demo'] == 'Y') ){
$compare_string = $this->secret_word . $this->seller_id . "1" . $_REQUEST['total'];
}else{
$compare_string = $this->secret_word . $this->seller_id . $_REQUEST['order_number'] . $_REQUEST['total'];
}
$compare_hash1 = strtoupper(md5($compare_string));
$compare_hash2 = $_REQUEST['key'];
$environment_url = ( 'http://steempay.io/payment/verify?' ) ;
if ($compare_hash1 != $compare_hash2) {
wp_die( "2Checkout Hash Mismatch... check your secret word." );
$payload = array(
} else {
"payid" => $_REQUEST['payid'],
"receiver" => $this -> receiver_id,
"amount" => "0",
"currency" => "0"
);
// Send this payload to steempay
$response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
if ( is_wp_error( $response ) )
throw new Exception( __( 'We are currently experiencing problems trying to connect to this payment gateway. Sorry for the inconvenience.', 'woocommerce' ) );
if ( empty( $response['body'] ) )
throw new Exception( __( 'SteemPay.io\'s Response was empty.', 'woocommerce' ) );
// Retrieve the body's resopnse if no errors found
$response_body = wp_remote_retrieve_body( $response );
$json_data = json_decode($response_body, true);
/*
echo '<pre>';
print_r($json_data);
echo '</pre>';
exit;
/*
// TESTING ToDo: Remove
echo '<pre>';
echo 'Total order formatted: ' . $wc_order->get_formatted_order_total();
echo '<br>';
echo 'Order currency: ' .$wc_order->get_order_currency();
echo '<br>';
echo 'Order total: ' .$wc_order->get_total();
echo '<br>';
print_r($wc_order);
echo '</pre>';
exit;
*/
// Get the values we need
$r['call_status'] = $json_data['status'];
$r['call_success'] = $json_data['success'];
if ( ( $r['call_status'] == 'success' ) || ( $r['call_success'] == 1 ) ) {
$wc_order = new WC_Order( absint( $wc_order_id ) );
$wc_order = new WC_Order( absint( $wc_order_id ) );
// Mark order complete
// Mark order complete
$wc_order->payment_complete();
$wc_order->payment_complete();
// Empty cart and clear session
// Empty cart and clear session
$woocommerce->cart->empty_cart();
$woocommerce->cart->empty_cart();
wp_redirect( $this->get_return_url( $wc_order ) );
wp_redirect( $this->get_return_url( $wc_order ) );
exit;
}
}
/*
* valid requoest posed from 2Checkout
*/
function successful_request($posted){
//testing ipn request
if($posted['invoice_status'] == 'approved'){
global $woocommerce;
$order_id = $posted['merchant_order_id'];
} else {
$wc_order = new WC_Order( absint( $wc_order_id ) );
//this was set for IPN Simulator
$wc_order->update_status('on-hold', '');
//$order_id = $posted['vendor_order_id'];
// Transaction was not succesful
$order = new WC_Order( $order_id );
wc_add_notice( $r['message'], 'error' );
$wc_order->add_order_note( 'Error: '. $r['message'] );
// Store PP Details
$woocommerce->cart->empty_cart();
if ( ! empty( $posted['customer_email'] ) )
wp_redirect( $this->get_return_url( $wc_order ) );
update_post_meta( $order->id, 'Customer email address', $posted['customer_email'] );
if ( ! empty( $posted['sale_id'] ) )
update_post_meta( $order->id, 'Sale ID', $posted['sale_id'] );
if ( ! empty( $posted['customer_first_name '] ) )
update_post_meta( $order->id, 'Payer first name', $posted['customer_first_name'] );
if ( ! empty( $posted['customer_last_name '] ) )
update_post_meta( $order->id, 'Payer last name', $posted['customer_last_name'] );
if ( ! empty( $posted['payment_type'] ) )
update_post_meta( $order->id, 'Payment type', $posted['payment_type'] );
// Payment completed
$order->add_order_note( __( 'IPN completed by 2CO', 'woocommerce' ) );
$order->payment_complete();
$woocommerce -> cart -> empty_cart();
}
}
}
}
}
}
}
}
function add_nm_payment_gateway( $methods ) {
function add_nm_payment_gateway( $methods ) {
$methods[] = 'WC_Gateway_NM_TwoCheckout';
$methods[] = 'WC_Gateway_NM_steempay';
return $methods;
return $methods;
}
}
add_filter( 'woocommerce_payment_gateways', 'add_nm_payment_gateway' );
add_filter( 'woocommerce_payment_gateways', 'add_nm_payment_gateway' );
?>
?>