Failed Conditions
Push — master ( e05daf...7d13b7 )
by Reüel
08:31 queued 12s
created

src/Gateway.php (1 issue)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Buckaroo;
4
5
use Pronamic\WordPress\Pay\Banks\BankAccountDetails;
6
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
7
use Pronamic\WordPress\Pay\Core\PaymentMethods as Core_PaymentMethods;
8
use Pronamic\WordPress\Pay\Core\Server;
9
use Pronamic\WordPress\Pay\Payments\Payment;
10
11
/**
12
 * Title: Buckaroo gateway
13
 * Description:
14
 * Copyright: 2005-2021 Pronamic
15
 * Company: Pronamic
16
 *
17
 * @author Remco Tolsma
18
 * @version 2.0.4
19
 * @since 1.0.0
20
 */
21
class Gateway extends Core_Gateway {
22
	/**
23
	 * Client.
24
	 *
25
	 * @var Client
26
	 */
27
	protected $client;
28
29
	/**
30
	 * Constructs and initializes an Buckaroo gateway
31
	 *
32
	 * @param Config $config Config.
33
	 */
34
	public function __construct( Config $config ) {
35
		parent::__construct( $config );
36
37
		$this->set_method( self::METHOD_HTML_FORM );
38
39
		$this->client = new Client();
40
		$this->client->set_website_key( $config->get_website_key() );
41
		$this->client->set_secret_key( $config->get_secret_key() );
42
		$this->client->set_excluded_services( $config->get_excluded_services() );
43
		$this->client->set_invoice_number( $config->get_invoice_number() );
44
		$this->client->set_push_url( add_query_arg( 'buckaroo_push', '', home_url( '/' ) ) );
45
46
		if ( self::MODE_TEST === $config->mode ) {
47
			$this->client->set_payment_server_url( Client::GATEWAY_TEST_URL );
48
		}
49
	}
50
51
	/**
52
	 * Get issuers.
53
	 *
54
	 * @since 1.2.4
55
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
56
	 */
57
	public function get_issuers() {
58
		$groups = array();
59
60
		try {
61
			$result = $this->client->get_issuers();
62
63
			$groups[] = array(
64
				'options' => $result,
65
			);
66
		} catch ( \Exception $e ) {
67
			$this->error = new \WP_Error( 'buckaroo', $e->getMessage() );
68
		}
69
70
		return $groups;
71
	}
72
73
	/**
74
	 * Get supported payment methods
75
	 *
76
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
77
	 */
78
	public function get_supported_payment_methods() {
79
		return array(
80
			Core_PaymentMethods::BANK_TRANSFER,
81
			Core_PaymentMethods::BANCONTACT,
82
			Core_PaymentMethods::CREDIT_CARD,
83
			Core_PaymentMethods::GIROPAY,
84
			Core_PaymentMethods::IDEAL,
85
			Core_PaymentMethods::PAYPAL,
86
			Core_PaymentMethods::SOFORT,
87
		);
88
	}
89
90
	/**
91
	 * Start
92
	 *
93
	 * @param Payment $payment Payment.
94
	 *
95
	 * @see Core_Gateway::start()
96
	 */
97
	public function start( Payment $payment ) {
98
		$payment->set_action_url( $this->client->get_payment_server_url() );
99
	}
100
101
	/**
102
	 * Get output HTML
103
	 *
104
	 * @param Payment $payment Payment.
105
	 *
106
	 * @return array
107
	 *
108
	 * @see     Core_Gateway::get_output_html()
109
	 * @since   1.1.1
110
	 * @version 2.0.4
111
	 */
112
	public function get_output_fields( Payment $payment ) {
113
		$payment_method = $payment->get_method();
114
115
		switch ( $payment_method ) {
116
			case Core_PaymentMethods::IDEAL:
117
				$this->client->set_payment_method( PaymentMethods::IDEAL );
118
				$this->client->set_ideal_issuer( $payment->get_issuer() );
119
120
				break;
121
			case Core_PaymentMethods::CREDIT_CARD:
122
				$this->client->add_requested_service( PaymentMethods::AMERICAN_EXPRESS );
123
				$this->client->add_requested_service( PaymentMethods::MAESTRO );
124
				$this->client->add_requested_service( PaymentMethods::MASTERCARD );
125
				$this->client->add_requested_service( PaymentMethods::VISA );
126
127
				break;
128
			case Core_PaymentMethods::BANK_TRANSFER:
129
			case Core_PaymentMethods::BANCONTACT:
130
			case Core_PaymentMethods::MISTER_CASH:
131
			case Core_PaymentMethods::GIROPAY:
132
			case Core_PaymentMethods::PAYPAL:
133
			case Core_PaymentMethods::SOFORT:
134
				$this->client->set_payment_method( PaymentMethods::transform( (string) $payment_method ) );
135
136
				break;
137
			default:
138
				if ( '0' !== $payment_method ) {
139
					// Leap of faith if the WordPress payment method could not transform to a Buckaroo method?
140
					$this->client->set_payment_method( $payment_method );
141
				}
142
143
				break;
144
		}
145
146
		// Locale.
147
		$culture = null;
148
149
		$customer = $payment->get_customer();
150
151
		if ( null !== $customer ) {
152
			$locale = $customer->get_locale();
153
154
			// Buckaroo uses 'nl-NL' instead of 'nl_NL'.
155
			if ( ! empty( $locale ) ) {
156
				$culture = str_replace( '_', '-', $locale );
157
			}
158
		}
159
160
		$this->client->set_payment_id( (string) $payment->get_id() );
161
		$this->client->set_culture( $culture );
162
		$this->client->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() );
163
		$this->client->set_description( $payment->get_description() );
164
		$this->client->set_amount( $payment->get_total_amount()->get_value() );
165
		$this->client->set_invoice_number( Util::get_invoice_number( (string) $this->client->get_invoice_number(), $payment ) );
166
		$this->client->set_return_url( $payment->get_return_url() );
167
		$this->client->set_return_cancel_url( $payment->get_return_url() );
168
		$this->client->set_return_error_url( $payment->get_return_url() );
169
		$this->client->set_return_reject_url( $payment->get_return_url() );
170
171
		return $this->client->get_fields();
172
	}
173
174
	/**
175
	 * Update status of the specified payment
176
	 *
177
	 * @param Payment $payment Payment.
178
	 */
179
	public function update_status( Payment $payment ) {
180
		$method = Server::get( 'REQUEST_METHOD', FILTER_SANITIZE_STRING );
181
182
		$data = array();
183
184
		switch ( $method ) {
185
			case 'GET':
186
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended
187
				$data = $_GET;
188
189
				break;
190
			case 'POST':
191
				// phpcs:ignore WordPress.Security.NonceVerification.Missing
192
				$data = $_POST;
193
194
				break;
195
		}
196
197
		$data = Util::urldecode( $data );
198
199
		$data = stripslashes_deep( $data );
200
201
		$data = $this->client->verify_request( $data );
202
203
		if ( false === $data ) {
0 ignored issues
show
The condition false === $data is always true.
Loading history...
204
			return;
205
		}
206
207
		$payment->set_transaction_id( (string) $data[ Parameters::PAYMENT ] );
208
		$payment->set_status( Statuses::transform( (string) $data[ Parameters::STATUS_CODE ] ) );
209
210
		// Consumer bank details.
211
		$consumer_bank_details = $payment->get_consumer_bank_details();
212
213
		if ( null === $consumer_bank_details ) {
214
			$consumer_bank_details = new BankAccountDetails();
215
216
			$payment->set_consumer_bank_details( $consumer_bank_details );
217
		}
218
219
		if ( \array_key_exists( Parameters::SERVICE_IDEAL_CONSUMER_NAME, $data ) ) {
220
			$consumer_bank_details->set_name( (string) $data[ Parameters::SERVICE_IDEAL_CONSUMER_NAME ] );
221
		}
222
223
		if ( \array_key_exists( Parameters::SERVICE_IDEAL_CONSUMER_IBAN, $data ) ) {
224
			$consumer_bank_details->set_iban( (string) $data[ Parameters::SERVICE_IDEAL_CONSUMER_IBAN ] );
225
		}
226
227
		if ( \array_key_exists( Parameters::SERVICE_IDEAL_CONSUMER_BIC, $data ) ) {
228
			$consumer_bank_details->set_bic( (string) $data[ Parameters::SERVICE_IDEAL_CONSUMER_BIC ] );
229
		}
230
231
		$labels = array(
232
			Parameters::PAYMENT                       => __( 'Payment', 'pronamic_ideal' ),
233
			Parameters::PAYMENT_METHOD                => __( 'Payment Method', 'pronamic_ideal' ),
234
			Parameters::STATUS_CODE                   => __( 'Status Code', 'pronamic_ideal' ),
235
			Parameters::STATUS_CODE_DETAIL            => __( 'Status Code Detail', 'pronamic_ideal' ),
236
			Parameters::STATUS_MESSAGE                => __( 'Status Message', 'pronamic_ideal' ),
237
			Parameters::INVOICE_NUMBER                => __( 'Invoice Number', 'pronamic_ideal' ),
238
			Parameters::AMOUNT                        => __( 'Amount', 'pronamic_ideal' ),
239
			Parameters::CURRENCY                      => __( 'Currency', 'pronamic_ideal' ),
240
			Parameters::TIMESTAMP                     => __( 'Timestamp', 'pronamic_ideal' ),
241
			Parameters::SERVICE_IDEAL_CONSUMER_ISSUER => __( 'Service iDEAL Consumer Issuer', 'pronamic_ideal' ),
242
			Parameters::SERVICE_IDEAL_CONSUMER_NAME   => __( 'Service iDEAL Consumer Name', 'pronamic_ideal' ),
243
			Parameters::SERVICE_IDEAL_CONSUMER_IBAN   => __( 'Service iDEAL Consumer IBAN', 'pronamic_ideal' ),
244
			Parameters::SERVICE_IDEAL_CONSUMER_BIC    => __( 'Service iDEAL Consumer BIC', 'pronamic_ideal' ),
245
			Parameters::TRANSACTIONS                  => __( 'Transactions', 'pronamic_ideal' ),
246
		);
247
248
		$note = '';
249
250
		$note .= '<p>';
251
		$note .= __( 'Buckaroo data:', 'pronamic_ideal' );
252
		$note .= '</p>';
253
254
		$note .= '<dl>';
255
256
		foreach ( $labels as $key => $label ) {
257
			if ( ! isset( $data[ $key ] ) ) {
258
				continue;
259
			}
260
261
			$note .= sprintf(
262
				'<dt>%s</dt><dd>%s</dd>',
263
				esc_html( $label ),
264
				esc_html( (string) $data[ $key ] )
265
			);
266
		}
267
268
		$note .= '</dl>';
269
270
		$payment->add_note( $note );
271
	}
272
}
273