-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPricer.php
More file actions
173 lines (157 loc) · 4.46 KB
/
Pricer.php
File metadata and controls
173 lines (157 loc) · 4.46 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
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* The purpose of this class is to set a fixed-auto-converted price for your product or service.
* First, you need to convert the base price (your currency) to your user's currency. e.g, 1GBP to HKD
* Then after that you can already convert your price to your user's equivalent-currency price.
*
* API used for fetching latest currency is from exchangeratesapi.io
* API used for fetching user's details is from geoplugin.net
*/
class Pricer
{
// Your Properties
protected $myCurrencyCode;
protected $myPrice;
// Your User's Properties
protected $userConvertedPrice;
protected $userCurrencySymbol;
protected $userCurrencyCode;
protected $userCurrencyRate;
protected $userIPAddress;
protected $userCountry;
protected $userRegion;
/**
* @param String $cur
*/
public function setMyCurrencyCode(String $cur)
{
$this->myCurrencyCode = $cur;
}
/**
* @param Int $price
*/
public function setMyPrice(Int $price)
{
$this->myPrice = $price;
}
/**
* @return string
*/
public function getConvertedPrice()
{
// Turning on all the methods
self::setUserDetails();
self::setUserCurrencyRate();
// Formula : price * userCurrencyRate
$this->userConvertedPrice = $this->myPrice * $this->userCurrencyRate;
return number_format($this->userConvertedPrice, 2);
}
/**
* @return String
*/
public function getMyCurrencyCode()
{
return $this->myCurrencyCode;
}
/**
* @return Float
*/
public function getMyPrice()
{
return $this->myPrice;
}
/**
* @return String
*/
public function getUserCurrencyRate()
{
self::setUserCurrencyRate();
return $this->userCurrencyRate;
}
/**
* @return mixed
*/
public function getUserCurrencyCode()
{
self::setUserDetails();
return $this->userCurrencyCode;
}
/**
* @return mixed
*/
public function getUserCurrencySymbol()
{
self::setUserDetails();
return $this->userCurrencySymbol;
}
/**
* @return mixed
*/
public function getUserCountry()
{
self::setUserDetails();
return $this->userCountry;
}
/**
* @return mixed
*/
public function getUserRegion()
{
self::setUserDetails();
return $this->userRegion;
}
/**
* @return mixed
*/
public function getUserIP()
{
self::setUserDetails();
return $this->userIPAddress;
}
/**
* This will set all your user's details:
* IP, Currency Symbol, Currency Code, Country, and Region
*/
protected function setUserDetails()
{
// User's IP
$proxyPublicIP = filter_var(getenv('REMOTE_ADDR'), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
// Getting the user's details
$geo = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$proxyPublicIP));
// User's currency symbol
$this->userCurrencySymbol = $geo['geoplugin_currencySymbol'] ?? '*';
// User's currency code
$this->userCurrencyCode = $geo['geoplugin_currencyCode'];
// User's Country
$this->userCountry = $geo['geoplugin_countryName'];
// User's Region
$this->userRegion = $geo['geoplugin_region'];
// User's IP Add
$this->userIPAddress = $proxyPublicIP;
}
/**
* This will get the latest conversion between your curency and your user's currency
*
* For example, if you set your currency to GBP, and your user is from Hong Kong
* then setUserCurrencyRate() will call the API to get a conversion rate for 1GBP to HKD
*/
protected function setUserCurrencyRate()
{
// Get the latest exchange rate from ExchangeRate API (base is your own currency)
$url = 'https://api.exchangeratesapi.io/latest?base='.$this->myCurrencyCode.'&symbols='.$this->userCurrencyCode;
$retrievedData = json_decode(file_get_contents($url), true);
// Your user's rate casted from string to float
$this->userCurrencyRate = self::formatNumberNoRound($retrievedData['rates'][$this->userCurrencyCode]);
}
/**
* @param $num
* @return float
*/
protected function formatNumberNoRound($num)
{
$step1 = $num * 100;
$step2 = floor($step1);
$result = (float)($step2 / 100);
return $result;
}
}