Failed Conditions
Push — feature/webhook-status ( 7d098e )
by Reüel
05:54
created

Gateway::get_supported_features()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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