Test Failed
Push — develop ( 1f5549...88de64 )
by Reüel
14:27
created

src/Gateway.php (3 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.2
18
 * @since 1.0.0
19
 */
20
class Gateway extends Core_Gateway {
21
	/**
22
	 * Client.
23
	 *
24
	 * @var Client
25
	 */
26
	protected $client;
27
28
	/**
29
	 * Constructs and initializes an Buckaroo gateway
30
	 *
31
	 * @param Config $config Config.
32
	 */
33
	public function __construct( Config $config ) {
34
		parent::__construct( $config );
35
36
		$this->set_method( self::METHOD_HTML_FORM );
37
38
		$this->client = new Client();
39
		$this->client->set_website_key( $config->website_key );
40
		$this->client->set_secret_key( $config->secret_key );
41
		$this->client->set_excluded_services( $config->excluded_services );
42
		$this->client->set_invoice_number( $config->invoice_number );
43
		$this->client->set_push_url( add_query_arg( 'buckaroo_push', '', home_url( '/' ) ) );
44
45
		if ( self::MODE_TEST === $config->mode ) {
46
			$this->client->set_payment_server_url( Client::GATEWAY_TEST_URL );
47
		}
48
	}
49
50
	/**
51
	 * Get issuers.
52
	 *
53
	 * @since 1.2.4
54
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
55
	 */
56
	public function get_issuers() {
57
		$groups = array();
58
59
		$result = $this->client->get_issuers();
60
61
		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...
62
			$groups[] = array(
63
				'options' => $result,
64
			);
65
66
			return $groups;
67
		}
68
69
		$this->error = $this->client->get_error();
0 ignored issues
show
The method get_error() does not exist on Pronamic\WordPress\Pay\Gateways\Buckaroo\Client. ( Ignorable by Annotation )

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

69
		/** @scrutinizer ignore-call */ 
70
  $this->error = $this->client->get_error();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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

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