Failed Conditions
Push — main ( b61967...dcc89f )
by Remco
06:08 queued 13s
created

Gateway::start()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 66
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 7.2056

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 31
c 2
b 0
f 0
nc 15
nop 1
dl 0
loc 66
rs 8.4906
ccs 26
cts 31
cp 0.8387
crap 7.2056

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\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::MONEYOU               => \__( 'Moneyou', 'pronamic_ideal' ),
70 1
					IssuerIdIDeal::REGIOBANK             => \__( 'RegioBank', 'pronamic_ideal' ),
71 1
					IssuerIdIDeal::REVOLUT               => \__( 'Revolut', 'pronamic_ideal' ),
72 1
					IssuerIdIDeal::SNS                   => \__( 'SNS Bank', 'pronamic_ideal' ),
73 1
					IssuerIdIDeal::TRIODOS               => \__( 'Triodos Bank', 'pronamic_ideal' ),
74 1
					IssuerIdIDeal::VAN_LANSCHOT_BANKIERS => \__( 'Van Lanschot', 'pronamic_ideal' ),
75
				),
76
			),
77
		);
78
	}
79
80
	/**
81
	 * Get supported payment methods
82
	 *
83
	 * @see Core_Gateway::get_supported_payment_methods()
84
	 * @return array<string>
85
	 */
86 1
	public function get_supported_payment_methods() {
87
		return array(
88 1
			PaymentMethods::IDEAL,
89
			PaymentMethods::PAYPAL,
90
		);
91
	}
92
93
	/**
94
	 * Is payment method required to start transaction?
95
	 *
96
	 * @see Core_Gateway::payment_method_is_required()
97
	 * @return true
98
	 */
99 1
	public function payment_method_is_required() {
100 1
		return true;
101
	}
102
103
	/**
104
	 * Start.
105
	 *
106
	 * @param Payment $payment Payment.
107
	 * @return void
108
	 * @throws \InvalidArgumentException Throws exception if payment ID or currency is empty.
109
	 */
110 1
	public function start( Payment $payment ) {
111 1
		$header = new RequestHeader( $this->config->get_business_id() );
112
113 1
		$payment_id = $payment->get_id();
114
115 1
		if ( null === $payment_id ) {
116
			throw new \InvalidArgumentException( 'Can not start payment with empty ID.' );
117
		}
118
119 1
		$tracking_code = TrackingCode::from_id( $payment_id );
120
121 1
		$transaction = new Transaction(
122 1
			$this->config->get_store_id(),
123
			/**
124
			 * Amounts are to be formatted using a dot (“.”) as a decimal
125
			 * separator. The maximum number of decimals depends on the
126
			 * currency used and it is specified according to ISO 4217.
127
			 * It is not needed to specify all decimals, e.g. €5.00 can be
128
			 * formatted as “5”, “5.0” or “5.00".The maximum amount depends
129
			 * on the brand used.
130
			 *
131
			 * @link https://developers.acehubpaymentservices.com/docs/data-types
132
			 */
133 1
			$payment->get_total_amount()->get_number()->format( null, '.', '' ),
134 1
			$payment->get_total_amount()->get_currency()->get_alphabetic_code(),
135
			$tracking_code
136
		);
137
138 1
		$transaction->set_purchase_id( $payment->format_string( (string) $this->config->get_purchase_id() ) );
139 1
		$transaction->set_return_url( $payment->get_return_url() );
140 1
		$transaction->set_brand_id( BrandId::from_core( $payment->get_method() ) );
141 1
		$transaction->set_descriptor( DataHelper::sanitize_an( (string) $payment->get_description(), 127 ) );
142
143 1
		$payment_request = new PaymentRequest( $header, $transaction );
144
145
		// iDEAL.
146 1
		if ( BrandId::IDEAL === $transaction->get_brand_id() ) {
147
			$bank = new BankDetails();
148
149
			$bank->set_issuer_id( $payment->get_issuer() );
150
151
			$payment_request->set_bank( $bank );
152
		}
153
154 1
		$payment->set_meta( 'payvision_business_id', $this->config->get_business_id() );
155 1
		$payment->set_meta( 'payvision_tracking_code', \strval( $tracking_code ) );
156
157
		// Create payment.
158 1
		$object = $this->client->send_request( 'POST', '/gateway/v3/payments', \wp_json_encode( $payment_request ) );
159
160 1
		$payment_response = PaymentResponse::from_json( $object );
161
162 1
		$error = $payment_response->get_error();
163
164 1
		if ( null !== $error ) {
165
			throw $error;
166
		}
167
168 1
		if ( null !== $payment_response->redirect ) {
169 1
			if ( null !== $payment_response->redirect->url ) {
170 1
				$payment->set_action_url( $payment_response->redirect->url );
171
			}
172
		}
173
174 1
		if ( null !== $payment_response->transaction ) {
175 1
			$payment->set_transaction_id( $payment_response->transaction->id );
176
		}
177 1
	}
178
179
	/**
180
	 * Update status of the specified payment.
181
	 *
182
	 * @param Payment $payment Payment.
183
	 * @return void
184
	 */
185 5
	public function update_status( Payment $payment ) {
186 5
		$id = $payment->get_transaction_id();
187
188
		// Get payment.
189 5
		$object = $this->client->send_request(
190 5
			'GET',
191 5
			'/gateway/v3/payments/' . $id,
192
			array(
193 5
				'businessId' => $payment->get_meta( 'payvision_business_id' ),
194
			)
195
		);
196
197 1
		$response = PaymentResponse::from_json( $object );
198
199
		// Update payment status.
200 1
		$result_code = $response->get_result();
201
202 1
		$status = ResultCode::to_core( $result_code );
203
204 1
		if ( null !== $status ) {
205 1
			$payment->set_status( $status );
206
		}
207
208
		// Add error as note.
209 1
		$error = $response->get_error();
210
211 1
		if ( null !== $error ) {
212
			$payment->add_note( \sprintf( '%s: %s', $error->get_code(), $error->get_message() ) );
213
		}
214 1
	}
215
}
216