Skip to content

Commit

Permalink
Refactorings: wc gateway settings API, code cleanup. Fixes seanvoss#24,
Browse files Browse the repository at this point in the history
  • Loading branch information
mgiulio committed Sep 10, 2014
1 parent 6a87ea3 commit 67413f6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 78 deletions.
File renamed without changes
File renamed without changes.
4 changes: 3 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ None at this time

= 0.30 =
* Bug fix: Force SSL warning bad link, gh-17
* Enhancement: prepare plugin messages for I18N
* Bug fix: no need of explicit unset of 'order_awaiting_payment' session variable in newer WC versions, gh-21
* Enhancement: prepare plugin messages for I18N
* Refactoring: wc gateway admin settings API
* Code cleanup
= 0.29 =
* Fixing bug where Striper is not the payment type selected.
= 0.28 =
Expand Down
116 changes: 57 additions & 59 deletions stripe_gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

class Striper extends WC_Payment_Gateway
{
private $version = '0.28';
private $version = '0.30';
private $path;
private $url;


protected $GATEWAY_NAME = "Striper";
protected $usesandboxapi = true;
protected $order = null;
protected $transactionId = null;
Expand All @@ -29,25 +28,31 @@ public function __construct()
{
$this->setup_paths_and_urls();

$this->id = 'Striper';
$this->id = 'striper';
$this->method_title = __('Striper', 'striper');
$this->method_description = __('Process credit cards via Stripe', 'striper');
$this->has_fields = true;

$this->init_form_fields();
$this->init_settings();

$this->title = $this->settings['title'];
$this->description = '';
$this->icon = $this->settings['alternate_imageurl'] ? $this->settings['alternate_imageurl'] : WP_PLUGIN_URL . "/" . plugin_basename( dirname(__FILE__)) . '/images/credits.png';
$this->usesandboxapi = strcmp($this->settings['debug'], 'yes') == 0;
$this->testApiKey = $this->settings['test_api_key' ];
$this->liveApiKey = $this->settings['live_api_key' ];
$this->testPublishableKey = $this->settings['test_publishable_key' ];
$this->livePublishableKey = $this->settings['live_publishable_key' ];
$this->useUniquePaymentProfile = strcmp($this->settings['enable_unique_profile'], 'yes') == 0;
$this->useInterval = strcmp($this->settings['enable_interval'], 'yes') == 0;
$this->publishable_key = $this->usesandboxapi ? $this->testPublishableKey : $this->livePublishableKey;
$this->title = $this->get_option('title');

$alt_image = $this->get_option('alternate_imageurl');
$this->icon = empty($alt_image) ? $this->url['assets'] . 'images/credits.png' : $alt_image;

$this->usesandboxapi = $this->get_option('sandbox') == 'yes';

$this->testApiKey = $this->get_option('test_api_key');
$this->liveApiKey = $this->get_option('live_api_key');
$this->testPublishableKey = $this->get_option('test_publishable_key');
$this->livePublishableKey = get_option('live_publishable_key');
$this->publishable_key = $this->usesandboxapi ? $this->testPublishableKey : $this->livePublishableKey;
$this->secret_key = $this->usesandboxapi ? $this->testApiKey : $this->liveApiKey;
$this->capture = strcmp($this->settings['capture'], 'yes') == 0;

$this->useUniquePaymentProfile = $this->get_option('enable_unique_profile') == 'yes';
$this->useInterval = $this->get_option('enable_interval') == 'yes';
$this->capture = $this->get_option('capture') == 'yes';

// tell WooCommerce to save options
add_action('woocommerce_update_options_payment_gateways_' . $this->id , array($this, 'process_admin_options'));
Expand All @@ -67,7 +72,7 @@ public function perform_ssl_check()
'<div class="error"><p>' .
sprintf(
__('%s sandbox testing is disabled and can performe live transactions but the <a href="%s">force SSL option</a> is disabled; your checkout is not secure! Please enable SSL and ensure your server has a valid SSL certificate.', 'striper'),
$this->GATEWAY_NAME,
$this->method_title,
admin_url('admin.php?page=wc-settings&tab=checkout')
) .
'</p></div>'
Expand All @@ -78,74 +83,70 @@ public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'type' => 'checkbox',
'title' => __('Enable/Disable', 'striper'),
'type' => 'checkbox',
'label' => __('Enable Credit Card Payment', 'striper'),
'default' => 'yes'
),
'capture' => array(
'type' => 'checkbox',
'title' => __('Auth & Capture', 'striper'),
'label' => __('Enable Authorization & Capture', 'striper'),
'default' => 'no'
),
'debug' => array(
'type' => 'checkbox',
'title' => __('Testing', 'striper'),
'label' => __('Turn on testing', 'striper'),
'default' => 'no'
),
'title' => array(
'type' => 'text',
'title' => array(
'title' => __('Title', 'striper'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'striper'),
'default' => __('Credit Card Payment', 'striper')
'default' => __('Credit Card Payment with Stripe', 'striper')
),
'test_api_key' => array(
'type' => 'text',
'test_api_key' => array(
'title' => __('Stripe API Test Secret key', 'striper'),
'type' => 'text',
'default' => ''
),
'test_publishable_key' => array(
'type' => 'text',
'title' => __('Stripe API Test Publishable key', 'striper'),
'type' => 'text',
'default' => ''
),
'live_api_key' => array(
'type' => 'text',
'title' => __('Stripe API Live Secret key', 'striper'),
'type' => 'text',
'default' => ''
),
'live_publishable_key' => array(
'type' => 'text',
'title' => __('Stripe API Live Publishable key', 'striper'),
'default' => ''
),
'alternate_imageurl' => array(
'type' => 'text',
'title' => __('Alternate Image to display on checkout, use fullly qualified url, served via https', 'striper'),
'default' => ''
),
'enable_interval' => array(
'capture' => array(
'title' => __('Auth & Capture', 'striper'),
'type' => 'checkbox',
'label' => __('Enable Authorization & Capture', 'striper'),
'default' => 'no'
),
'enable_interval' => array(
'title' => __('Enable Interval', 'striper'),
'type' => 'checkbox',
'label' => __('Use this only if nothing else is working', 'striper'),
'default' => 'no'
),
'enable_unique_profile' => array(
'type' => 'checkbox',
'title' => __('Enable Payment Profile Creation', 'striper'),
'type' => 'checkbox',
'label' => __('Use this to always create a Payment Profile in Stripe (always creates new profile, regardless of logged in user), and associate the charge with the profile. This allows you more easily identify order, credit, or even make an additional charge (from Stripe admin) at a later date.', 'striper'),
'default' => 'no'
),
'alternate_imageurl' => array(
'title' => __('Alternate Image to display on checkout', 'striper'),
'type' => 'text',
'description' => __('Use fullly qualified url, served via https', 'striper'),
'default' => ''
),
'sandbox' => array(
'title' => __('Testing', 'striper'),
'type' => 'checkbox',
'label' => __('Turn on testing with Stripe sandbox', 'striper'),
'default' => 'no'
),
);
}

public function admin_options()
{
include_once('templates/admin.php');
}

public function payment_fields()
{
$this->get_template('payment', array('publishable_key' => $this->publishable_key));
Expand All @@ -168,7 +169,7 @@ protected function send_to_stripe()
Stripe::setApiKey($this->secret_key);

// Get the credit card details submitted by the form
$data = $this->getRequestData();
$data = $this->get_request_data();

// Create the charge on Stripe's servers - this will charge the user's card
try {
Expand Down Expand Up @@ -225,7 +226,7 @@ public function process_payment($order_id)
$this->order = new WC_Order($order_id);
if ($this->send_to_stripe())
{
$this->completeOrder();
$this->complete_order();

$result = array(
'result' => 'success',
Expand All @@ -235,23 +236,23 @@ public function process_payment($order_id)
}
else
{
$this->markAsFailedPayment();
$this->mark_as_failed_payment();
$woocommerce->add_error(__('Transaction Error: Could not complete your payment'), 'striper');
}
}

protected function markAsFailedPayment()
protected function mark_as_failed_payment()
{
$this->order->add_order_note(
sprintf(
"%s Credit Card Payment Failed with message: '%s'",
$this->GATEWAY_NAME,
$this->method_title,
$this->transactionErrorMessage
)
);
}

protected function completeOrder()
protected function complete_order()
{
global $woocommerce;

Expand All @@ -264,14 +265,14 @@ protected function completeOrder()
$this->order->add_order_note(
sprintf(
"%s payment completed with Transaction Id of '%s'",
$this->GATEWAY_NAME,
$this->method_title,
$this->transactionId
)
);
}


protected function getRequestData()
protected function get_request_data()
{
if ($this->order AND $this->order != null)
{
Expand Down Expand Up @@ -357,8 +358,6 @@ function striper_order_status_completed($order_id = null)
}
}



function striper_add_creditcard_gateway($methods)
{
array_push($methods, 'Striper');
Expand All @@ -367,4 +366,3 @@ function striper_add_creditcard_gateway($methods)

add_filter('woocommerce_payment_gateways', 'striper_add_creditcard_gateway');
add_action('woocommerce_order_status_processing_to_completed', 'striper_order_status_completed' );

18 changes: 0 additions & 18 deletions templates/admin.php

This file was deleted.

0 comments on commit 67413f6

Please sign in to comment.