Failed Conditions
Push — develop ( f91526...05c5cb )
by Reüel
03:39
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
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
	 * @see Pronamic_WP_Pay_Gateway::start()
94
	 */
95
	public function start( Payment $payment ) {
96
		$request = new OrderRequest();
97
98
		$request->currency          = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
99
		$request->amount            = $payment->get_total_amount()->get_cents();
100
		$request->merchant_order_id = $payment->get_order_id();
101
		$request->description       = $payment->get_description();
102
		$request->return_url        = $payment->get_return_url();
103
104
		// To make the 'Test' meta box work, set payment method to iDEAL if an issuer_id has been set.
105
		$issuer = $payment->get_issuer();
106
107
		$payment_method = $payment->get_method();
108
109
		if ( empty( $payment_method ) && ! empty( $issuer ) ) {
110
			$payment_method = PaymentMethods::IDEAL;
111
		}
112
113
		if ( PaymentMethods::IDEAL === $payment_method ) {
114
			$request->issuer = $issuer;
115
		}
116
117
		$request->method = Methods::transform( $payment_method );
118
119
		$order = $this->client->create_order( $request );
120
121
		if ( $order ) {
122
			$payment->set_transaction_id( $order->id );
123
124
			$action_url = add_query_arg( array(
125
				'payment_redirect' => $payment->get_id(),
126
				'key'              => $payment->key,
127
			), home_url( '/' ) );
128
129
			if ( PaymentMethods::BANK_TRANSFER === $payment_method ) {
130
				// Set payment redirect message with received transaction reference.
131
				// @link https://s3-eu-west-1.amazonaws.com/wl1-apidocs/api.kassacompleet.nl/index.html#payment-methods-without-the-redirect-flow-performing_redirect-requirement
132
				$message = sprintf(
133
					/* translators: 1: payment provider name, 2: PSP account name, 3: PSP account number, 4: PSP account BIC, 5: formatted amount, 6: transaction reference */
134
					__( 'You have chosen the payment method "Bank transfer". To complete your payment, please transfer the amount to the payment service provider (%1$s).
135
136
<strong>Account holder:</strong> %2$s
137
<strong>Account IBAN:</strong> %3$s
138
<strong>Account BIC:</strong> %4$s
139
<strong>Amount:</strong> %5$s
140
<strong>Transaction reference:</strong> %6$s
141
142
<em>Please note: only payments with the mentioned transaction reference can be processed.</em>', 'pronamic_ideal' ),
143
					__( 'ING', 'pronamic_ideal' ),
144
					'ING PSP',
145
					'NL13INGB0005300060',
146
					'INGBNL2A',
147
					$payment->get_total_amount()->format_i18n(),
148
					$order->transactions[0]->payment_method_details->reference
149
				);
150
151
				$payment->set_meta( 'payment_redirect_message', $message );
152
			}
153
154
			if ( isset( $order->transactions[0]->payment_url ) ) {
155
				$action_url = $order->transactions[0]->payment_url;
156
			}
157
158
			$payment->set_action_url( $action_url );
159
		}
160
161
		$error = $this->client->get_error();
162
163
		if ( is_wp_error( $error ) ) {
164
			$this->error = $error;
165
		}
166
	}
167
168
	/**
169
	 * Update status of the specified payment
170
	 *
171
	 * @param Payment $payment
172
	 */
173
	public function update_status( Payment $payment ) {
174
		$transaction_id = $payment->get_transaction_id();
175
176
		if ( empty( $transaction_id ) ) {
177
			return;
178
		}
179
180
		$order = $this->client->get_order( $transaction_id );
181
182
		if ( ! is_object( $order ) ) {
183
			return;
184
		}
185
186
		$payment->set_status( Statuses::transform( $order->status ) );
187
188
		if ( isset( $order->transactions[0]->payment_method_details ) ) {
189
			$details = $order->transactions[0]->payment_method_details;
190
191
			if ( isset( $details->consumer_name ) ) {
192
				$payment->set_consumer_name( $details->consumer_name );
193
			}
194
195
			if ( isset( $details->consumer_iban ) ) {
196
				$payment->set_consumer_iban( $details->consumer_iban );
197
			}
198
		}
199
200
		$error = $this->client->get_error();
201
202
		if ( is_wp_error( $error ) ) {
203
			$this->error = $error;
204
		}
205
	}
206
}
207