Failed Conditions
Push — develop ( 71d0ec...6395e5 )
by Reüel
03:18
created

src/Gateway.php (3 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Pronamic\WordPress\Pay\G...Compleet\PaymentMethods. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet\PaymentMethods as Methods;
8
use Pronamic\WordPress\Pay\Payments\Payment;
9
10
/**
11
 * Title: ING Kassa Compleet
12
 * Description:
13
 * Copyright: 2005-2019 Pronamic
14
 * Company: Pronamic
15
 *
16
 * @author  Reüel van der Steege
17
 * @version 2.0.1
18
 * @since   1.0.0
19
 */
20
class Gateway extends Core_Gateway {
21
	/**
22
	 * Constructs and initializes an ING Kassa Compleet gateway
23
	 *
24
	 * @param Config $config Config.
25
	 */
26
	public function __construct( Config $config ) {
27
		parent::__construct( $config );
28
29
		$this->supports = array(
30
			'payment_status_request',
31
		);
32
33
		$this->set_method( self::METHOD_HTTP_REDIRECT );
34
35
		// Client.
36
		$this->client = new Client( $config->api_key );
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
	}
38
39
	/**
40
	 * Get issuers
41
	 *
42
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
43
	 */
44
	public function get_issuers() {
45
		$groups = array();
46
47
		$result = $this->client->get_issuers();
48
49
		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...
50
			$groups[] = array(
51
				'options' => $result,
52
			);
53
		}
54
55
		$error = $this->client->get_error();
56
57
		if ( is_wp_error( $error ) ) {
58
			$this->error = $error;
59
		}
60
61
		return $groups;
62
	}
63
64
	/**
65
	 * Get supported payment methods
66
	 *
67
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
68
	 */
69
	public function get_supported_payment_methods() {
70
		return array(
71
			PaymentMethods::BANCONTACT,
72
			PaymentMethods::BANK_TRANSFER,
73
			PaymentMethods::CREDIT_CARD,
74
			PaymentMethods::IDEAL,
75
			PaymentMethods::PAYCONIQ,
76
			PaymentMethods::PAYPAL,
77
			PaymentMethods::SOFORT,
78
		);
79
	}
80
81
	/**
82
	 * Is payment method required to start transaction?
83
	 *
84
	 * @see Pronamic_WP_Pay_Gateway::payment_method_is_required()
85
	 */
86
	public function payment_method_is_required() {
87
		return true;
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
		$request = new OrderRequest();
99
100
		$request->currency          = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
101
		$request->amount            = $payment->get_total_amount()->get_cents();
102
		$request->merchant_order_id = $payment->get_order_id();
103
		$request->description       = $payment->get_description();
104
		$request->return_url        = $payment->get_return_url();
105
106
		// To make the 'Test' meta box work, set payment method to iDEAL if an issuer_id has been set.
107
		$issuer = $payment->get_issuer();
108
109
		$payment_method = $payment->get_method();
110
111
		if ( empty( $payment_method ) && ! empty( $issuer ) ) {
112
			$payment_method = PaymentMethods::IDEAL;
113
		}
114
115
		if ( PaymentMethods::IDEAL === $payment_method ) {
116
			$request->issuer = $issuer;
117
		}
118
119
		$request->method = Methods::transform( $payment_method );
120
121
		$order = $this->client->create_order( $request );
122
123
		if ( $order ) {
124
			$payment->set_transaction_id( $order->id );
125
126
			$action_url = add_query_arg(
127
				array(
128
					'payment_redirect' => $payment->get_id(),
129
					'key'              => $payment->key,
130
				),
131
				home_url( '/' )
132
			);
133
134
			if ( PaymentMethods::BANK_TRANSFER === $payment_method ) {
135
				/*
136
				 * Set payment redirect message with received transaction reference.
137
				 *
138
				 * @link https://s3-eu-west-1.amazonaws.com/wl1-apidocs/api.kassacompleet.nl/index.html#payment-methods-without-the-redirect-flow-performing_redirect-requirement
139
				 */
140
				$message = sprintf(
141
					/* translators: 1: payment provider name, 2: PSP account name, 3: PSP account number, 4: PSP account BIC, 5: formatted amount, 6: transaction reference */
142
					__(
143
						'You have chosen the payment method "Bank transfer". To complete your payment, please transfer the amount to the payment service provider (%1$s).
144
145
<strong>Account holder:</strong> %2$s
146
<strong>Account IBAN:</strong> %3$s
147
<strong>Account BIC:</strong> %4$s
148
<strong>Amount:</strong> %5$s
149
<strong>Transaction reference:</strong> %6$s
150
151
<em>Please note: only payments with the mentioned transaction reference can be processed.</em>',
152
						'pronamic_ideal'
153
					),
154
					__( 'ING', 'pronamic_ideal' ),
155
					'ING PSP',
156
					'NL13INGB0005300060',
157
					'INGBNL2A',
158
					$payment->get_total_amount()->format_i18n(),
159
					$order->transactions[0]->payment_method_details->reference
160
				);
161
162
				$payment->set_meta( 'payment_redirect_message', $message );
163
			}
164
165
			if ( isset( $order->transactions[0]->payment_url ) ) {
166
				$action_url = $order->transactions[0]->payment_url;
167
			}
168
169
			$payment->set_action_url( $action_url );
170
		}
171
172
		$error = $this->client->get_error();
173
174
		if ( is_wp_error( $error ) ) {
175
			$this->error = $error;
176
		}
177
	}
178
179
	/**
180
	 * Update status of the specified payment
181
	 *
182
	 * @param Payment $payment Payment.
183
	 */
184
	public function update_status( Payment $payment ) {
185
		$transaction_id = $payment->get_transaction_id();
186
187
		if ( empty( $transaction_id ) ) {
188
			return;
189
		}
190
191
		$order = $this->client->get_order( $transaction_id );
192
193
		if ( ! is_object( $order ) ) {
194
			return;
195
		}
196
197
		$payment->set_status( Statuses::transform( $order->status ) );
198
199
		if ( isset( $order->transactions[0]->payment_method_details ) ) {
200
			$details = $order->transactions[0]->payment_method_details;
201
202
			if ( isset( $details->consumer_name ) ) {
203
				$payment->set_consumer_name( $details->consumer_name );
204
			}
205
206
			if ( isset( $details->consumer_iban ) ) {
207
				$payment->set_consumer_iban( $details->consumer_iban );
208
			}
209
		}
210
211
		$error = $this->client->get_error();
212
213
		if ( is_wp_error( $error ) ) {
214
			$this->error = $error;
215
		}
216
	}
217
}
218