1 | <?php |
||
2 | |||
3 | namespace Pronamic\WordPress\Pay\Gateways\Ingenico\OrderStandard; |
||
4 | |||
5 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
6 | use Pronamic\WordPress\Pay\Core\PaymentMethods as Core_PaymentMethods; |
||
7 | use Pronamic\WordPress\Pay\Gateways\Ingenico\Brands; |
||
8 | use Pronamic\WordPress\Pay\Gateways\Ingenico\DataCustomerHelper; |
||
9 | use Pronamic\WordPress\Pay\Gateways\Ingenico\DataGeneralHelper; |
||
10 | use Pronamic\WordPress\Pay\Gateways\Ingenico\DataUrlHelper; |
||
11 | use Pronamic\WordPress\Pay\Gateways\Ingenico\Parameters; |
||
12 | use Pronamic\WordPress\Pay\Gateways\Ingenico\PaymentMethods; |
||
13 | use Pronamic\WordPress\Pay\Gateways\Ingenico\Statuses; |
||
14 | use Pronamic\WordPress\Pay\Gateways\Ingenico\Util; |
||
15 | use Pronamic\WordPress\Pay\Gateways\Ingenico\Security; |
||
16 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
17 | |||
18 | /** |
||
19 | * Title: Ingenico order standard gateway |
||
20 | * Description: |
||
21 | * Copyright: 2005-2021 Pronamic |
||
22 | * Company: Pronamic |
||
23 | * |
||
24 | * @author Remco Tolsma |
||
25 | * @version 2.0.4 |
||
26 | * @since 1.0.0 |
||
27 | */ |
||
28 | class Gateway extends Core_Gateway { |
||
29 | /** |
||
30 | * Client. |
||
31 | * |
||
32 | * @var Client |
||
33 | */ |
||
34 | protected $client; |
||
35 | |||
36 | /** |
||
37 | * Constructs and initializes an OrderStandard gateway |
||
38 | * |
||
39 | * @param Config $config Config. |
||
40 | */ |
||
41 | public function __construct( Config $config ) { |
||
42 | parent::__construct( $config ); |
||
43 | |||
44 | $this->set_method( self::METHOD_HTML_FORM ); |
||
45 | |||
46 | // Supported features. |
||
47 | $this->supports = array( |
||
48 | 'payment_status_request', |
||
49 | ); |
||
50 | |||
51 | // Client. |
||
52 | $this->client = new Client( $this->config->psp_id ); |
||
53 | |||
54 | $this->client->set_payment_server_url( $config->get_form_action_url() ); |
||
55 | $this->client->set_direct_query_url( $config->get_direct_query_url() ); |
||
56 | $this->client->set_pass_phrase_in( $config->sha_in_pass_phrase ); |
||
57 | $this->client->set_pass_phrase_out( $config->sha_out_pass_phrase ); |
||
58 | $this->client->set_user_id( $config->user_id ); |
||
59 | $this->client->set_password( $config->password ); |
||
60 | |||
61 | if ( ! empty( $config->hash_algorithm ) ) { |
||
62 | $this->client->set_hash_algorithm( $config->hash_algorithm ); |
||
63 | } |
||
64 | } |
||
65 | /** |
||
66 | * Get supported payment methods |
||
67 | * |
||
68 | * @see Core_Gateway::get_supported_payment_methods() |
||
69 | */ |
||
70 | public function get_supported_payment_methods() { |
||
71 | return array( |
||
72 | Core_PaymentMethods::BANK_TRANSFER, |
||
73 | Core_PaymentMethods::IDEAL, |
||
74 | Core_PaymentMethods::CREDIT_CARD, |
||
75 | Core_PaymentMethods::BANCONTACT, |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Start |
||
81 | * |
||
82 | * @see Core_Gateway::start() |
||
83 | * |
||
84 | * @param Payment $payment Payment. |
||
85 | */ |
||
86 | public function start( Payment $payment ) { |
||
87 | $payment->set_action_url( $this->client->get_payment_server_url() ); |
||
88 | |||
89 | if ( $this->config->alias_enabled ) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
90 | $alias = uniqid(); |
||
91 | |||
92 | $payment->set_meta( 'ogone_alias', $alias ); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get output fields |
||
98 | * |
||
99 | * @param Payment $payment Payment. |
||
100 | * |
||
101 | * @return array |
||
102 | * @since 1.2.1 |
||
103 | * @version 2.0.4 |
||
104 | * @see Core_Gateway::get_output_html() |
||
105 | */ |
||
106 | public function get_output_fields( Payment $payment ) { |
||
107 | $ogone_data = $this->client->get_data(); |
||
108 | |||
109 | // General. |
||
110 | $ogone_data_general = new DataGeneralHelper( $ogone_data ); |
||
111 | |||
112 | $ogone_data_general |
||
113 | ->set_order_id( $payment->format_string( $this->config->order_id ) ) |
||
114 | ->set_order_description( $payment->get_description() ) |
||
115 | ->set_param_plus( 'payment_id=' . $payment->get_id() ) |
||
116 | ->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() ) |
||
117 | ->set_amount( $payment->get_total_amount()->get_minor_units()->format( 0, '', '' ) ); |
||
118 | |||
119 | // Alias. |
||
120 | $alias = $payment->get_meta( 'ogone_alias' ); |
||
121 | |||
122 | if ( $this->config->alias_enabled && false !== $alias ) { |
||
0 ignored issues
–
show
|
|||
123 | $ogone_data_general->set_alias( $alias ) |
||
124 | ->set_alias_usage( $this->config->alias_usage ); |
||
0 ignored issues
–
show
|
|||
125 | } |
||
126 | |||
127 | $customer = $payment->get_customer(); |
||
128 | |||
129 | if ( null !== $customer ) { |
||
130 | // Localised language. |
||
131 | $ogone_data_general->set_language( $customer->get_locale() ); |
||
132 | } |
||
133 | |||
134 | // Customer. |
||
135 | $ogone_data_customer = new DataCustomerHelper( $ogone_data ); |
||
136 | |||
137 | if ( null !== $customer ) { |
||
138 | $name = $customer->get_name(); |
||
139 | |||
140 | if ( null !== $name ) { |
||
141 | $ogone_data_customer->set_name( strval( $name ) ); |
||
142 | } |
||
143 | |||
144 | $ogone_data_customer->set_email( $customer->get_email() ); |
||
145 | } |
||
146 | |||
147 | $billing_address = $payment->get_billing_address(); |
||
148 | |||
149 | if ( null !== $billing_address ) { |
||
150 | $ogone_data_customer |
||
151 | ->set_address( $billing_address->get_line_1() ) |
||
152 | ->set_zip( $billing_address->get_postal_code() ) |
||
153 | ->set_town( $billing_address->get_city() ) |
||
154 | ->set_country( $billing_address->get_country_code() ) |
||
155 | ->set_telephone_number( $billing_address->get_phone() ); |
||
156 | } |
||
157 | |||
158 | /* |
||
159 | * Payment method. |
||
160 | * |
||
161 | * @link https://github.com/wp-pay-gateways/ogone/wiki/Brands |
||
162 | */ |
||
163 | switch ( $payment->get_method() ) { |
||
164 | case Core_PaymentMethods::BANK_TRANSFER: |
||
165 | /* |
||
166 | * Set bank transfer payment method. |
||
167 | * @since 2.2.0 |
||
168 | */ |
||
169 | $ogone_data_general |
||
170 | ->set_payment_method( PaymentMethods::BANK_TRANSFER ); |
||
171 | |||
172 | break; |
||
173 | case Core_PaymentMethods::CREDIT_CARD: |
||
174 | /* |
||
175 | * Set credit card payment method. |
||
176 | * @since 1.2.3 |
||
177 | */ |
||
178 | $ogone_data_general |
||
179 | ->set_payment_method( PaymentMethods::CREDIT_CARD ); |
||
180 | |||
181 | break; |
||
182 | case Core_PaymentMethods::IDEAL: |
||
183 | /* |
||
184 | * Set iDEAL payment method. |
||
185 | * @since 1.2.3 |
||
186 | */ |
||
187 | $ogone_data_general |
||
188 | ->set_brand( Brands::IDEAL ) |
||
189 | ->set_payment_method( PaymentMethods::IDEAL ); |
||
190 | |||
191 | break; |
||
192 | case Core_PaymentMethods::BANCONTACT: |
||
193 | case Core_PaymentMethods::MISTER_CASH: |
||
194 | $ogone_data_general |
||
195 | ->set_brand( Brands::BCMC ) |
||
196 | ->set_payment_method( PaymentMethods::CREDIT_CARD ); |
||
197 | |||
198 | break; |
||
199 | } |
||
200 | |||
201 | // Parameter Variable. |
||
202 | $param_var = Util::get_param_var( $this->config->param_var ); |
||
0 ignored issues
–
show
|
|||
203 | |||
204 | if ( ! empty( $param_var ) ) { |
||
205 | $ogone_data->set_field( 'PARAMVAR', $param_var ); |
||
206 | } |
||
207 | |||
208 | // Template Page. |
||
209 | $template_page = $this->config->template_page; |
||
0 ignored issues
–
show
|
|||
210 | |||
211 | if ( ! empty( $template_page ) ) { |
||
212 | $ogone_data->set_field( 'TP', $template_page ); |
||
213 | } |
||
214 | |||
215 | // URLs. |
||
216 | $ogone_url_helper = new DataUrlHelper( $ogone_data ); |
||
217 | |||
218 | $ogone_url_helper |
||
219 | ->set_accept_url( add_query_arg( 'status', 'accept', $payment->get_return_url() ) ) |
||
220 | ->set_cancel_url( add_query_arg( 'status', 'cancel', $payment->get_return_url() ) ) |
||
221 | ->set_decline_url( add_query_arg( 'status', 'decline', $payment->get_return_url() ) ) |
||
222 | ->set_exception_url( add_query_arg( 'status', 'exception', $payment->get_return_url() ) ); |
||
223 | |||
224 | return $this->client->get_fields(); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Update status of the specified payment |
||
229 | * |
||
230 | * @param Payment $payment Payment. |
||
231 | */ |
||
232 | public function update_status( Payment $payment ) { |
||
233 | $data = Security::get_request_data(); |
||
234 | |||
235 | $data = $this->client->verify_request( $data ); |
||
236 | |||
237 | if ( false !== $data ) { |
||
238 | $status = Statuses::transform( $data[ Parameters::STATUS ] ); |
||
239 | |||
240 | $payment->set_status( $status ); |
||
241 | |||
242 | // Update transaction ID. |
||
243 | if ( \array_key_exists( Parameters::PAY_ID, $data ) ) { |
||
244 | $payment->set_transaction_id( $data[ Parameters::PAY_ID ] ); |
||
245 | } |
||
246 | |||
247 | // Add payment note. |
||
248 | $this->update_status_payment_note( $payment, $data ); |
||
249 | |||
250 | return; |
||
251 | } |
||
252 | |||
253 | // Get order status with direct query. |
||
254 | $order_id = $payment->format_string( $this->config->order_id ); |
||
255 | |||
256 | try { |
||
257 | $status = $this->client->get_order_status( $order_id ); |
||
258 | } catch ( \Exception $e ) { |
||
259 | $payment->add_note( $e->getMessage() ); |
||
260 | |||
261 | return; |
||
262 | } |
||
263 | |||
264 | if ( null !== $status ) { |
||
265 | $payment->set_status( $status ); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Update status payment note |
||
271 | * |
||
272 | * @param Payment $payment Payment. |
||
273 | * @param array $data Data. |
||
274 | */ |
||
275 | private function update_status_payment_note( Payment $payment, $data ) { |
||
276 | $labels = array( |
||
277 | 'STATUS' => __( 'Status', 'pronamic_ideal' ), |
||
278 | 'ORDERID' => __( 'Order ID', 'pronamic_ideal' ), |
||
279 | 'CURRENCY' => __( 'Currency', 'pronamic_ideal' ), |
||
280 | 'AMOUNT' => __( 'Amount', 'pronamic_ideal' ), |
||
281 | 'PM' => __( 'Payment Method', 'pronamic_ideal' ), |
||
282 | 'ACCEPTANCE' => __( 'Acceptance', 'pronamic_ideal' ), |
||
283 | 'CARDNO' => __( 'Card Number', 'pronamic_ideal' ), |
||
284 | 'ED' => __( 'End Date', 'pronamic_ideal' ), |
||
285 | 'CN' => __( 'Customer Name', 'pronamic_ideal' ), |
||
286 | 'TRXDATE' => __( 'Transaction Date', 'pronamic_ideal' ), |
||
287 | 'PAYID' => __( 'Pay ID', 'pronamic_ideal' ), |
||
288 | 'NCERROR' => __( 'NC Error', 'pronamic_ideal' ), |
||
289 | 'BRAND' => __( 'Brand', 'pronamic_ideal' ), |
||
290 | 'IP' => __( 'IP', 'pronamic_ideal' ), |
||
291 | 'SHASIGN' => __( 'SHA Signature', 'pronamic_ideal' ), |
||
292 | ); |
||
293 | |||
294 | $note = ''; |
||
295 | |||
296 | $note .= '<p>'; |
||
297 | $note .= __( 'Ogone transaction data in response message:', 'pronamic_ideal' ); |
||
298 | $note .= '</p>'; |
||
299 | |||
300 | $note .= '<dl>'; |
||
301 | |||
302 | foreach ( $labels as $key => $label ) { |
||
303 | if ( isset( $data[ $key ] ) && '' !== $data[ $key ] ) { |
||
304 | $note .= sprintf( '<dt>%s</dt>', esc_html( $label ) ); |
||
305 | $note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) ); |
||
306 | } |
||
307 | } |
||
308 | |||
309 | $note .= '</dl>'; |
||
310 | |||
311 | $payment->add_note( $note ); |
||
312 | } |
||
313 | } |
||
314 |