Failed Conditions
Push — develop ( 3ad9f7...e51f98 )
by Remco
05:02
created

src/Gateway.php (1 issue)

Labels
Severity
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-2020 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 string
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( $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
		$locale = '';
148
149
		if ( null !== $payment->get_customer() ) {
150
			$locale = $payment->get_customer()->get_locale();
151
		}
152
153
		// Buckaroo uses 'nl-NL' instead of 'nl_NL'.
154
		$culture = str_replace( '_', '-', $locale );
155
156
		$this->client->set_payment_id( $payment->get_id() );
157
		$this->client->set_culture( $culture );
158
		$this->client->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() );
159
		$this->client->set_description( $payment->get_description() );
160
		$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

160
		$this->client->set_amount( /** @scrutinizer ignore-type */ $payment->get_total_amount()->get_value() );
Loading history...
161
		$this->client->set_invoice_number( Util::get_invoice_number( $this->client->get_invoice_number(), $payment ) );
162
		$this->client->set_return_url( $payment->get_return_url() );
163
		$this->client->set_return_cancel_url( $payment->get_return_url() );
164
		$this->client->set_return_error_url( $payment->get_return_url() );
165
		$this->client->set_return_reject_url( $payment->get_return_url() );
166
167
		return $this->client->get_fields();
168
	}
169
170
	/**
171
	 * Update status of the specified payment
172
	 *
173
	 * @param Payment $payment Payment.
174
	 */
175
	public function update_status( Payment $payment ) {
176
		$method = Server::get( 'REQUEST_METHOD', FILTER_SANITIZE_STRING );
177
178
		$data = array();
179
180
		switch ( $method ) {
181
			case 'GET':
182
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended
183
				$data = $_GET;
184
185
				break;
186
			case 'POST':
187
				// phpcs:ignore WordPress.Security.NonceVerification.Missing
188
				$data = $_POST;
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
203
			// Consumer bank details.
204
			$consumer_bank_details = $payment->get_consumer_bank_details();
205
206
			if ( null === $consumer_bank_details ) {
207
				$consumer_bank_details = new BankAccountDetails();
208
209
				$payment->set_consumer_bank_details( $consumer_bank_details );
210
			}
211
212
			$consumer_bank_details->set_name( $data[ Parameters::SERVICE_IDEAL_CONSUMER_NAME ] );
213
			$consumer_bank_details->set_iban( $data[ Parameters::SERVICE_IDEAL_CONSUMER_IBAN ] );
214
			$consumer_bank_details->set_bic( $data[ Parameters::SERVICE_IDEAL_CONSUMER_BIC ] );
215
216
			$labels = array(
217
				Parameters::PAYMENT                       => __( 'Payment', 'pronamic_ideal' ),
218
				Parameters::PAYMENT_METHOD                => __( 'Payment Method', 'pronamic_ideal' ),
219
				Parameters::STATUS_CODE                   => __( 'Status Code', 'pronamic_ideal' ),
220
				Parameters::STATUS_CODE_DETAIL            => __( 'Status Code Detail', 'pronamic_ideal' ),
221
				Parameters::STATUS_MESSAGE                => __( 'Status Message', 'pronamic_ideal' ),
222
				Parameters::INVOICE_NUMBER                => __( 'Invoice Number', 'pronamic_ideal' ),
223
				Parameters::AMOUNT                        => __( 'Amount', 'pronamic_ideal' ),
224
				Parameters::CURRENCY                      => __( 'Currency', 'pronamic_ideal' ),
225
				Parameters::TIMESTAMP                     => __( 'Timestamp', 'pronamic_ideal' ),
226
				Parameters::SERVICE_IDEAL_CONSUMER_ISSUER => __( 'Service iDEAL Consumer Issuer', 'pronamic_ideal' ),
227
				Parameters::SERVICE_IDEAL_CONSUMER_NAME   => __( 'Service iDEAL Consumer Name', 'pronamic_ideal' ),
228
				Parameters::SERVICE_IDEAL_CONSUMER_IBAN   => __( 'Service iDEAL Consumer IBAN', 'pronamic_ideal' ),
229
				Parameters::SERVICE_IDEAL_CONSUMER_BIC    => __( 'Service iDEAL Consumer BIC', 'pronamic_ideal' ),
230
				Parameters::TRANSACTIONS                  => __( 'Transactions', 'pronamic_ideal' ),
231
			);
232
233
			$note = '';
234
235
			$note .= '<p>';
236
			$note .= __( 'Buckaroo data:', 'pronamic_ideal' );
237
			$note .= '</p>';
238
239
			$note .= '<dl>';
240
241
			foreach ( $labels as $key => $label ) {
242
				if ( isset( $data[ $key ] ) ) {
243
					$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
244
					$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
245
				}
246
			}
247
248
			$note .= '</dl>';
249
250
			$payment->add_note( $note );
251
		}
252
	}
253
}
254