Skip to content

Commit 561ebd0

Browse files
committed
Add support for automatic renewals with Subscriptions
1 parent c3534d1 commit 561ebd0

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Allows for minutes and hours-long Membership lengths for quicker testing.
2222

2323
### Bogus Gateway
2424

25-
Adds a testing payment gateway that will call the `$order->payment_complete()` method to simulate a credit card payment.
25+
Adds a testing payment gateway that will call the `$order->payment_complete()` method to simulate a credit card payment. Can also be used for Subscriptions automatic renewals.
2626

2727
### Global Functions
2828

@@ -45,6 +45,7 @@ Download and install just like any other WordPress plugin. If you want to be rea
4545
## Changelog
4646

4747
### 0.7.0 - 2017.04.12
48+
* Feature - Use the Bogus gateway for Subscriptions automatic renewals
4849
* Fix - Subscriptions integration throwing a warning in WooCommerce 3.0+
4950

5051
### 0.6.0 - 2017.02.18

includes/class-wc-dev-helper-bogus-gateway.php

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*
2424
* @since 0.6.0
2525
*/
26-
2726
class WC_Bogus_Gateway extends WC_Payment_Gateway {
2827

2928
/**
@@ -39,16 +38,40 @@ public function __construct() {
3938
$this->method_title = __( 'Bogus', 'woocommerce-dev-helper' );
4039
$this->method_description = __( 'A testing gateway that calls "payment complete" to simulate credit card transactions.', 'woocommerce-dev-helper' );
4140

42-
// Load the settings.
41+
// load the settings
4342
$this->init_form_fields();
4443
$this->init_settings();
4544

46-
// Define user set variables
47-
$this->title = $this->get_option( 'title' );
48-
$this->description = $this->get_option( 'description' );
49-
50-
// Actions
45+
// define user set variables
46+
$this->title = $this->get_option( 'title' );
47+
$this->description = $this->get_option( 'description' );
48+
$this->subscriptions = $this->get_option( 'subscriptions' );
49+
50+
if ( $this->subscriptions_available() ) {
51+
52+
// Subscriptions support
53+
$this->supports = array_merge( $this->supports,
54+
array(
55+
'subscriptions',
56+
'subscription_suspension',
57+
'subscription_cancellation',
58+
'subscription_reactivation',
59+
'subscription_amount_changes',
60+
'subscription_date_changes',
61+
'subscription_payment_method_change_customer',
62+
'subscription_payment_method_change_admin',
63+
'multiple_subscriptions',
64+
)
65+
);
66+
}
67+
68+
// Save settings
5169
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
70+
71+
// Process renewal orders
72+
if ( ! has_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'process_renewal_payment' ) ) ) {
73+
add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'process_renewal_payment' ), 10, 2 );
74+
}
5275
}
5376

5477

@@ -61,33 +84,52 @@ public function init_form_fields() {
6184

6285
$this->form_fields = apply_filters( 'wc_offline_form_fields', array(
6386

64-
'enabled' => array(
87+
'enabled' => array(
6588
'title' => __( 'Enable/Disable', 'woocommerce-dev-helper' ),
6689
'type' => 'checkbox',
6790
'label' => __( 'Enable Bogus Gateway', 'woocommerce-dev-helper' ),
6891
'default' => 'yes'
6992
),
7093

71-
'title' => array(
94+
'title' => array(
7295
'title' => __( 'Title', 'woocommerce-dev-helper' ),
7396
'type' => 'text',
7497
'description' => __( 'This controls the title for the payment method the customer sees during checkout.', 'woocommerce-dev-helper' ),
7598
'default' => __( 'Bogus (Test)', 'woocommerce-dev-helper' ),
7699
'desc_tip' => true,
77100
),
78101

79-
'description' => array(
102+
'description' => array(
80103
'title' => __( 'Description', 'woocommerce-dev-helper' ),
81104
'type' => 'textarea',
82105
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce-dev-helper' ),
83106
'default' => __( 'Nothingtodohere 🚀', 'woocommerce-dev-helper' ),
84107
'desc_tip' => true,
85108
),
86109

110+
'subscriptions' => array(
111+
'title' => __( 'Enable Subscriptions support?', 'woocommerce-dev-helper' ),
112+
'type' => 'checkbox',
113+
'description' => __( 'Makes the gateway available for subscriptions purchases', 'woocommerce-dev-helper' ),
114+
'default' => 'yes',
115+
'desc_tip' => true,
116+
),
117+
87118
) );
88119
}
89120

90121

122+
/**
123+
* Returns true if Subscriptions support is enabled.
124+
*
125+
* @since 0.7.0
126+
* @return bool
127+
*/
128+
public function subscriptions_available() {
129+
return 'yes' === $this->subscriptions;
130+
}
131+
132+
91133
/**
92134
* Process the payment and return the result.
93135
*
@@ -114,4 +156,20 @@ public function process_payment( $order_id ) {
114156
}
115157

116158

159+
/**
160+
* Processes a renewal payment automatically.
161+
*
162+
* @since 0.7.0
163+
* @param float $amount_to_charge subscription amount to charge, could include
164+
* multiple renewals if they've previously failed and the admin
165+
* has enabled it
166+
* @param WC_Order $order original order containing the subscription
167+
*/
168+
public function process_renewal_payment( $amount_to_charge, $order ) {
169+
170+
$order->payment_complete();
171+
$order->add_order_note( __( 'Renewal order processed. Bogus is always approved 😎', 'woocommerce-dev-helper' ) );
172+
}
173+
174+
117175
}

0 commit comments

Comments
 (0)