-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-travel-opayo.php
387 lines (318 loc) · 11.8 KB
/
wp-travel-opayo.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/*
Plugin Name: Wp Travel OPayO
Plugin URI: https://libertyware.io
Description: SagePay for WP Travel.
Version: 0.1.0
Author: Libertyware Limited
Author URI: https://libertyware.io
License: MIT
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
require_once plugin_dir_path(__FILE__) . 'vendor/autoload.php';
use Omnipay\Omnipay;
use Omnipay\Common\CreditCard;
use Omnipay\Common\ItemBag;
use Omnipay\SagePay\Extend\Item;
// WP Travel OPayO Checkout core.
if (!class_exists('WP_Travel_OPayO_Checkout_Core')) :
/**
* Core Class
*/
class WP_Travel_OPayO_Checkout_Core
{
const WP_TRAVEL_OPAYO_HANDLE = 'wp_travel_opayo_';
/**
* ABSPATH
*
* @var string $abspath
*/
protected static $abspath;
/**
* Plugin File Path
*
* @var string $plugin_file
*/
protected static $plugin_file;
/**
* Plugin File URL
*
* @var string $plugin_url
*/
protected static $plugin_url;
/**
* Plugin Version
*
* @var string $version
*/
protected static $version;
/**
* The single instance of the class.
*
* @var WP Travel OPayO Checkout Core
* @since 1.0.0
*/
protected static $_instance = null;
/**
* Main WP_Travel_OPayO_Checkout_Core Instance.
* Ensures only one instance of WP_Travel_OPayO_Checkout_Core is loaded or can be loaded.
*
* @since 1.0.0
* @static
* @see WP_Travel_OPayO_Checkout_Core()
* @return WP_Travel_OPayO_Checkout_Core - Main instance.
*/
public static function instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Init core.
*
* @param array $plugin_data Plagin data.
*/
public static function init($plugin_data)
{
self::$abspath = plugin_dir_path(__FILE__);
self::$plugin_file = __FILE__;
self::$plugin_url = plugin_dir_url(__FILE__);
self::$version = $plugin_data['version'];
self::includes();
add_action('wp_enqueue_scripts', array('WP_Travel_OPayO_Checkout_Core', 'frontend_assets'), 20);
add_action('admin_enqueue_scripts', array('WP_Travel_OPayO_Checkout_Core', 'admin_assets'), 20);
// Payment Gateway list.
add_filter('wp_travel_payment_gateway_lists', 'wp_travel_gateway_opayo');
add_filter('wp_travel_premium_addons_list', 'wp_travel_opayo_addons');
if (self::is_enabled()) {
add_filter('wp_travel_frontend_data', 'wp_travel_opayo_add_vars', 10, 2);
}
add_action('wp_travel_action_after_payment_info_field', array( 'WP_Travel_OPayO_Checkout_Core', 'add_extra_fields'));
add_action('wp_travel_dashboard_booking_after_detail', array( 'WP_Travel_OPayO_Checkout_Core', 'add_extra_fields'), 15, 2);
if (self::uses_opayo_checkout()) {
add_action('wp_travel_after_frontend_booking_save', array('WP_Travel_OPayO_Checkout_Core', 'process'), 20);
add_action('wp_travel_after_partial_payment_complete', array('WP_Travel_OPayO_Checkout_Core', 'process'), 20);
}
isset($_SESSION['used-opayo']) && $_SESSION['used-opayo'] && add_filter('wp_travel_booked_message', 'wp_travel_opayo_booking_message', 20);
}
public static function wp_travel_opayo_booking_message( $message ) {
unset( $_SESSION['used-opayo'] );
var_dump( $message );
$message = esc_html__( "We've received your booking and payment details. We'll contact you soon.", 'wp-travel-pro' );
return $message;
}
/**
* Determine if booking used express checkout.
*/
private static function uses_opayo_checkout()
{
return isset($_POST['wp_travel_booking_option']) && 'booking_with_payment' === $_POST['wp_travel_booking_option'] && 'POST' === $_SERVER['REQUEST_METHOD'] && array_key_exists('wp_travel_payment_gateway', $_REQUEST) && 'opayo' === $_REQUEST['wp_travel_payment_gateway'];
}
/**
* Determine if OPayO checkout is enabled.
*/
private static function is_enabled()
{
$settings = function_exists('wp_travel_get_settings') ? wp_travel_get_settings() : array();
return array_key_exists('payment_option_opayo', $settings) && 'yes' === $settings['payment_option_opayo'];
}
/**
* Determing is OPayO checkout is disabled.
*/
private static function is_disabled()
{
return !self::is_enabled();
}
/**
* OPayO Frontend assets.
*/
public static function frontend_assets()
{
if (wp_travel_can_load_payment_scripts() && self::is_enabled()) {
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
// Styles.
wp_enqueue_style('wp-travel-opayo-custom', self::$plugin_url . 'assets/css/custom.css', '', self::$version);
$dependencies = array('jquery', 'wp-travel-payment-frontend-script');
$dependencies[] = 'wp-travel-payment-frontend-script';
wp_enqueue_script('wp_travel_opayo_checkout_js', 'https://js.opayo.com/v3/', array('jquery'), self::$version, true);
wp_register_script('wp-travel-opayo-script', self::$plugin_url . 'assets/js/wp-travel-opayo.js', $dependencies, self::$version, true);
wp_localize_script(
'wp-travel-opayo-script',
'_wpTravelOPayOL10n',
array(
'brandLogo' => plugin_dir_url(__FILE__) . 'assets/img/opayo.png',
)
);
wp_enqueue_script('wp-travel-opayo-script');
}
}
/**
* Admin assets.
*/
public static function admin_assets()
{
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
$screen = get_current_screen();
$allowed_screen = array('edit-itineraries', WP_TRAVEL_POST_TYPE, 'itineraries_page_settings', 'itinerary-booking', 'itinerary-booking_page_settings');
$settings = function_exists('wp_travel_get_settings') ? wp_travel_get_settings() : array();
$wp_travel_react_switch = isset($settings['wp_travel_switch_to_react']) && 'yes' === $settings['wp_travel_switch_to_react'];
if ($wp_travel_react_switch) {
$screen = get_current_screen();
// settings_screen.
if ('itinerary-booking_page_settings2' == $screen->id) {
$deps = include_once sprintf('%sbuild/index.asset.php', plugin_dir_path(__FILE__));
$deps['dependencies'][] = 'jquery';
wp_enqueue_script(self::WP_TRAVEL_OPAYO_HANDLE . 'admin-settings', plugin_dir_url(__FILE__) . 'build/index.js', $deps['dependencies'], $deps['version'], true);
}
}
}
/**
* Include required core files used in admin and on the frontend.
*
* @return void
*/
public static function includes()
{
include sprintf('%sinc/functions.php', self::$abspath);
if (self::is_request('admin')) {
include sprintf('%sinc/admin/settings.php', self::$abspath);
}
}
/**
* Sets up payment options
*
* @param string $booking_id ID of booking.
* @return void
*/
public static function process($booking_id, $complete_partial_payment = false )
{
if (self::is_disabled()) {
return;
}
if (!self::uses_opayo_checkout()) {
return;
}
if (!$booking_id) {
return;
}
if (!isset($_POST['wp_travel_book_now'])) {
return;
}
do_action('wt_before_payment_process', $booking_id);
$settings = wp_travel_get_settings();
$is_test_mode = $settings['wt_test_mode'] === 'yes';
global $wt_cart;
$items = $wt_cart->getItems();
$cart_amounts = $wt_cart->get_total();
$cart_total = $cart_amounts['cart_total'];
$card = new CreditCard( self::get_buyer_details( $booking_id ) );
$gateway = OmniPay::create( 'SagePay\Form' )->initialize(
array(
'vendor' => $settings['opayo_vendor'],
'testMode' => $is_test_mode,
'encryptionKey' => $settings['opayo_encryption_key'],
'disableUtf8Decode' => true,
)
);
$return_url = self::get_return_url( $booking_id );
$request = $gateway->purchase(
array(
'currency' => $settings['currency'] ? $settings['currency'] : 'GBP',
'card' => $card,
'amount' => $cart_total,
'transactionId' => $booking_id,
'clientIp' => $_SERVER['HTTP_CLIENT_IP'] || $_SERVER['HTTP_X_FORWARDED_FOR'] || $_SERVER['REMOTE_ADDR'],
'description' => self::get_trip_title( $booking_id ),
'returnUrl' => add_query_arg(
array(
'booking_id' => $booking_id,
'booked' => true,
'status' => 'success',
'order_id' => $booking_id,
),
$return_url
),
'failureUrl' => add_query_arg(
array(
'booking_id' => $booking_id,
'booked' => true,
'status' => 'cancel',
),
$return_url
),
)
);
$_SESSION['used-opayo'] = true;
$response = $request->send();
$response->redirect();
// update_post_meta($payment_id, 'wp_travel_payment_gateway', $payment_method);
// wp_travel_update_payment_status($booking_id, $amount, 'paid', $detail, sprintf('_%s_args', $payment_method), $payment_id);
// $_SESSION['used-opayo'] = true;
// do_action('wp_travel_after_successful_payment', $booking_id);
}
private static function get_trip_title( $booking_id ) {
$trip_id = get_post_meta( (int) $booking_id, 'wp_travel_post_id', true );
$post = get_post( $trip_id );
return $post->post_title;
}
private static function get_return_url( $booking_id ) {
$trip_id = get_post_meta( (int) $booking_id, 'wp_travel_post_id', true );
$url = '';
if ( function_exists( 'wp_travel_thankyou_page_url' ) ) {
$url = wp_travel_thankyou_page_url( (int) $trip_id );
}
return $url;
}
private static function get_buyer_details( $booking_id ) {
$booking_data = get_post_meta( (int) $booking_id, 'order_data', true );
$args = array();
$key = self::get_form_index_key( $booking_id );
if ( $key ) :
// Buyer Details.
$args['firstName'] = isset( $booking_data['wp_travel_fname_traveller'][ $key ][0] ) ? $booking_data['wp_travel_fname_traveller'][ $key ][0] : '';
$args['lastName'] = isset( $booking_data['wp_travel_lname_traveller'][ $key ][0] ) ? $booking_data['wp_travel_lname_traveller'][ $key ][0] : '';
$args['email'] = isset( $booking_data['wp_travel_email_traveller'][ $key ][0] ) ? $booking_data['wp_travel_email_traveller'][ $key ][0] : '';
$args['billingAddress1'] = isset( $booking_data['wp_travel_address'] ) ? $booking_data['wp_travel_address'] : '';
$args['billingCity'] = isset( $booking_data['billing_city'] ) ? $booking_data['billing_city'] : '';
$args['billingPostcode'] = isset( $booking_data['billing_postal'] ) ? $booking_data['billing_postal'] : '';
$args['billingCountry'] = isset( $booking_data['wp_travel_country'] ) ? $booking_data['wp_travel_country'] : '';
$args['shippingAddress1'] = isset( $booking_data['wp_travel_address'] ) ? $booking_data['wp_travel_address'] : '';
$args['shippingCity'] = isset( $booking_data['billing_city'] ) ? $booking_data['billing_city'] : '';
$args['shippingPostcode'] = isset( $booking_data['billing_postal'] ) ? $booking_data['billing_postal'] : '';
$args['shippingCountry'] = isset( $booking_data['wp_travel_country'] ) ? $booking_data['wp_travel_country'] : '';
endif;
return $args;
}
private static function get_form_index_key( $booking_id ) {
$order_details = get_post_meta( (int) $booking_id, 'order_items_data', true ); // Multiple Trips.
$index = is_array( $order_details ) && count( $order_details ) > 0 ? array_keys( $order_details )[0] : null;
return $index;
}
/**
* What type of request is this?
*
* @param string $type admin, ajax, cron or frontend.
* @return bool
*/
private static function is_request($type)
{
switch ($type) {
case 'admin':
return is_admin();
case 'ajax':
return defined('DOING_AJAX');
case 'cron':
return defined('DOING_CRON');
case 'frontend':
return (!is_admin() || defined('DOING_AJAX')) && !defined('Doing_cRON');
}
}
}
endif;
$opayo_init = WP_Travel_OPayO_Checkout_Core::init(array('version' => '0.0.1'));
$opayo = WP_Travel_OPayO_Checkout_Core::instance();