-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc-verifone-observer.php
139 lines (116 loc) · 4.08 KB
/
wc-verifone-observer.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
<?php
/**
* NOTICE OF LICENSE
*
* This source file is released under commercial license by Lamia Oy.
*
* @copyright Copyright (c) 2017 Lamia Oy (https://lamia.fi)
* @author Szymon Nosal <[email protected]>
*/
if (!defined('ABSPATH')) {
exit;
}
use \Verifone\Core\DependencyInjection\CoreResponse\PaymentResponseImpl;
use \Verifone\Core\DependencyInjection\CoreResponse\Interfaces\Card;
class WC_Verifone_Observer
{
/**
*
*/
public static function refreshSavedCards()
{
if (WC_Verifone_Config::getInstance()->isAllowToSaveCC()) {
try {
$response = WC_Verifone_Core_SavedPaymentMethods::getListSavedPaymentMethods();
if($response === null) {
return false;
}
$cards = $response->getBody();
if (count($cards)) {
foreach ($cards as $card) {
WC_Verifone_Core_SavedPaymentMethods::addCard($card);
}
}
} catch (Exception $e) {
// no cards
}
}
}
/**
* @param WC_Order $order
* @param PaymentResponseImpl $body
* @return array
*/
public static function addMaskedPanNumber($order, $body)
{
$config = WC_Verifone_Config::getInstance();
if (!$config->isSaveMaskedPanNumber()) {
return [$order, $body];
}
/** @var Card $card */
$card = $body->getCard();
if (
strlen($card->getFirst6()) &&
$card->getFirst6() &&
strlen($card->getLast2()) &&
$card->getLast2()
) {
if (empty($order->get_meta('masked_pan_number'))) {
$maskedPanNumber = $card->getFirst6() . '********' . $card->getLast2();
$order->add_meta_data('masked_pan_number', $maskedPanNumber);
}
return [$order, $body];
}
return [$order, $body];
}
/**
* Add a custom action to order actions select box on edit order page
* Only added for paid orders that haven't fired this action yet
*
* @param array $actions order actions array to display
* @return array - updated actions
*/
public static function addCheckOrderPaymentAction($actions)
{
/** @var WC_Order $theorder */
global $theorder;
if ($theorder->get_payment_method() != WC_VERIFONE_GATEWAY_ID) {
return $actions;
}
if ($theorder->is_paid()) {
return $actions;
}
$actions['verifone_check_order_payment'] = __('Verifone: Check payment status', WC_VERIFONE_DOMAIN);
return $actions;
}
public static function executeCheckOrderPaymentAction(WC_Order $order, $cron = false)
{
if ($order->is_paid()) {
return true;
}
$result = WC_Verifone_Payment::checkPaymentStatus($order, $cron);
if ($result) {
$status = $order->needs_processing() ? 'processing' : 'completed';
$message = sprintf(__('Order status is now [%s] because payment for this order was found.', WC_VERIFONE_DOMAIN), $status);
if (!$cron) {
WC_Verifone_Notice::addSuccess($message, false, true);
}
} elseif ($result === null) {
$message = __('Order status is now [pending] because payment for this order was NOT found.', WC_VERIFONE_DOMAIN);
if (!$cron) {
WC_Verifone_Notice::addInfo($message, false, true);
}
} else {
$message = __('Order status is now [cancel] because payment for this order was cancelled', WC_VERIFONE_DOMAIN);
if (!$cron) {
WC_Verifone_Notice::addError($message, false, true);
}
}
if (!$cron) {
$note = __('Manually check order payment status result: ', WC_VERIFONE_DOMAIN);
} else {
$note = __('Cron check order payment status result: ', WC_VERIFONE_DOMAIN);
}
$order->add_order_note($note . '<br>' . $message);
}
}