/
Icepay.php
189 lines (172 loc) · 5.38 KB
/
Icepay.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
<?php
namespace subdee\icepay;
use yii\base\Component;
/**
* Icepay provides a payment component for the Yii2 framework in order to initialise and complete payments using your
* Icepay account.
*
* To use Icepay, you should configure it in the application configuration like the following,
*
* ~~~
* 'components' => [
* ...
* 'icepay' => [
* 'class' => 'subdee\icepay\Icepay',
* 'merchantID' => 'merchant id',
* 'secretCode' => 'secretCode',
* ],
* ...
* ],
* ~~~
*
* By default, Icepay will take the application configuration for the language, country and currency properties.
* It uses the \Yii::$app->lanaguage and \Yii::$app->formatter->currencyCode properties to determine default values.
* To override the default values, you can specify those properties separately either in the config file:
*
* ~~~
* 'components' => [
* ...
* 'icepay' => [
* 'class' => 'subdee\icepay\Icepay',
* 'merchantID' => 'merchant id',
* 'secretCode' => 'secretCode',
* 'language' => 'EN',
* 'country' => 'GB',
* 'currency' => 'GBP'
* ],
* ...
* ],
* ~~~
*
* or by setting the property in your code:
*
* ~~~
* \Yii::$app->icepay->language = 'EN'
* ~~~
*
* @see http://www.icepay.com
*
* @author Kostas Thermos <info@subdee.org>
*/
class Icepay extends Component
{
public $merchantID;
public $secretCode;
public $language;
public $country;
public $currency;
/**
* Initialize application default values if not set
*/
public function init()
{
if (!$this->language || !$this->country || !$this->currency) {
list($language, $country) = explode('-', \Yii::$app->language);
if (!$this->language) {
$this->language = strtoupper($language);
}
if (!$this->country) {
$this->country = strtoupper($country);
}
if (!$this->currency) {
$this->currency = strtoupper(\Yii::$app->formatter->currencyCode);
}
}
}
/**
* Get an array of payment methods
* @return array The payment methods available for this merchant
* @throws \Exception
*/
public function getPaymentMethods()
{
try {
$ideal = new \Icepay_Webservice_Paymentmethods();
return $ideal
->setMerchantID($this->merchantID)
->setSecretCode($this->secretCode)
->retrieveAllPaymentmethods()
->asArray();
} catch (\Exception $e) {
\Yii::error($e->getMessage());
}
return [];
}
/**
* Create a new payment with Icepay
* @param string $method The method used for the payment as returned from the list of payments
* @param string $amount The amount of the payment
* @param string $orderID The order ID
* @param string|null $description A description of the payment
* @param string|null $issuer The issuer of the payment method, if applicable
* @param string|null $reference A reference for the payment
* @return bool|string The URL to redirect for the payment if successful, false if unsuccessful
* @throws \Exception
*/
public function createPayment($method, $amount, $orderID, $description = null, $issuer = null, $reference = null)
{
$methodName = '\Icepay_Paymentmethod_' . ucfirst(strtolower($method));
$paymentMethod = new $methodName();
$payment = new \Icepay_PaymentObject();
$payment->setPaymentMethod($paymentMethod->getCode())
->setAmount($amount)
->setOrderID($orderID)
->setDescription($description)
->setReference($reference)
->setLanguage($this->language)
->setCountry($this->country)
->setCurrency($this->currency)
->setIssuer($issuer);
$basicMode = \Icepay_Basicmode::getInstance();
$basicMode->setMerchantId($this->merchantID)
->setSecretCode($this->secretCode)
->setProtocol('https')
->validatePayment($payment);
try {
return $basicMode->getURL();
} catch (\Exception $e) {
\Yii::error($e->getMessage());
}
return false;
}
/**
* Do the postback validation and finish payment
* @return bool|\Icepay_Postback
*/
public function doPostback()
{
try {
$postback = new \Icepay_Postback();
$postback->setMerchantID($this->merchantID)
->setSecretCode($this->secretCode)
->doIPCheck();
if ($postback->validate() && $postback->getStatus() == \Icepay_StatusCode::SUCCESS) {
return $postback;
}
} catch (\Exception $e) {
\Yii::error($e->getMessage());
}
return false;
}
/**
* Handle the success or error result
* @return bool|\Icepay_Result
*/
public function handleResult()
{
try {
$result = new \Icepay_Result();
$result->setMerchantID($this->merchantID)
->setSecretCode($this->secretCode);
if (!$result->validate()) {
return false;
}
if ($result->getStatus() == \Icepay_StatusCode::SUCCESS) {
return $result;
}
} catch (\Exception $e) {
\Yii::error($e->getMessage());
}
return false;
}
}