Failed Conditions
Push — develop ( b48a0e...170d92 )
by Reüel
15:46
created

src/Gateway.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Gateway.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet;
12
13
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
14
use Pronamic\WordPress\Pay\Core\PaymentMethods as Core_PaymentMethods;
15
use Pronamic\WordPress\Pay\Payments\Payment;
16
17
/**
18
 * Title: ING Kassa Compleet
19
 * Description:
20
 * Copyright: 2005-2019 Pronamic
21
 * Company: Pronamic
22
 *
23
 * @author  Reüel van der Steege
24
 * @version 2.0.1
25
 * @since   1.0.0
26
 */
27
class Gateway extends Core_Gateway {
28
	/**
29
	 * Client.
30
	 *
31
	 * @var Client
32
	 */
33
	protected $client;
34
35
	/**
36
	 * Constructs and initializes an ING Kassa Compleet 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_HTTP_REDIRECT );
44
45
		// Supported features.
46
		$this->supports = array(
47
			'payment_status_request',
48
		);
49
50
		// Client.
51
		$this->client = new Client( $config->api_key );
52
	}
53
54
	/**
55
	 * Get issuers
56
	 *
57
	 * @see Core_Gateway::get_issuers()
58
	 */
59
	public function get_issuers() {
60
		$groups = array();
61
62
		try {
63
			$result = $this->client->get_issuers();
64
		} catch ( \Pronamic\WordPress\Pay\PayException $e ) {
0 ignored issues
show
The type Pronamic\WordPress\Pay\PayException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
65
			// @todo What todo on error?
66
			return $groups;
67
		}
68
69
		if ( is_array( $result ) ) {
70
			$groups[] = array(
71
				'options' => $result,
72
			);
73
		}
74
75
		return $groups;
76
	}
77
78
	/**
79
	 * Get supported payment methods
80
	 *
81
	 * @see Core_Gateway::get_supported_payment_methods()
82
	 */
83
	public function get_supported_payment_methods() {
84
		return array(
85
			Core_PaymentMethods::BANCONTACT,
86
			Core_PaymentMethods::BANK_TRANSFER,
87
			Core_PaymentMethods::CREDIT_CARD,
88
			Core_PaymentMethods::IDEAL,
89
			Core_PaymentMethods::PAYCONIQ,
90
			Core_PaymentMethods::PAYPAL,
91
			Core_PaymentMethods::SOFORT,
92
		);
93
	}
94
95
	/**
96
	 * Is payment method required to start transaction?
97
	 *
98
	 * @see Core_Gateway::payment_method_is_required()
99
	 */
100
	public function payment_method_is_required() {
101
		return true;
102
	}
103
104
	/**
105
	 * Start
106
	 *
107
	 * @param Payment $payment Payment.
108
	 *
109
	 * @see Core_Gateway::start()
110
	 */
111
	public function start( Payment $payment ) {
112
		$request = new OrderRequest();
113
114
		$request->currency          = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
115
		$request->amount            = $payment->get_total_amount()->get_cents();
116
		$request->merchant_order_id = $payment->get_order_id();
117
		$request->description       = $payment->get_description();
118
		$request->return_url        = $payment->get_return_url();
119
120
		// To make the 'Test' meta box work, set payment method to iDEAL if an issuer_id has been set.
121
		$issuer = $payment->get_issuer();
122
123
		$payment_method = $payment->get_method();
124
125
		if ( empty( $payment_method ) && ! empty( $issuer ) ) {
126
			$payment_method = Core_PaymentMethods::IDEAL;
127
		}
128
129
		if ( Core_PaymentMethods::IDEAL === $payment_method ) {
130
			$request->issuer = $issuer;
131
		}
132
133
		$request->method = PaymentMethods::transform( $payment_method );
134
135
		// Create order.
136
		$order = $this->client->create_order( $request );
137
138
		if ( $order ) {
139
			// Transaction ID.
140
			$payment->set_transaction_id( $order->id );
141
142
			// Action URL.
143
			$action_url = $payment->get_pay_redirect_url();
144
145
			if ( isset( $order->transactions[0]->payment_url ) ) {
146
				$action_url = $order->transactions[0]->payment_url;
147
			}
148
149
			$payment->set_action_url( $action_url );
150
151
			// Bank transfer payment redirect message.
152
			if ( Core_PaymentMethods::BANK_TRANSFER === $payment_method ) {
153
				/*
154
				 * Set payment redirect message with received transaction reference.
155
				 *
156
				 * @link https://s3-eu-west-1.amazonaws.com/wl1-apidocs/api.kassacompleet.nl/index.html#payment-methods-without-the-redirect-flow-performing_redirect-requirement
157
				 */
158
				$message = sprintf(
159
					/* translators: 1: payment provider name, 2: PSP account name, 3: PSP account number, 4: PSP account BIC, 5: formatted amount, 6: transaction reference */
160
					__(
161
						'You have chosen the payment method "Bank transfer". To complete your payment, please transfer the amount to the payment service provider (%1$s).
162
163
<strong>Account holder:</strong> %2$s
164
<strong>Account IBAN:</strong> %3$s
165
<strong>Account BIC:</strong> %4$s
166
<strong>Amount:</strong> %5$s
167
<strong>Transaction reference:</strong> %6$s
168
169
<em>Please note: only payments with the mentioned transaction reference can be processed.</em>',
170
						'pronamic_ideal'
171
					),
172
					__( 'ING', 'pronamic_ideal' ),
173
					'ING PSP',
174
					'NL13INGB0005300060',
175
					'INGBNL2A',
176
					$payment->get_total_amount()->format_i18n(),
177
					$order->transactions[0]->payment_method_details->reference
178
				);
179
180
				$payment->set_meta( 'payment_redirect_message', $message );
181
			}
182
		}
183
	}
184
185
	/**
186
	 * Update status of the specified payment
187
	 *
188
	 * @param Payment $payment Payment.
189
	 */
190
	public function update_status( Payment $payment ) {
191
		$transaction_id = $payment->get_transaction_id();
192
193
		if ( empty( $transaction_id ) ) {
194
			return;
195
		}
196
197
		try {
198
			$order = $this->client->get_order( $transaction_id );
199
		} catch ( \Pronamic\WordPress\Pay\PayException $e ) {
200
			$payment->add_note( sprintf( '%s: %s', $e->get_error_code(), $e->get_message() ) );
201
202
			return;
203
		}
204
205
		if ( ! is_object( $order ) ) {
206
			return;
207
		}
208
209
		$payment->set_status( Statuses::transform( $order->status ) );
210
211
		if ( isset( $order->transactions[0]->payment_method_details ) ) {
212
			$details = $order->transactions[0]->payment_method_details;
213
214
			if ( isset( $details->consumer_name ) ) {
215
				$payment->set_consumer_name( $details->consumer_name );
216
			}
217
218
			if ( isset( $details->consumer_iban ) ) {
219
				$payment->set_consumer_iban( $details->consumer_iban );
220
			}
221
		}
222
	}
223
}
224