-
Notifications
You must be signed in to change notification settings - Fork 0
/
kundan-payment-gateway.php
179 lines (146 loc) · 6.73 KB
/
kundan-payment-gateway.php
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
172
173
174
175
176
177
178
179
<?php
// Either place this code in functions.php or create your custom plugin with this code
function wc_pay_on_account_add_to_gateways( $gateways ) { // Add Payment On Account Payment gateway
$current_user = wp_get_current_user();
$user_roles=$current_user->roles;
if ( in_array("wholesale_customer", $user_roles) || in_array("administrator", $user_roles)){ // Applicable only for user with role wholesale_customer or administrator
$gateways[] = 'WC_Gateway_pay_on_account';
}
return $gateways;
}
add_filter( 'woocommerce_payment_gateways', 'wc_pay_on_account_add_to_gateways' );
add_action( 'plugins_loaded', 'wc_pay_on_account_gateway_init', 11 ); // define Payment On Account Payment gateway
function wc_pay_on_account_gateway_init() {
$current_user = wp_get_current_user();
$user_roles=$current_user->roles;
if ( in_array("wholesale_customer", $user_roles) || in_array("administrator", $user_roles)){ // Applicable only for user with role wholesale_customer or administrator
class WC_Gateway_pay_on_account extends WC_Payment_Gateway {
/**
* Constructor for the gateway.
*/
public function __construct() {
$current_user = wp_get_current_user();
$user_roles=$current_user->roles;
$this->id = 'pay_on_account_gateway';
$this->icon = apply_filters('woocommerce_pay_on_account_icon', '');
$this->has_fields = false;
$this->method_title = __( 'Payment On Account', 'wc-gateway-pay_on_account' );
$this->method_description = __( 'This payment option is applicable only for Wholesale Customers.', 'wc-gateway-pay_on_account' );
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions', $this->description );
// Actions
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
// Customer Emails
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
}
/**
* Initialize Gateway Settings Form Fields
*/
public function init_form_fields() {
$current_user = wp_get_current_user();
$user_roles=$current_user->roles;
$this->form_fields = apply_filters( 'wc_pay_on_account_form_fields', array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'wc-gateway-pay_on_account' ),
'type' => 'checkbox',
'label' => __( 'Payment On Account', 'wc-gateway-pay_on_account' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'wc-gateway-pay_on_account' ),
'type' => 'text',
'description' => __( 'This controls the title for the payment method the customer sees during checkout.', 'wc-gateway-pay_on_account' ),
'default' => __( 'Payment On Account', 'wc-gateway-pay_on_account' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'wc-gateway-pay_on_account' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your checkout.', 'wc-gateway-pay_on_account' ),
'default' => __( 'You need to pay this payment with your monthly bill.', 'wc-gateway-pay_on_account' ),
'desc_tip' => true,
),
'instructions' => array(
'title' => __( 'Instructions', 'wc-gateway-pay_on_account' ),
'type' => 'textarea',
'description' => __( 'Instructions that will be added to the thank you page and emails.', 'wc-gateway-pay_on_account' ),
'default' => __( 'Just checkout with this option without payment. You need to pay it with your monthly bill.', 'wc-gateway-pay_on_account' ),
'desc_tip' => true,
),
) );
}
/**
* Output for the order received page.
*/
public function thankyou_page() {
if ( $this->instructions ) {
echo wpautop( wptexturize( $this->instructions ) );
}
}
/**
* Add content to the WC emails.
*
* @access public
* @param WC_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( $this->instructions && ! $sent_to_admin && $this->id === $order->payment_method && $order->has_status( 'pay_on_account' ) ) {
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
}
}
/**
* Process the payment and return the result
*
* @param int $order_id
* @return array
*/
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
// Mark as on-hold (we're awaiting the payment)
$order->update_status( 'pay_on_account', __( 'Payment On Account', 'wc-gateway-pay_on_account' ) );
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
WC()->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
} // end \WC_Gateway_pay_on_account class
}
}
function register_pay_on_account_order_status() { // Add Payment On Account order status
register_post_status( 'wc-pay_on_account', array(
'label' => 'Payment On Account',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Payment On Account <span class="count">(%s)</span>', 'Payment On Account <span class="count">(%s)</span>' )
) );
}
add_action( 'init', 'register_pay_on_account_order_status' );
// Add to list of WC Order statuses
function add_awaiting_shipment_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-pay_on_account'] = 'Payment On Account';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );
?>