class-wcs-change-payment-method-admin.php
6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Class to handle everything to do with changing a payment method for a subscription on the
* edit subscription admin page.
*
* @class WCS_Change_Payment_Method_Admin
* @version 2.0
* @package WooCommerce Subscriptions/Includes
* @category Class
* @author Prospress
*/
class WCS_Change_Payment_Method_Admin {
/**
* Display the edit payment gateway option under
*
* @since 2.0
*/
public static function display_fields( $subscription ) {
$payment_method = ! empty( $subscription->payment_method ) ? $subscription->payment_method : '';
$valid_payment_methods = self::get_valid_payment_methods( $subscription );
if ( ! $subscription->is_manual() && ! isset( $valid_payment_methods[ $payment_method ] ) ) {
$subscription_payment_gateway = WC_Subscriptions_Payment_Gateways::get_payment_gateway( $payment_method );
if ( false != $subscription_payment_gateway ) {
$valid_payment_methods[ $payment_method ] = $subscription_payment_gateway->title;
}
}
echo '<p class="form-field form-field-wide">';
if ( count( $valid_payment_methods ) > 1 ) {
$found_method = false;
echo '<label>' . esc_html__( 'Payment Method', 'woocommerce-subscriptions' ) . ':</label>';
echo '<select class="wcs_payment_method_selector" name="_payment_method" id="_payment_method" class="first">';
foreach ( $valid_payment_methods as $gateway_id => $gateway_title ) {
echo '<option value="' . esc_attr( $gateway_id ) . '" ' . selected( $payment_method, $gateway_id, false ) . '>' . esc_html( $gateway_title ) . '</option>';
if ( $payment_method == $gateway_id ) {
$found_method = true;
}
}
echo '</select>';
} elseif ( count( $valid_payment_methods ) == 1 ) {
echo '<strong>' . esc_html__( 'Payment Method', 'woocommerce-subscriptions' ) . ':</strong><br/>' . esc_html( current( $valid_payment_methods ) );
echo '<img class="help_tip" data-tip="Gateway ID: [' . esc_attr( key( $valid_payment_methods ) ) . ']" src="' . esc_url( WC()->plugin_url() ) . '/assets/images/help.png" height="16" width="16" />';
echo '<input type="hidden" value="' . esc_attr( key( $valid_payment_methods ) ) . '" id="_payment_method" name="_payment_method">';
}
echo '</p>';
$payment_method_table = apply_filters( 'woocommerce_subscription_payment_meta', array(), $subscription );
if ( is_array( $payment_method_table ) ) {
foreach ( $payment_method_table as $payment_method_id => $payment_method_meta ) {
echo '<div class="wcs_payment_method_meta_fields" id="wcs_' . esc_attr( $payment_method_id ) . '_fields" ' . ( ( $payment_method_id != $payment_method || $subscription->is_manual() ) ? 'style="display:none;"' : '' ) .' >';
foreach ( $payment_method_meta as $meta_table => $meta ) {
foreach ( $meta as $meta_key => $meta_data ) {
$field_id = sprintf( '_payment_method_meta[%s][%s]', $meta_table , $meta_key );
$field_label = ( ! empty( $meta_data['label'] ) ) ? $meta_data['label'] : $meta_key ;
$field_value = ( ! empty( $meta_data['value'] ) ) ? $meta_data['value'] : null ;
$field_disabled = ( isset( $meta_data['disabled'] ) && true == $meta_data['disabled'] ) ? ' readonly' : '';
echo '<p class="form-field form-field-wide">';
echo '<label for="' . esc_attr( $field_id ) . '">' . esc_html( $field_label ) . '</label>';
echo '<input type="text" class="short" name="' . esc_attr( $field_id ) . '" id="' . esc_attr( $field_id ) . '" value="' . esc_attr( $field_value ) . '" placeholder="" ' . esc_attr( $field_disabled ) . '>';
echo '</p>';
}
}
echo '</div>';
}
}
wp_nonce_field( 'wcs_change_payment_method_admin', '_wcsnonce' );
}
/**
* Get the new payment data from POST and check the new payment method supports
* the new admin change hook.
*
* @since 2.0
* @param $subscription WC_Subscription
*/
public static function save_meta( $subscription ) {
if ( empty( $_POST['_wcsnonce'] ) || ! wp_verify_nonce( $_POST['_wcsnonce'], 'wcs_change_payment_method_admin' ) ) {
return;
}
$payment_gateways = WC()->payment_gateways->payment_gateways();
$payment_method = isset( $_POST['_payment_method'] ) ? wc_clean( $_POST['_payment_method'] ) : '';
$payment_method_meta = apply_filters( 'woocommerce_subscription_payment_meta', array(), $subscription );
$payment_method_meta = ( ! empty( $payment_method_meta[ $payment_method ] ) ) ? $payment_method_meta[ $payment_method ] : array();
$valid_payment_methods = self::get_valid_payment_methods( $subscription );
if ( ! isset( $valid_payment_methods[ $payment_method ] ) ) {
throw new Exception( __( 'Please choose a valid payment gateway to change to.', 'woocommerce-subscriptions' ) );
}
if ( ! empty( $payment_method_meta ) ) {
foreach ( $payment_method_meta as $meta_table => &$meta ) {
if ( ! is_array( $meta ) ) {
continue;
}
foreach ( $meta as $meta_key => &$meta_data ) {
$meta_data['value'] = isset( $_POST['_payment_method_meta'][ $meta_table ][ $meta_key ] ) ? $_POST['_payment_method_meta'][ $meta_table ][ $meta_key ] : '';
}
}
}
$payment_gateway = ( 'manual' != $payment_method ) ? $payment_gateways[ $payment_method ] : '';
if ( ! $subscription->is_manual() && property_exists( $subscription->payment_gateway, 'id' ) && ( '' == $payment_gateway || ( $subscription->payment_gateway->id != $payment_gateway->id ) ) ) {
// Before updating to a new payment gateway make sure the subscription status is updated with the current gateway
$gateway_status = apply_filters( 'wcs_gateway_status_payment_changed', 'cancelled', $subscription, $payment_gateway );
WC_Subscriptions_Payment_Gateways::trigger_gateway_status_updated_hook( $subscription, $gateway_status );
}
$subscription->set_payment_method( $payment_gateway, $payment_method_meta );
}
/**
* Get a list of possible gateways that a subscription could be changed to by admins.
*
* @since 2.0
* @param $subscription int | WC_Subscription
* @return
*/
public static function get_valid_payment_methods( $subscription ) {
if ( ! $subscription instanceof WC_Subscription ) {
$subscription = wcs_get_subscription( $subscription );
}
$valid_gateways = array( 'manual' => __( 'Manual Renewal', 'woocommerce-subscriptions' ) );
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
foreach ( $available_gateways as $gateway_id => $gateway ) {
if ( $gateway->supports( 'subscription_payment_method_change_admin' ) && 'no' == get_option( WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no' ) || ( ! $subscription->is_manual() && $gateway_id == $subscription->payment_method ) ) {
$valid_gateways[ $gateway_id ] = $gateway->get_title();
}
}
return $valid_gateways;
}
}