Skip to content

Commit 1b54009

Browse files
committed
Add Bogus Gateway for testing
1 parent 2112c84 commit 1b54009

3 files changed

Lines changed: 176 additions & 2 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Allows minutes and hours-long Subscription periods for quicker testing.
2020

2121
Allows minutes and hours-long Membership lengths for quicker testing.
2222

23+
### Bogus Gateway
24+
25+
Adds a testing payment gateway that will call the `$order->payment_complete()` method to simulate a credit card payment.
26+
2327
### Global Functions
2428

2529
* `wp_debug_backtrace()` - helper for using the `debug_backtrace()` function with a bit more sanity
@@ -40,6 +44,9 @@ Download and install just like any other WordPress plugin. If you want to be rea
4044

4145
## Changelog
4246

47+
### 0.6.0 - 2017.02.18
48+
* Feature - Adds a bogus gateway that calls `$order->payment_complete()` when used
49+
4350
### 0.5.0 - 2017.01.19
4451
* Feature - Dump the current session in AJAX to display in browser console
4552

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* WooCommerce Dev Helper
4+
*
5+
* This source file is subject to the GNU General Public License v3.0
6+
* that is bundled with this package in the file license.txt.
7+
* It is also available through the world-wide-web at this URL:
8+
* http://www.gnu.org/licenses/gpl-3.0.html
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to license@skyverge.com so we can send you a copy immediately.
12+
*
13+
* @package WC-Dev-Helper/Classes
14+
* @author SkyVerge
15+
* @copyright Copyright (c) 2015-2017, SkyVerge, Inc.
16+
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
17+
*/
18+
19+
defined( 'ABSPATH' ) or exit;
20+
21+
/**
22+
* Adds a testing gateway that calls the WooCommerce payment_complete() method.
23+
*
24+
* @since 0.6.0
25+
*/
26+
27+
class WC_Bogus_Gateway extends WC_Payment_Gateway {
28+
29+
/**
30+
* Constructor for the gateway.
31+
*
32+
* @since 0.6.0
33+
*/
34+
public function __construct() {
35+
36+
$this->id = 'bogus_gateway';
37+
$this->icon = apply_filters('woocommerce_bogus_icon', '');
38+
$this->has_fields = false;
39+
$this->method_title = __( 'Bogus', 'woocommerce-dev-helper' );
40+
$this->method_description = __( 'A testing gateway that calls "payment complete" to simulate credit card transactions.', 'woocommerce-dev-helper' );
41+
42+
// Load the settings.
43+
$this->init_form_fields();
44+
$this->init_settings();
45+
46+
// Define user set variables
47+
$this->title = $this->get_option( 'title' );
48+
$this->description = $this->get_option( 'description' );
49+
50+
// Actions
51+
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
52+
}
53+
54+
55+
/**
56+
* Initialize gateway settings form fields.
57+
*
58+
* @since 0.6.0
59+
*/
60+
public function init_form_fields() {
61+
62+
$this->form_fields = apply_filters( 'wc_offline_form_fields', array(
63+
64+
'enabled' => array(
65+
'title' => __( 'Enable/Disable', 'woocommerce-dev-helper' ),
66+
'type' => 'checkbox',
67+
'label' => __( 'Enable Bogus Gateway', 'woocommerce-dev-helper' ),
68+
'default' => 'yes'
69+
),
70+
71+
'title' => array(
72+
'title' => __( 'Title', 'woocommerce-dev-helper' ),
73+
'type' => 'text',
74+
'description' => __( 'This controls the title for the payment method the customer sees during checkout.', 'woocommerce-dev-helper' ),
75+
'default' => __( 'Bogus (Test)', 'woocommerce-dev-helper' ),
76+
'desc_tip' => true,
77+
),
78+
79+
'description' => array(
80+
'title' => __( 'Description', 'woocommerce-dev-helper' ),
81+
'type' => 'textarea',
82+
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce-dev-helper' ),
83+
'default' => __( 'Nothingtodohere &#128640;', 'woocommerce-dev-helper' ),
84+
'desc_tip' => true,
85+
),
86+
87+
) );
88+
}
89+
90+
91+
/**
92+
* Process the payment and return the result.
93+
*
94+
* @since 0.6.0
95+
* @param int $order_id
96+
* @return array
97+
*/
98+
public function process_payment( $order_id ) {
99+
100+
$order = wc_get_order( $order_id );
101+
102+
// Update order status and add a transaction note
103+
$order->payment_complete();
104+
$order->add_order_note( __( 'Bogus is always approved &#128526;', 'woocommerce-dev-helper' ) );
105+
106+
// Remove cart
107+
WC()->cart->empty_cart();
108+
109+
// Return thank you redirect
110+
return array(
111+
'result' => 'success',
112+
'redirect' => $this->get_return_url( $order )
113+
);
114+
}
115+
116+
117+
}

woocommerce-dev-helper.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Description: A simple plugin for helping develop/debug WooCommerce & extensions
66
* Author: SkyVerge
77
* Author URI: http://www.skyverge.com
8-
* Version: 0.5.0
8+
* Version: 0.6.0
99
* Text Domain: woocommerce-dev-helper
1010
* Domain Path: /i18n/languages/
1111
*
@@ -41,6 +41,9 @@ class WC_Dev_Helper {
4141
/** @var \WC_Dev_Helper_Memberships instance */
4242
protected $memberships;
4343

44+
/** @var \WC_Bogus_Gateway instance */
45+
protected $gateway;
46+
4447

4548
/**
4649
* Bootstrap class
@@ -66,6 +69,10 @@ public function __construct() {
6669

6770
// add some inline JS
6871
add_action( 'wp_footer', array( $this, 'enqueue_scripts' ) );
72+
add_action( 'wp_head', array( $this, 'bogus_gateway_styles' ) );
73+
74+
// add the testing gateway
75+
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_bogus_gateway' ) );
6976
}
7077

7178

@@ -110,6 +117,19 @@ function wc_dev_get_session() {
110117
}
111118

112119

120+
/**
121+
* Add the bogus gateway to WC available gateways.
122+
*
123+
* @since 0.6.0
124+
* @param array $gateways all available WC gateways
125+
* @return array updated gateways
126+
*/
127+
function add_bogus_gateway( $gateways ) {
128+
$gateways[] = 'WC_Bogus_Gateway';
129+
return $gateways;
130+
}
131+
132+
113133
/**
114134
* Include required files
115135
*
@@ -118,8 +138,10 @@ function wc_dev_get_session() {
118138
public function includes() {
119139

120140
require_once( $this->get_plugin_path() . '/includes/class-wc-dev-helper-ajax.php' );
141+
require_once( $this->get_plugin_path() . '/includes/class-wc-dev-helper-bogus-gateway.php' );
121142
if ( $this->is_plugin_active( 'woocommerce.php' ) && is_ajax() ) {
122-
$this->ajax = new WC_Dev_Helper_Ajax();
143+
$this->ajax = new WC_Dev_Helper_Ajax();
144+
$this->gateway = new WC_Bogus_Gateway();
123145
}
124146

125147
// use forwarded URLs
@@ -228,6 +250,23 @@ public function is_plugin_active( $plugin_name ) {
228250
}
229251

230252

253+
/**
254+
* Adjust Bogus Gateway styles
255+
*
256+
* @since 0.6.0
257+
*/
258+
public function bogus_gateway_styles() {
259+
260+
if ( is_checkout() || is_checkout_pay_page() ) {
261+
echo '<style type="text/css">
262+
#payment .payment_methods li.payment_method_bogus_gateway img {
263+
float: none !important;
264+
}
265+
</style>';
266+
}
267+
}
268+
269+
231270
/** Instance Getters ******************************************************/
232271

233272

@@ -264,6 +303,17 @@ public function memberships() {
264303
}
265304

266305

306+
/**
307+
* Return the gateway class instance
308+
*
309+
* @since 0.6.0
310+
* @return \WC_Bogus_Gateway
311+
*/
312+
public function gateway() {
313+
return $this->gateway();
314+
}
315+
316+
267317
/** Housekeeping **********************************************************/
268318

269319

0 commit comments

Comments
 (0)