1 | <?php |
||
2 | |||
3 | namespace Pronamic\WordPress\Pay\Gateways\MultiSafepay; |
||
4 | |||
5 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
6 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
7 | use Pronamic\WordPress\Pay\Core\Server; |
||
8 | use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\DirectTransactionRequestMessage; |
||
9 | use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionRequestMessage; |
||
10 | use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusRequestMessage; |
||
11 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
12 | |||
13 | /** |
||
14 | * Title: MultiSafepay Connect gateay |
||
15 | * Description: |
||
16 | * Copyright: 2005-2020 Pronamic |
||
17 | * Company: Pronamic |
||
18 | * |
||
19 | * @author Remco Tolsma |
||
20 | * @version 2.0.3 |
||
21 | * @since 1.0.1 |
||
22 | */ |
||
23 | class Gateway extends Core_Gateway { |
||
24 | /** |
||
25 | * Client. |
||
26 | * |
||
27 | * @var Client |
||
28 | */ |
||
29 | protected $client; |
||
30 | |||
31 | /** |
||
32 | * Config |
||
33 | * |
||
34 | * @var Config |
||
35 | */ |
||
36 | protected $config; |
||
37 | |||
38 | /** |
||
39 | * Constructs and initializes an MultiSafepay Connect gateway |
||
40 | * |
||
41 | * @param Config $config Config. |
||
42 | */ |
||
43 | 1 | public function __construct( Config $config ) { |
|
44 | 1 | parent::__construct( $config ); |
|
45 | |||
46 | 1 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
|
47 | |||
48 | // Supported features. |
||
49 | 1 | $this->supports = array( |
|
50 | 'payment_status_request', |
||
51 | ); |
||
52 | |||
53 | // Client. |
||
54 | 1 | $this->client = new Client(); |
|
55 | |||
56 | 1 | $this->client->api_url = $config->api_url; |
|
57 | 1 | } |
|
58 | |||
59 | /** |
||
60 | * Get iDEAL issuers |
||
61 | * |
||
62 | * @see Core_Gateway::get_issuers() |
||
63 | * @since 1.2.0 |
||
64 | */ |
||
65 | 1 | public function get_issuers() { |
|
66 | 1 | $groups = array(); |
|
67 | |||
68 | // Merchant. |
||
69 | 1 | $merchant = new Merchant(); |
|
70 | 1 | $merchant->account = $this->config->account_id; |
|
71 | 1 | $merchant->site_id = $this->config->site_id; |
|
72 | 1 | $merchant->site_secure_code = $this->config->site_code; |
|
73 | |||
74 | 1 | $result = $this->client->get_ideal_issuers( $merchant ); |
|
75 | |||
76 | 1 | if ( $result ) { |
|
77 | 1 | $groups[] = array( |
|
78 | 1 | 'options' => $result, |
|
79 | ); |
||
80 | } |
||
81 | |||
82 | 1 | return $groups; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Get credit card issuers |
||
87 | * |
||
88 | * @see Core_Gateway::get_credit_card_issuers() |
||
89 | */ |
||
90 | public function get_credit_card_issuers() { |
||
91 | $groups[] = array( |
||
92 | 'options' => array( |
||
93 | Methods::AMEX => _x( 'AMEX', 'Payment method name', 'pronamic_ideal' ), |
||
94 | Methods::MAESTRO => _x( 'Maestro', 'Payment method name', 'pronamic_ideal' ), |
||
95 | Methods::MASTERCARD => _x( 'MASTER', 'Payment method name', 'pronamic_ideal' ), |
||
96 | Methods::VISA => _x( 'VISA', 'Payment method name', 'pronamic_ideal' ), |
||
97 | ), |
||
98 | ); |
||
99 | |||
100 | return $groups; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Get payment methods |
||
105 | * |
||
106 | * @see Pronamic_WP_Pay_Gateway::get_payment_methods() |
||
107 | */ |
||
108 | public function get_available_payment_methods() { |
||
109 | $payment_methods = array(); |
||
110 | |||
111 | // Merchant. |
||
112 | $merchant = new Merchant(); |
||
113 | $merchant->account = $this->config->account_id; |
||
114 | $merchant->site_id = $this->config->site_id; |
||
115 | $merchant->site_secure_code = $this->config->site_code; |
||
116 | |||
117 | // Customer. |
||
118 | $customer = new Customer(); |
||
119 | |||
120 | // Get gateways. |
||
121 | try { |
||
122 | $result = $this->client->get_gateways( $merchant, $customer ); |
||
123 | } catch ( \Exception $e ) { |
||
124 | $error = new \WP_Error( 'multisafepay_error', $e->getMessage() ); |
||
125 | |||
126 | $this->set_error( $error ); |
||
127 | |||
128 | return $payment_methods; |
||
129 | } |
||
130 | |||
131 | if ( false === $result ) { |
||
132 | return $payment_methods; |
||
133 | } |
||
134 | |||
135 | foreach ( $result as $method => $title ) { |
||
136 | $payment_method = Methods::transform_gateway_method( $method ); |
||
137 | |||
138 | if ( $payment_method ) { |
||
139 | $payment_methods[] = $payment_method; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | return $payment_methods; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Get supported payment methods |
||
148 | * |
||
149 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||
150 | */ |
||
151 | public function get_supported_payment_methods() { |
||
152 | return array( |
||
153 | PaymentMethods::ALIPAY, |
||
154 | PaymentMethods::BANCONTACT, |
||
155 | PaymentMethods::BANK_TRANSFER, |
||
156 | PaymentMethods::BELFIUS, |
||
157 | PaymentMethods::CREDIT_CARD, |
||
158 | PaymentMethods::DIRECT_DEBIT, |
||
159 | PaymentMethods::IDEAL, |
||
160 | PaymentMethods::IDEALQR, |
||
161 | PaymentMethods::GIROPAY, |
||
162 | PaymentMethods::KBC, |
||
163 | PaymentMethods::PAYPAL, |
||
164 | PaymentMethods::SOFORT, |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Start payment. |
||
170 | * |
||
171 | * @param Payment $payment Payment object. |
||
172 | */ |
||
173 | public function start( Payment $payment ) { |
||
174 | $payment_method = $payment->get_method(); |
||
175 | |||
176 | $transaction_description = $payment->get_description(); |
||
177 | |||
178 | if ( empty( $transaction_description ) ) { |
||
179 | $transaction_description = $payment->get_id(); |
||
180 | } |
||
181 | |||
182 | // Merchant. |
||
183 | $merchant = new Merchant(); |
||
184 | $merchant->account = $this->config->account_id; |
||
185 | $merchant->site_id = $this->config->site_id; |
||
186 | $merchant->site_secure_code = $this->config->site_code; |
||
187 | $merchant->notification_url = $payment->get_return_url(); |
||
188 | $merchant->redirect_url = $payment->get_return_url(); |
||
189 | $merchant->cancel_url = $payment->get_return_url(); |
||
190 | $merchant->close_window = 'false'; |
||
191 | |||
192 | // Customer. |
||
193 | $customer = new Customer(); |
||
194 | $customer->ip_address = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP ); |
||
195 | $customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP ); |
||
196 | |||
197 | if ( null !== $payment->get_customer() ) { |
||
198 | $name = $payment->get_customer()->get_name(); |
||
199 | |||
200 | if ( null !== $name ) { |
||
201 | $customer->first_name = $name->get_first_name(); |
||
202 | $customer->last_name = $name->get_last_name(); |
||
203 | } |
||
204 | |||
205 | $customer->locale = $payment->get_customer()->get_locale(); |
||
206 | $customer->email = $payment->get_customer()->get_email(); |
||
207 | } |
||
208 | |||
209 | // Transaction. |
||
210 | $transaction = new Transaction(); |
||
211 | $transaction->id = uniqid(); |
||
212 | $transaction->currency = $payment->get_total_amount()->get_currency()->get_alphabetic_code(); |
||
213 | $transaction->amount = $payment->get_total_amount()->get_cents(); |
||
214 | $transaction->description = $transaction_description; |
||
215 | |||
216 | switch ( $payment_method ) { |
||
217 | case PaymentMethods::IDEAL: |
||
218 | $transaction->gateway = Methods::IDEAL; |
||
219 | |||
220 | $issuer = $payment->get_issuer(); |
||
221 | |||
222 | if ( empty( $issuer ) ) { |
||
223 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
224 | } else { |
||
225 | $gateway_info = new GatewayInfo(); |
||
226 | |||
227 | $gateway_info->issuer_id = $issuer; |
||
228 | |||
229 | $message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info ); |
||
230 | } |
||
231 | |||
232 | break; |
||
233 | case PaymentMethods::CREDIT_CARD: |
||
234 | $gateway = Methods::transform( $payment_method ); |
||
235 | |||
236 | $issuer = $payment->get_issuer(); |
||
237 | |||
238 | if ( empty( $issuer ) ) { |
||
239 | if ( $gateway ) { |
||
240 | $transaction->gateway = $gateway; |
||
241 | } |
||
242 | } else { |
||
243 | $transaction->gateway = $issuer; |
||
244 | } |
||
245 | |||
246 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
247 | |||
248 | break; |
||
249 | default: |
||
250 | $gateway = Methods::transform( $payment_method ); |
||
251 | |||
252 | if ( $gateway ) { |
||
253 | $transaction->gateway = $gateway; |
||
254 | } |
||
255 | |||
256 | if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) { |
||
257 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||
258 | $transaction->gateway = $payment_method; |
||
259 | } |
||
260 | |||
261 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
262 | } |
||
263 | |||
264 | $signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id ); |
||
265 | |||
266 | $message->signature = $signature; |
||
267 | |||
268 | try { |
||
269 | $response = $this->client->start_transaction( $message ); |
||
270 | } catch ( \Exception $e ) { |
||
271 | $error = new \WP_Error( 'multisafepay_error', $e->getMessage() ); |
||
272 | |||
273 | $this->set_error( $error ); |
||
274 | |||
275 | return; |
||
276 | } |
||
277 | |||
278 | if ( false !== $response ) { |
||
279 | $transaction = $response->transaction; |
||
280 | |||
281 | $payment->set_transaction_id( $transaction->id ); |
||
282 | |||
283 | if ( isset( $transaction->payment_url ) ) { |
||
284 | $payment->set_action_url( $transaction->payment_url ); |
||
285 | } |
||
286 | |||
287 | if ( isset( $response->gateway_info->redirect_url ) ) { |
||
288 | $payment->set_action_url( $response->gateway_info->redirect_url ); |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Update status. |
||
295 | * |
||
296 | * @param Payment $payment Payment. |
||
297 | */ |
||
298 | public function update_status( Payment $payment ) { |
||
299 | $merchant = new Merchant(); |
||
300 | |||
301 | $merchant->account = $this->config->account_id; |
||
302 | $merchant->site_id = $this->config->site_id; |
||
303 | $merchant->site_secure_code = $this->config->site_code; |
||
304 | |||
305 | $message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() ); |
||
306 | |||
307 | try { |
||
308 | $result = $this->client->get_status( $message ); |
||
309 | } catch ( \Exception $e ) { |
||
310 | $this->error = new \WP_Error( 'multisafepay_error', $e->getMessage() ); |
||
311 | |||
312 | return; |
||
313 | } |
||
314 | |||
315 | if ( false !== $result ) { |
||
316 | $status = Statuses::transform( $result->ewallet->status ); |
||
317 | |||
318 | $payment->set_status( $status ); |
||
319 | $payment->set_consumer_name( $result->payment_details->account_holder_name ); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
320 | $payment->set_consumer_iban( $result->payment_details->account_iban ); |
||
321 | $payment->set_consumer_bic( $result->payment_details->account_bic ); |
||
322 | $payment->set_consumer_account_number( $result->payment_details->account_id ); |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 |