Gateway::get_issuers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 16
ccs 13
cts 13
cp 1
crap 1
rs 9.7666
c 1
b 0
f 0
1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Payvision
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Payvision;
12
13
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
14
use Pronamic\WordPress\Pay\Core\PaymentMethods;
15
use Pronamic\WordPress\Pay\Payments\Payment;
16
17
/**
18
 * Gateway
19
 *
20
 * @link    https://github.com/payvisionpayments/php/blob/master/generatepaymentform.php
21
 * @author  Remco Tolsma
22
 * @version 1.1.0
23
 * @since   1.0.0
24
 */
25
class Gateway extends Core_Gateway {
26
	/**
27
	 * Client.
28
	 *
29
	 * @var Client
30
	 */
31
	private $client;
32
33
	/**
34
	 * Constructs and initializes an Payvision gateway.
35
	 *
36
	 * @param Config $config Config.
37
	 */
38 8
	public function __construct( Config $config ) {
39 8
		parent::__construct( $config );
40
41 8
		$this->set_method( self::METHOD_HTTP_REDIRECT );
42
43
		// Supported features.
44 8
		$this->supports = array(
45
			'payment_status_request',
46
		);
47
48
		// Client.
49 8
		$this->client = new Client( $config );
50 8
	}
51
52
	/**
53
	 * Get issuers
54
	 *
55
	 * @see Core_Gateway::get_issuers()
56
	 * @return array<int, array<string, array<string>>>
57
	 */
58 1
	public function get_issuers() {
59
		return array(
60
			array(
61
				'options' => array(
62 1
					IssuerIdIDeal::ABN_AMRO              => \__( 'ABN Amro', 'pronamic_ideal' ),
63 1
					IssuerIdIDeal::ING                   => \__( 'ING', 'pronamic_ideal' ),
64 1
					IssuerIdIDeal::RABOBANK              => \__( 'Rabobank', 'pronamic_ideal' ),
65 1
					IssuerIdIDeal::ASN                   => \__( 'ASN Bank', 'pronamic_ideal' ),
66 1
					IssuerIdIDeal::BUNQ                  => \__( 'Bunq', 'pronamic_ideal' ),
67 1
					IssuerIdIDeal::HANDELSBANKEN         => \__( 'Handelsbanken', 'pronamic_ideal' ),
68 1
					IssuerIdIDeal::KNAB                  => \__( 'Knab', 'pronamic_ideal' ),
69 1
					IssuerIdIDeal::REGIOBANK             => \__( 'RegioBank', 'pronamic_ideal' ),
70 1
					IssuerIdIDeal::REVOLUT               => \__( 'Revolut', 'pronamic_ideal' ),
71 1
					IssuerIdIDeal::SNS                   => \__( 'SNS Bank', 'pronamic_ideal' ),
72 1
					IssuerIdIDeal::TRIODOS               => \__( 'Triodos Bank', 'pronamic_ideal' ),
73 1
					IssuerIdIDeal::VAN_LANSCHOT_BANKIERS => \__( 'Van Lanschot', 'pronamic_ideal' ),
74 1
				),
75
			),
76
		);
77
	}
78
79
	/**
80
	 * Get supported payment methods
81
	 *
82
	 * @see Core_Gateway::get_supported_payment_methods()
83
	 * @return array<string>
84
	 */
85
	public function get_supported_payment_methods() {
86 1
		return array(
87
			PaymentMethods::IDEAL,
88 1
			PaymentMethods::PAYPAL,
89
		);
90
	}
91
92
	/**
93
	 * Is payment method required to start transaction?
94
	 *
95
	 * @see Core_Gateway::payment_method_is_required()
96
	 * @return true
97
	 */
98
	public function payment_method_is_required() {
99 1
		return true;
100 1
	}
101
102
	/**
103
	 * Start.
104
	 *
105
	 * @param Payment $payment Payment.
106
	 * @return void
107
	 * @throws \InvalidArgumentException Throws exception if payment ID or currency is empty.
108
	 */
109
	public function start( Payment $payment ) {
110 1
		$header = new RequestHeader( $this->config->get_business_id() );
111 1
112
		$payment_id = $payment->get_id();
113 1
114
		if ( null === $payment_id ) {
115 1
			throw new \InvalidArgumentException( 'Can not start payment with empty ID.' );
116
		}
117
118
		$tracking_code = TrackingCode::from_id( $payment_id );
119 1
120
		$transaction = new Transaction(
121 1
			$this->config->get_store_id(),
122 1
			/**
123
			 * Amounts are to be formatted using a dot (“.”) as a decimal
124
			 * separator. The maximum number of decimals depends on the
125
			 * currency used and it is specified according to ISO 4217.
126
			 * It is not needed to specify all decimals, e.g. €5.00 can be
127
			 * formatted as “5”, “5.0” or “5.00".The maximum amount depends
128
			 * on the brand used.
129
			 *
130
			 * @link https://developers.acehubpaymentservices.com/docs/data-types
131
			 */
132
			$payment->get_total_amount()->get_number()->format( null, '.', '' ),
133 1
			$payment->get_total_amount()->get_currency()->get_alphabetic_code(),
134 1
			$tracking_code
135
		);
136
137
		$transaction->set_purchase_id( $payment->format_string( (string) $this->config->get_purchase_id() ) );
138 1
		$transaction->set_return_url( $payment->get_return_url() );
139 1
		$transaction->set_brand_id( BrandId::from_core( $payment->get_payment_method() ) );
140 1
		$transaction->set_descriptor( DataHelper::sanitize_an( (string) $payment->get_description(), 127 ) );
141 1
142
		$payment_request = new PaymentRequest( $header, $transaction );
143 1
144
		// iDEAL.
145
		if ( BrandId::IDEAL === $transaction->get_brand_id() ) {
146 1
			$bank = new BankDetails();
147
148
			$bank->set_issuer_id( $payment->get_meta( 'issuer' ) );
0 ignored issues
show
Bug introduced by
It seems like $payment->get_meta('issuer') can also be of type false; however, parameter $issuer_id of Pronamic\WordPress\Pay\G...etails::set_issuer_id() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

148
			$bank->set_issuer_id( /** @scrutinizer ignore-type */ $payment->get_meta( 'issuer' ) );
Loading history...
149
150
			$payment_request->set_bank( $bank );
151
		}
152
153
		$payment->set_meta( 'payvision_business_id', $this->config->get_business_id() );
154 1
		$payment->set_meta( 'payvision_tracking_code', \strval( $tracking_code ) );
155 1
156
		// Create payment.
157
		$object = $this->client->send_request( 'POST', '/gateway/v3/payments', \wp_json_encode( $payment_request ) );
158 1
159
		$payment_response = PaymentResponse::from_json( $object );
160 1
161
		$error = $payment_response->get_error();
162 1
163
		if ( null !== $error ) {
164 1
			throw $error;
165
		}
166
167
		if ( null !== $payment_response->redirect ) {
168 1
			if ( null !== $payment_response->redirect->url ) {
169 1
				$payment->set_action_url( $payment_response->redirect->url );
170 1
			}
171
		}
172
173
		if ( null !== $payment_response->transaction ) {
174 1
			$payment->set_transaction_id( $payment_response->transaction->id );
175 1
		}
176
	}
177 1
178
	/**
179
	 * Update status of the specified payment.
180
	 *
181
	 * @param Payment $payment Payment.
182
	 * @return void
183
	 */
184
	public function update_status( Payment $payment ) {
185 5
		$id = $payment->get_transaction_id();
186 5
187
		// Get payment.
188
		$object = $this->client->send_request(
189 5
			'GET',
190 5
			'/gateway/v3/payments/' . $id,
191 5
			array(
192
				'businessId' => $payment->get_meta( 'payvision_business_id' ),
193 5
			)
194
		);
195
196
		$response = PaymentResponse::from_json( $object );
197 1
198
		// Update payment status.
199
		$result_code = $response->get_result();
200 1
201
		$status = ResultCode::to_core( $result_code );
202 1
203
		if ( null !== $status ) {
204 1
			$payment->set_status( $status );
205 1
		}
206
207
		// Add error as note.
208
		$error = $response->get_error();
209 1
210
		if ( null !== $error ) {
211 1
			$payment->add_note( \sprintf( '%s: %s', $error->get_code(), $error->get_message() ) );
212
		}
213
	}
214
}
215