-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathANZeGate.php
More file actions
157 lines (136 loc) · 5.36 KB
/
ANZeGate.php
File metadata and controls
157 lines (136 loc) · 5.36 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
<?php
/**
* Plugin Name: ANZ eGate Payment Gateway for WooCommerce
* Plugin URI: https://example.com
* Description: A WooCommerce payment gateway plugin for ANZ eGate Developed by Rukhsar Nasir.
* Version: 1.0.0
* Author: Rukhsar Nasir
* Author URI: https://www.linkedin.com/in/rukhsar-nasir/
* License: GPL2
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
// Hook into WooCommerce payment gateway filter
add_filter('woocommerce_payment_gateways', 'add_anzegate_gateway');
function add_anzegate_gateway($gateways)
{
$gateways[] = 'WC_Gateway_ANZeGate';
return $gateways;
}
// Include the main gateway class
add_action('plugins_loaded', 'initialize_anzegate_gateway');
function initialize_anzegate_gateway()
{
if (!class_exists('WC_Payment_Gateway')) {
return;
}
class WC_Gateway_ANZeGate extends WC_Payment_Gateway
{
public function __construct()
{
$this->id = 'anzegate';
$this->method_title = 'ANZ eGate';
$this->method_description = 'Accept payments using ANZ eGate payment gateway.';
// Define user-settable settings fields
$this->init_form_fields();
// Load settings
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->enabled = $this->get_option('enabled');
$this->test_mode = $this->get_option('test_mode');
$this->merchant_id = $this->get_option('merchant_id');
$this->access_code = $this->get_option('access_code');
$this->secure_hash = $this->get_option('secure_hash');
add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
add_action('woocommerce_api_wc_gateway_anzegate', [$this, 'handle_response']);
}
public function init_form_fields()
{
$this->form_fields = [
'enabled' => [
'title' => 'Enable/Disable',
'type' => 'checkbox',
'label' => 'Enable ANZ eGate Payment Gateway',
'default' => 'yes'
],
'title' => [
'title' => 'Title',
'type' => 'text',
'description' => 'Title that the user sees during checkout.',
'default' => 'Credit Card (ANZ eGate)',
'desc_tip' => true,
],
'description' => [
'title' => 'Description',
'type' => 'textarea',
'description' => 'Description that the user sees during checkout.',
'default' => 'Pay securely using your credit card through ANZ eGate.',
],
'merchant_id' => [
'title' => 'Merchant ID',
'type' => 'text',
'description' => 'Your ANZ eGate Merchant ID.',
'default' => '',
],
'access_code' => [
'title' => 'Access Code',
'type' => 'text',
'description' => 'Your ANZ eGate Access Code.',
'default' => '',
],
'secure_hash' => [
'title' => 'Secure Hash',
'type' => 'text',
'description' => 'Your ANZ eGate Secure Hash.',
'default' => '',
],
'test_mode' => [
'title' => 'Test Mode',
'type' => 'checkbox',
'label' => 'Enable Test Mode',
'default' => 'yes',
],
];
}
public function process_payment($order_id)
{
$order = wc_get_order($order_id);
$return_url = $this->get_return_url($order);
$amount = number_format($order->get_total(), 2, '.', '') * 100; // Convert to cents
$data = [
'vpc_Version' => '1',
'vpc_Command' => 'pay',
'vpc_Merchant' => $this->merchant_id,
'vpc_AccessCode' => $this->access_code,
'vpc_MerchTxnRef' => $order_id,
'vpc_OrderInfo' => $order->get_order_number(),
'vpc_Amount' => $amount,
'vpc_ReturnURL' => $return_url,
];
$data['vpc_SecureHash'] = $this->generate_secure_hash($data);
$redirect_url = 'https://migs.mastercard.com.au/vpcpay?' . http_build_query($data);
return [
'result' => 'success',
'redirect' => $redirect_url,
];
}
private function generate_secure_hash($data)
{
ksort($data);
$hash_string = '';
foreach ($data as $key => $value) {
if (substr($key, 0, 4) === 'vpc_' && $value !== '') {
$hash_string .= $key . '=' . $value . '&';
}
}
$hash_string = rtrim($hash_string, '&');
return strtoupper(hash_hmac('SHA256', $hash_string, pack('H*', $this->secure_hash)));
}
public function handle_response()
{
// Handle payment gateway response here
}
}
}