forked from satispay/woocommerce-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc-satispay.php
213 lines (179 loc) · 7.72 KB
/
wc-satispay.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
if (!defined('ABSPATH')) {
exit;
}
require_once(dirname(__FILE__).'/satispay-sdk/init.php');
class WC_Satispay extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'satispay';
$this->method_title = __('Satispay', 'woo-satispay');
$this->order_button_text = __('Pay with Satispay', 'woo-satispay');
$this->method_description = __('With Satispay you can send money to friends and pay in stores from your phone. Free, simple, secure! #doitsmart', 'woo-satispay');
$this->has_fields = false;
$this->supports = array(
'products',
'refunds'
);
$this->title = $this->method_title;
$this->description = $this->method_description;
$this->icon = plugins_url('/logo.png', __FILE__);
$this->init_form_fields();
$this->init_settings();
add_action('woocommerce_update_options_payment_gateways_'.$this->id, array($this, 'process_admin_options'));
add_action('woocommerce_api_wc_gateway_'.$this->id, array($this, 'gateway_api'));
if ($this->get_option('sandbox') == 'yes') {
\SatispayGBusiness\Api::setSandbox(true);
}
\SatispayGBusiness\Api::setPublicKey($this->get_option('publicKey'));
\SatispayGBusiness\Api::setPrivateKey($this->get_option('privateKey'));
\SatispayGBusiness\Api::setKeyId($this->get_option('keyId'));
}
public function process_refund($order, $amount = null, $reason = '') {
$order = new WC_Order($order);
\SatispayGBusiness\Payment::create(array(
'flow' => 'REFUND',
'amount_unit' => round($amount * 100),
'currency' => (method_exists($order, 'get_currency')) ? $order->get_currency() : $order->order_currency,
'parent_payment_uid' => $order->get_transaction_id()
));
return true;
}
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'woo-satispay'),
'label' => __('Enable Satispay', 'woo-satispay'),
'type' => 'checkbox',
'default' => 'no'
),
'activationCode' => array(
'title' => __('Activation Code', 'woo-satispay'),
'type' => 'text',
'description' => sprintf(__('Get a six characters Activation Code from Online Shop section on <a href="%s" target="_blank">Satispay Dashboard</a>.', 'woo-satispay'), 'https://business.satispay.com')
),
// 'advanced' => array(
// 'title' => __( 'Advanced Options', 'woo-satispay' ),
// 'type' => 'title',
// 'description' => '',
// ),
'sandbox' => array(
'title' => __('Sandbox', 'woo-satispay'),
'label' => __('Sandbox Mode', 'woo-satispay'),
'type' => 'checkbox',
'default' => 'no',
'description' => sprintf(__('Sandbox Mode can be used to test payments. Request a <a href="%s" target="_blank">Sandbox Account</a>.', 'woo-satispay'), 'https://developers.satispay.com/docs/sandbox-account')
),
// 'debug' => array(
// 'title' => __('Debug Logs', 'woo-satispay'),
// 'label' => __('Enable Logs', 'woo-satispay'),
// 'type' => 'checkbox',
// 'default' => 'no',
// 'description' => sprintf(__('Log Satispay requests inside %s.<br />Note: this may log personal informations. We recommend using this for debugging purposes only and deleting the logs when finished.', 'woo-satispay'), '<code>'.WC_Log_Handler_File::get_log_file_path('satispay').'</code>')
// )
);
}
public function gateway_api() {
switch($_GET['action']) {
case 'redirect':
$payment = \SatispayGBusiness\Payment::get($_GET['payment_id']);
$order = new WC_Order($payment->metadata->order_id);
if ($payment->status === 'ACCEPTED') {
header('Location: '.$this->get_return_url($order));
} else {
\SatispayGBusiness\Payment::update($payment->id, array(
'action' => 'CANCEL'
));
header('Location: '.$order->get_cancel_order_url_raw());
}
break;
case 'callback':
$payment = \SatispayGBusiness\Payment::get($_GET['payment_id']);
$order = new WC_Order($payment->metadata->order_id);
if ($order->has_status(wc_get_is_paid_statuses())) {
exit;
}
if ($payment->status === 'ACCEPTED') {
$order->payment_complete($payment->id);
}
break;
}
}
public function process_admin_options() {
$activationCode = $this->get_option('activationCode');
$sandbox = $this->get_option('sandbox');
$postData = $this->get_post_data();
$newActivationCode = $postData['woocommerce_satispay_activationCode'];
$newSandbox = $postData['woocommerce_satispay_sandbox'];
if (!empty($newActivationCode) && $newActivationCode != $activationCode) {
if ($newSandbox == '1') {
\SatispayGBusiness\Api::setSandbox(true);
}
try {
$authentication = \SatispayGBusiness\Api::authenticateWithToken($newActivationCode);
$this->update_option('keyId', $authentication->keyId);
$this->update_option('privateKey', $authentication->privateKey);
$this->update_option('publicKey', $authentication->publicKey);
$this->update_option('activationCode', $newActivationCode);
\SatispayGBusiness\Api::setKeyId($authentication->keyId);
\SatispayGBusiness\Api::setPrivateKey($authentication->privateKey);
\SatispayGBusiness\Api::setPublicKey($authentication->publicKey);
} catch(\Exception $ex) {
echo '<div class="notice-error notice">';
echo '<p>'.sprintf(__('The Activation Code "%s" is invalid', 'woo-satispay'), $newActivationCode).'</p>';
echo '</div>';
}
} else if (empty($newActivationCode)) {
$this->update_option('keyId', '');
$this->update_option('privateKey', '');
$this->update_option('publicKey', '');
$this->update_option('activationCode', '');
}
return parent::process_admin_options();
}
public function admin_options() {
try {
\SatispayGBusiness\Payment::all();
} catch (\Exception $ex) {
echo '<div class="notice-error notice">';
echo '<p>'.sprintf(__('Satispay is not correctly configured, get an Activation Code from Online Shop section on <a href="%s" target="_blank">Satispay Dashboard</a>', 'woo-satispay'), 'https://business.satispay.com').'</p>';
echo '</div>';
}
return parent::admin_options();
}
public function is_available() {
if (!$this->enabled) {
return false;
}
return true;
}
public function process_payment($order_id) {
$order = wc_get_order($order_id);
$apiUrl = WC()->api_request_url('WC_Gateway_Satispay');
if (strpos($apiUrl, '?') !== FALSE) {
$callbackUrl = $apiUrl.'&action=callback&payment_id={uuid}';
$redirectUrl = $apiUrl.'&action=redirect&payment_id={uuid}';
} else {
$callbackUrl = $apiUrl.'?action=callback&payment_id={uuid}';
$redirectUrl = $apiUrl.'?action=redirect&payment_id={uuid}';
}
$payment = \SatispayGBusiness\Payment::create(array(
'flow' => 'MATCH_CODE',
'amount_unit' => $order->get_total() * 100,
'currency' => (method_exists($order, 'get_currency')) ? $order->get_currency() : $order->order_currency,
'callback_url' => $callbackUrl,
'external_code' => $order->get_id(),
'metadata' => array(
'order_id' => $order->get_id(),
'redirect_url' => $redirectUrl
)
));
$paymentUrl = 'https://online.satispay.com/pay/';
if ($this->get_option('sandbox') == 'yes') {
$paymentUrl = 'https://staging.online.satispay.com/pay/';
}
return array(
'result' => 'success',
'redirect' => $paymentUrl.$payment->id
);
}
}