Failed Conditions
Push — develop ( 99c6e5...eea765 )
by Reüel
03:14
created

src/Gateway.php (2 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Buckaroo;
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\Core\Server;
8
use Pronamic\WordPress\Pay\Payments\Payment;
9
10
/**
11
 * Title: Buckaroo gateway
12
 * Description:
13
 * Copyright: 2005-2019 Pronamic
14
 * Company: Pronamic
15
 *
16
 * @author Remco Tolsma
17
 * @version 2.0.1
18
 * @since 1.0.0
19
 */
20
class Gateway extends Core_Gateway {
21
	/**
22
	 * Slug of this gateway
23
	 *
24
	 * @var string
25
	 */
26
	const SLUG = 'buckaroo';
27
28
	/**
29
	 * Client.
30
	 *
31
	 * @var Client
32
	 */
33
	protected $client;
34
35
	/**
36
	 * Constructs and initializes an Buckaroo gateway
37
	 *
38
	 * @param Config $config Config.
39
	 */
40
	public function __construct( Config $config ) {
41
		parent::__construct( $config );
42
43
		$this->set_method( self::METHOD_HTML_FORM );
44
		$this->set_slug( self::SLUG );
45
46
		$this->client = new Client();
47
		$this->client->set_website_key( $config->website_key );
48
		$this->client->set_secret_key( $config->secret_key );
49
		$this->client->set_excluded_services( $config->excluded_services );
50
		$this->client->set_invoice_number( $config->invoice_number );
51
		$this->client->set_push_url( add_query_arg( 'buckaroo_push', '', home_url( '/' ) ) );
52
53
		if ( self::MODE_TEST === $config->mode ) {
54
			$this->client->set_payment_server_url( Client::GATEWAY_TEST_URL );
55
		}
56
	}
57
58
	/**
59
	 * Get issuers.
60
	 *
61
	 * @since 1.2.4
62
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
63
	 */
64
	public function get_issuers() {
65
		$groups = array();
66
67
		$result = $this->client->get_issuers();
68
69
		if ( $result ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
70
			$groups[] = array(
71
				'options' => $result,
72
			);
73
74
			return $groups;
75
		}
76
77
		$this->error = $this->client->get_error();
78
	}
79
80
	/**
81
	 * Get supported payment methods
82
	 *
83
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
84
	 */
85
	public function get_supported_payment_methods() {
86
		return array(
87
			Core_PaymentMethods::BANK_TRANSFER,
88
			Core_PaymentMethods::BANCONTACT,
89
			Core_PaymentMethods::CREDIT_CARD,
90
			Core_PaymentMethods::GIROPAY,
91
			Core_PaymentMethods::IDEAL,
92
			Core_PaymentMethods::PAYPAL,
93
			Core_PaymentMethods::SOFORT,
94
		);
95
	}
96
97
	/**
98
	 * Start
99
	 *
100
	 * @param Payment $payment Payment.
101
	 *
102
	 * @see Core_Gateway::start()
103
	 */
104
	public function start( Payment $payment ) {
105
		$payment->set_action_url( $this->client->get_payment_server_url() );
106
107
		$payment_method = $payment->get_method();
108
109
		switch ( $payment_method ) {
110
			case Core_PaymentMethods::IDEAL:
111
				$this->client->set_payment_method( PaymentMethods::IDEAL );
112
				$this->client->set_ideal_issuer( $payment->get_issuer() );
113
114
				break;
115
			case Core_PaymentMethods::CREDIT_CARD:
116
				$this->client->add_requested_service( PaymentMethods::AMERICAN_EXPRESS );
117
				$this->client->add_requested_service( PaymentMethods::MAESTRO );
118
				$this->client->add_requested_service( PaymentMethods::MASTERCARD );
119
				$this->client->add_requested_service( PaymentMethods::VISA );
120
121
				break;
122
			case Core_PaymentMethods::BANK_TRANSFER:
123
			case Core_PaymentMethods::BANCONTACT:
124
			case Core_PaymentMethods::MISTER_CASH:
125
			case Core_PaymentMethods::GIROPAY:
126
			case Core_PaymentMethods::PAYPAL:
127
			case Core_PaymentMethods::SOFORT:
128
				$this->client->set_payment_method( PaymentMethods::transform( $payment_method ) );
129
130
				break;
131
			default:
132
				if ( '0' !== $payment_method ) {
133
					// Leap of faith if the WordPress payment method could not transform to a Buckaroo method?
134
					$this->client->set_payment_method( $payment_method );
135
				}
136
137
				break;
138
		}
139
140
		// Locale.
141
		$locale = '';
142
143
		if ( null !== $payment->get_customer() ) {
144
			$locale = $payment->get_customer()->get_locale();
145
		}
146
147
		// Buckaroo uses 'nl-NL' instead of 'nl_NL'.
148
		$culture = str_replace( '_', '-', $locale );
149
150
		$this->client->set_payment_id( $payment->get_id() );
151
		$this->client->set_culture( $culture );
152
		$this->client->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() );
153
		$this->client->set_description( $payment->get_description() );
154
		$this->client->set_amount( $payment->get_total_amount()->get_value() );
0 ignored issues
show
$payment->get_total_amount()->get_value() of type double is incompatible with the type integer expected by parameter $amount of Pronamic\WordPress\Pay\G...oo\Client::set_amount(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

154
		$this->client->set_amount( /** @scrutinizer ignore-type */ $payment->get_total_amount()->get_value() );
Loading history...
155
		$this->client->set_invoice_number( Util::get_invoice_number( $this->client->get_invoice_number(), $payment ) );
156
		$this->client->set_return_url( $payment->get_return_url() );
157
		$this->client->set_return_cancel_url( $payment->get_return_url() );
158
		$this->client->set_return_error_url( $payment->get_return_url() );
159
		$this->client->set_return_reject_url( $payment->get_return_url() );
160
	}
161
162
	/**
163
	 * Get output HTML
164
	 *
165
	 * @since 1.1.1
166
	 * @see Pronamic_WP_Pay_Gateway::get_output_html()
167
	 */
168
	public function get_output_fields() {
169
		return $this->client->get_fields();
170
	}
171
172
	/**
173
	 * Update status of the specified payment
174
	 *
175
	 * @param Payment $payment Payment.
176
	 */
177
	public function update_status( Payment $payment ) {
178
		$method = Server::get( 'REQUEST_METHOD', FILTER_SANITIZE_STRING );
179
180
		$data = array();
181
182
		switch ( $method ) {
183
			case 'GET':
184
				$data = $_GET; // WPCS: CSRF OK.
185
186
				break;
187
			case 'POST':
188
				$data = $_POST; // WPCS: CSRF OK.
189
190
				break;
191
		}
192
193
		$data = Util::urldecode( $data );
194
195
		$data = stripslashes_deep( $data );
196
197
		$data = $this->client->verify_request( $data );
198
199
		if ( $data ) {
200
			$payment->set_transaction_id( $data[ Parameters::PAYMENT ] );
201
			$payment->set_status( Statuses::transform( $data[ Parameters::STATUS_CODE ] ) );
202
			$payment->set_consumer_iban( $data[ Parameters::SERVICE_IDEAL_CONSUMER_IBAN ] );
203
			$payment->set_consumer_bic( $data[ Parameters::SERVICE_IDEAL_CONSUMER_BIC ] );
204
			$payment->set_consumer_name( $data[ Parameters::SERVICE_IDEAL_CONSUMER_NAME ] );
205
206
			$labels = array(
207
				Parameters::PAYMENT                       => __( 'Payment', 'pronamic_ideal' ),
208
				Parameters::PAYMENT_METHOD                => __( 'Payment Method', 'pronamic_ideal' ),
209
				Parameters::STATUS_CODE                   => __( 'Status Code', 'pronamic_ideal' ),
210
				Parameters::STATUS_CODE_DETAIL            => __( 'Status Code Detail', 'pronamic_ideal' ),
211
				Parameters::STATUS_MESSAGE                => __( 'Status Message', 'pronamic_ideal' ),
212
				Parameters::INVOICE_NUMBER                => __( 'Invoice Number', 'pronamic_ideal' ),
213
				Parameters::AMOUNT                        => __( 'Amount', 'pronamic_ideal' ),
214
				Parameters::CURRENCY                      => __( 'Currency', 'pronamic_ideal' ),
215
				Parameters::TIMESTAMP                     => __( 'Timestamp', 'pronamic_ideal' ),
216
				Parameters::SERVICE_IDEAL_CONSUMER_ISSUER => __( 'Service iDEAL Consumer Issuer', 'pronamic_ideal' ),
217
				Parameters::SERVICE_IDEAL_CONSUMER_NAME   => __( 'Service iDEAL Consumer Name', 'pronamic_ideal' ),
218
				Parameters::SERVICE_IDEAL_CONSUMER_IBAN   => __( 'Service iDEAL Consumer IBAN', 'pronamic_ideal' ),
219
				Parameters::SERVICE_IDEAL_CONSUMER_BIC    => __( 'Service iDEAL Consumer BIC', 'pronamic_ideal' ),
220
				Parameters::TRANSACTIONS                  => __( 'Transactions', 'pronamic_ideal' ),
221
			);
222
223
			$note = '';
224
225
			$note .= '<p>';
226
			$note .= __( 'Buckaroo data:', 'pronamic_ideal' );
227
			$note .= '</p>';
228
229
			$note .= '<dl>';
230
231
			foreach ( $labels as $key => $label ) {
232
				if ( isset( $data[ $key ] ) ) {
233
					$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
234
					$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
235
				}
236
			}
237
238
			$note .= '</dl>';
239
240
			$payment->add_note( $note );
241
		}
242
	}
243
}
244