1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class used to connecto the the Entersaleremotely API |
5
|
|
|
*@author nicolaas [at] sunnysideup.co.nz |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class EntersaleremotelyAPIConnector extends Object |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* REQUIRED! |
14
|
|
|
* @var String |
15
|
|
|
*/ |
16
|
|
|
private static $api_key = ""; |
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* REQUIRED! |
21
|
|
|
* @var String |
22
|
|
|
*/ |
23
|
|
|
private static $merchant_identifier = ""; |
|
|
|
|
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* sends the order data from the website to feefo using the Entersaleremotely API |
28
|
|
|
* |
29
|
|
|
* @param Order $order - the order to be assessed |
30
|
|
|
* @param int $delay - number of days to wait before feefo will send the feedback email |
31
|
|
|
* |
32
|
|
|
* @return array $messages - an array of message regarding the success of each curl request - one for each Order Item |
33
|
|
|
*/ |
34
|
|
|
public function sendOrderDataToFeefo($order, $delay = 0) |
35
|
|
|
{ |
36
|
|
|
$messages = []; |
37
|
|
|
//api details |
38
|
|
|
$apiKey = Config::inst()->get('EntersaleremotelyAPIConnector', 'api_key'); |
39
|
|
|
$merchant = Config::inst()->get('EntersaleremotelyAPIConnector', 'merchant_identifier'); |
40
|
|
|
|
41
|
|
|
//member specific details |
42
|
|
|
$member = $order->Member(); |
|
|
|
|
43
|
|
|
$email = $member->Email; |
44
|
|
|
$name = $member->FirstName; |
45
|
|
|
$locale = $member->Locale; |
46
|
|
|
$customerRef = $member->ID; |
47
|
|
|
|
48
|
|
|
//order specific details |
49
|
|
|
$orderRef = $order->ID; |
50
|
|
|
$currency = $order->CurrencyUsed()->Code; |
51
|
|
|
$dateAsString = strtotime($order->Created); |
52
|
|
|
$date = date('Y-m-d', $dateAsString); |
53
|
|
|
|
54
|
|
|
$feedbackDate = ''; |
55
|
|
|
if($delay){ |
56
|
|
|
$feedbackDate = date( 'Y-m-d', strtotime('+' . $delay . ' days', $dateAsString) ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($order->Items() as $orderItem) { |
60
|
|
|
$amount = $orderItem->CalculatedTotal; |
|
|
|
|
61
|
|
|
$product = $orderItem->Product(); |
62
|
|
|
$productTitle = $product->Title; |
63
|
|
|
$link = Director::absoluteURL($product->Link()); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$params = [ |
66
|
|
|
'apikey' => $apiKey, |
67
|
|
|
'merchantidentifier' => $merchant, |
68
|
|
|
'feedbackdate' => $feedbackDate, |
69
|
|
|
'email' =>$email, |
70
|
|
|
'name' => $name, |
71
|
|
|
'locale' => $locale, |
72
|
|
|
'customerref' => $customerRef, |
73
|
|
|
'orderref' => $orderRef, |
74
|
|
|
'date' => $date, |
75
|
|
|
'currency' => $currency, |
76
|
|
|
'amount ' => $orderItem->CalculatedTotal, |
77
|
|
|
'description' => $productTitle, |
78
|
|
|
'productsearchcode'=> $productTitle, |
79
|
|
|
'productlink' => 'https://www.picspeanutbutter.com/buy/buy-online/380g-smooth-no-salt/' |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$result = $this->sendCurlRequest($params); |
83
|
|
|
|
84
|
|
|
$result .= ' Order ID: ' . $orderRef . '; Product: ' . $productTitle . ';'; |
85
|
|
|
|
86
|
|
|
array_push($messages, $result); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $messages; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* performs the curl request |
94
|
|
|
* |
95
|
|
|
* @param array $params - the data to send to FEEFO |
96
|
|
|
* |
97
|
|
|
* @return string $reply |
98
|
|
|
*/ |
99
|
|
|
public function sendCurlRequest($params) |
100
|
|
|
{ |
101
|
|
|
$reply = ''; |
|
|
|
|
102
|
|
|
$url = 'https://api.feefo.com/api/entersaleremotely'; |
103
|
|
|
|
104
|
|
|
$data = http_build_query($params, '', '&'); |
105
|
|
|
|
106
|
|
|
$ch=curl_init(); |
107
|
|
|
|
108
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
109
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
110
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
111
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
112
|
|
|
|
113
|
|
|
$reply = curl_exec($ch); |
114
|
|
|
|
115
|
|
|
curl_close($ch); |
116
|
|
|
return $reply; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.