Failed Conditions
Push — develop ( 68fff7...ddcd41 )
by Remco
06:04
created

src/Gateway.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Gateway.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\IDealAdvancedV3;
12
13
use Pronamic\WordPress\Pay\Banks\BankAccountDetails;
14
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
15
use Pronamic\WordPress\Pay\Core\PaymentMethods;
16
use Pronamic\WordPress\Pay\Payments\Payment;
17
18
/**
19
 * Title: iDEAL Advanced v3+ gateway
20
 * Description:
21
 * Copyright: 2005-2021 Pronamic
22
 * Company: Pronamic
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.5
26
 * @since   1.0.0
27
 */
28
class Gateway extends Core_Gateway {
29
	/**
30
	 * Client.
31
	 *
32
	 * @var Client
33
	 */
34
	protected $client;
35
36
	/**
37
	 * Constructs and initializes an iDEAL Advanced v3 gateway
38
	 *
39
	 * @param Config $config Config.
40
	 */
41
	public function __construct( Config $config ) {
42
		parent::__construct( $config );
43
44
		$this->set_method( self::METHOD_HTTP_REDIRECT );
45
46
		// Supported features.
47
		$this->supports = array(
48
			'payment_status_request',
49
		);
50
51
		// Client.
52
		$client = new Client();
53
54
		$client->set_acquirer_url( (string) $config->get_payment_server_url() );
55
56
		$client->merchant_id          = (string) $config->get_merchant_id();
57
		$client->sub_id               = (string) $config->get_sub_id();
58
		$client->private_key          = (string) $config->get_private_key();
59
		$client->private_key_password = (string) $config->get_private_key_password();
60
		$client->private_certificate  = (string) $config->get_private_certificate();
61
62
		$this->client = $client;
63
	}
64
65
	/**
66
	 * Get issuers
67
	 *
68
	 * @see Core_Gateway::get_issuers()
69
	 * @return array<int, array<string, array<string, string>|string>>
70
	 */
71
	public function get_issuers() {
72
		$groups = array();
73
74
		try {
75
			$directory = $this->client->get_directory();
76
		} catch ( \Exception $e ) {
77
			$this->error = new \WP_Error( 'ideal_advanced_v3_error', $e->getMessage() );
78
79
			return $groups;
80
		}
81
82
		if ( null === $directory ) {
83
			return $groups;
84
		}
85
86
		foreach ( $directory->get_countries() as $country ) {
87
			$issuers = array();
88
89
			foreach ( $country->get_issuers() as $issuer ) {
90
				$id   = $issuer->get_id();
91
				$name = $issuer->get_name();
92
93
				if ( null === $id || null === $name ) {
94
					continue;
95
				}
96
97
				$issuers[ $id ] = $name;
98
			}
99
100
			$groups[] = array(
101
				'name'    => $country->get_name(),
102
				'options' => $issuers,
103
			);
104
		}
105
106
		return $groups;
107
	}
108
109
	/**
110
	 * Get supported payment methods
111
	 *
112
	 * @see Core_Gateway::get_supported_payment_methods()
113
	 * @return array<int, string>
114
	 */
115
	public function get_supported_payment_methods() {
116
		return array(
117
			PaymentMethods::IDEAL,
118
		);
119
	}
120
121
	/**
122
	 * Is payment method required to start transaction?
123
	 *
124
	 * @see   Core_Gateway::payment_method_is_required()
125
	 * @since 1.1.5
126
	 */
127
	public function payment_method_is_required() {
128
		return true;
129
	}
130
131
	/**
132
	 * Start
133
	 *
134
	 * @see Core_Gateway::start()
135
	 *
136
	 * @param Payment $payment Payment.
137
	 */
138
	public function start( Payment $payment ) {
139
		// Purchase ID.
140
		$purchase_id = $payment->format_string( $this->config->get_purchase_id() );
141
142
		$payment->set_meta( 'purchase_id', $purchase_id );
143
144
		// Transaction.
145
		$transaction = new Transaction();
146
		$transaction->set_purchase_id( $purchase_id );
147
		$transaction->set_amount( $payment->get_total_amount()->number_format( null, '.', '' ) );
0 ignored issues
show
The method number_format() does not exist on Pronamic\WordPress\Money\TaxedMoney. Did you maybe mean format()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
		$transaction->set_amount( $payment->get_total_amount()->/** @scrutinizer ignore-call */ number_format( null, '.', '' ) );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
		$transaction->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() );
149
		$transaction->set_expiration_period( 'PT30M' );
150
		$transaction->set_description( $payment->get_description() );
151
		$transaction->set_entrance_code( $payment->get_entrance_code() );
152
153
		$customer = $payment->get_customer();
154
155
		if ( null !== $customer ) {
156
			$transaction->set_language( $customer->get_language() );
157
		}
158
159
		// Create transaction.
160
		$result = $this->client->create_transaction( $transaction, $payment->get_return_url(), (string) $payment->get_issuer() );
161
162
		if ( null !== $result->issuer ) {
163
			$authentication_url = $result->issuer->get_authentication_url();
164
165
			if ( null !== $authentication_url ) {
166
				$payment->set_action_url( $authentication_url );
167
			}
168
		}
169
170
		if ( null !== $result->transaction ) {
171
			$payment->set_transaction_id( $result->transaction->get_id() );
172
		}
173
	}
174
175
	/**
176
	 * Update status of the specified payment
177
	 *
178
	 * @param Payment $payment Payment.
179
	 */
180
	public function update_status( Payment $payment ) {
181
		$transaction_id = (string) $payment->get_transaction_id();
182
183
		// Try to retrieve payment status.
184
		try {
185
			$result = $this->client->get_status( $transaction_id );
186
		} catch ( \Exception $e ) {
187
			$note = sprintf(
188
				/* translators: %s: exception message */
189
				__( 'Error getting payment status: %s', 'pronamic_ideal' ),
190
				$e->getMessage()
191
			);
192
193
			$payment->add_note( $note );
194
195
			return;
196
		}
197
198
		// Check transaction result.
199
		if ( null === $result->transaction ) {
200
			return;
201
		}
202
203
		// Update payment with transaction data.
204
		$transaction = $result->transaction;
205
206
		$payment->set_status( $transaction->get_status() );
207
208
		$consumer_bank_details = $payment->get_consumer_bank_details();
209
210
		if ( null === $consumer_bank_details ) {
211
			$consumer_bank_details = new BankAccountDetails();
212
213
			$payment->set_consumer_bank_details( $consumer_bank_details );
214
		}
215
216
		$consumer_bank_details->set_name( $transaction->get_consumer_name() );
217
		$consumer_bank_details->set_iban( $transaction->get_consumer_iban() );
218
		$consumer_bank_details->set_bic( $transaction->get_consumer_bic() );
219
	}
220
}
221