AffinityEcommerceUtils.php
5.12 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
158
159
160
161
<?php
class AffinityEcommerceUtils {
const SERVICE_MAX_TRIALS = 3;
const SERVICE_SECONDS_BEFORE_TRYING_AGAIN = 5;
const SERVICE_TIMEOUT = 600; //in seconds
public static function getCallbackUrl() {
return admin_url('admin-ajax.php');
}
public static function getStoreUrl() {
return get_site_url();
}
public static function getAdminEmail() {
return get_option('admin_email');
}
public static function isHttpsBeingUsed() {
return is_ssl();
}
public static function redirectToAffinityAuthenticationPage() {
$authUrl = admin_url('admin.php?page=ebay-sync-settings');
wp_redirect($authUrl);
exit();
}
public static function generateSecureRandomString() {
if(function_exists('openssl_random_pseudo_bytes')) {
return bin2hex(openssl_random_pseudo_bytes(16));
}
if (function_exists('random_bytes')) {
$return = bin2hex(random_bytes(16));
} else {
set_time_limit(600);
require_once(__DIR__ . "/../service/phpseclib/Crypt/Random.php");
$return = bin2hex(crypt_random_string(16));
}
return $return;
}
public static function callMethodWithJsonContent($url, $arrParameters, $extraArgs = array()) {
require_once(__DIR__ . "/../model/AffinityLog.php");
require_once(__DIR__ . "/../model/AffinityGlobalOptions.php");
require_once(__DIR__ . "/../service/AffinityEnc.php");
$method = $extraArgs['method'];
if (empty($arrParameters)) {
$arrParameters = array();
}
if (!class_exists('WP_Http')) {
require_once(ABSPATH . WPINC. '/class-http.php');
}
$token = AffinityEnc::getToken();
$defaultOpts = array(
'method' => 'GET',
'httpversion' => "1.0",
'headers' => array(
"Content-type" => "application/json",
"Accept" => "application/json",
"Authorization" => "Bearer " . $token,
"X-EBAY-AFFINITY-PARTNER-ID" => AffinityGlobalOptions::getInstallationId(),
"Expect" => ''
),
'timeout' => self::SERVICE_TIMEOUT,
'sslverify' => false
// 'sslcertificates' => __DIR__ . "/ebay.crt" @Todo, make sure production services use ssl certificates
);
$options = array_merge($defaultOpts, $extraArgs);
$finalUrl = $url;
if ($method === "DELETE" && count($arrParameters) > 0) {
$method = 'POST';
}
switch($method) {
case "POST":
case "PUT":
unset($arrParameters['access_token']);
$options['body'] = json_encode($arrParameters);
break;
default:
$urlParameters = http_build_query($arrParameters);
if(!empty($urlParameters)) {
$finalUrl = $url . '?' . $urlParameters;
}
}
$optionsWithoutHeaders = $options;
unset($optionsWithoutHeaders['headers']);
AffinityLog::saveLog(AffinityLog::TYPE_DEBUG, "Sending HTTP Request", "URL: $finalUrl<br>Options: " . print_r($optionsWithoutHeaders, true));
$requestCount = 1;
$result = wp_remote_request($finalUrl, $options);
while($requestCount < self::SERVICE_MAX_TRIALS && is_wp_error($result)) {
AffinityLog::saveLog(AffinityLog::TYPE_DEBUG, "HTTP Call Failed - Trying again $requestCount", "No details");
sleep(self::SERVICE_SECONDS_BEFORE_TRYING_AGAIN);
$requestCount += 1;
$result = wp_remote_request($finalUrl, $options);
}
if(is_wp_error($result)) {
AffinityLog::saveLog(AffinityLog::TYPE_ERROR, "Connection to eBay Failed", "After trying to connect " . self::SERVICE_MAX_TRIALS . " times, we couldn't succeed and gave up. Please check your connection and WordPress site conectivity to Internet.");
self::sendNotificationMail(self::getAdminEmail(), 'Connection to eBay Failed', "After trying to connect " . self::SERVICE_MAX_TRIALS . " times, we couldn't succeed and gave up. Please check your connection and WordPress site conectivity to Internet.");
return false;
}
$return = array(
'headers' => $result["headers"],
'httpResponseCode' => $result["response"]["code"],
'arrResult' => json_decode($result["body"], true)
);
if (is_array($return['headers'])) {
$arr = $return['headers'];
} else {
$arr = $return['headers']->getAll();
}
if (!empty($arr['rlogid'])) {
$return['rlogid'] = $arr['rlogid'];
}
unset($return['headers']);
$returnWithoutHeaders = $return;
unset($returnWithoutHeaders['headers']);
AffinityLog::saveLog(AffinityLog::TYPE_DEBUG, "Json Post Returning Result", "Return: " . print_r($returnWithoutHeaders, true));
return $return;
}
public static function sendNotificationMail($to, $subject, $messageContent) {
$messageWithStyledParagraphs = self::nl2p($messageContent, 'style="margin-top: 20px; margin-bottom: 20px; font-family: \'Helvetica Neue\', \'Open sans\', \'sans-serif\', \'Helvetica\'; color: #868686; font-size: 15px; line-height: 27px;"');
$htmlTemplate = file_get_contents(__DIR__ . "/../includes/email-template.html");
$htmlRenderedTemplate = str_replace("{{content}}", $messageWithStyledParagraphs, $htmlTemplate);
wp_mail( $to, $subject, $htmlRenderedTemplate, array('Content-Type: text/html; charset=UTF-8') );
}
private static function nl2p($string, $paragraphExtraAttributes) {
$return = '';
foreach(explode("\n", $string) as $line) {
if (trim($line)) {
$return .= "<p $paragraphExtraAttributes>$line</p>";
}
}
return $return;
}
}