Passed
Push — main ( 061772...28b955 )
by Remco
07:49 queued 12s
created

Gateway::start()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 8.3844

Importance

Changes 0
Metric Value
cc 8
nc 16
nop 1
dl 0
loc 63
ccs 27
cts 33
cp 0.8182
crap 8.3844
rs 7.5628
c 0
b 0
f 0

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.0.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>>>
0 ignored issues
show
Documentation introduced by
The doc-type array<int, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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::RABOBANK              => \__( 'Rabobank', 'pronamic_ideal' ),
64 1
					IssuerIdIDeal::ING                   => \__( 'ING', 'pronamic_ideal' ),
65 1
					IssuerIdIDeal::SNS                   => \__( 'SNS Bank', 'pronamic_ideal' ),
66 1
					IssuerIdIDeal::ASN                   => \__( 'ASN Bank', 'pronamic_ideal' ),
67 1
					IssuerIdIDeal::REGIOBANK             => \__( 'RegioBank', 'pronamic_ideal' ),
68 1
					IssuerIdIDeal::TRIODOS               => \__( 'Triodos Bank', 'pronamic_ideal' ),
69 1
					IssuerIdIDeal::KNAB                  => \__( 'Knab', 'pronamic_ideal' ),
70 1
					IssuerIdIDeal::VAN_LANSCHOT_BANKIERS => \__( 'Van Lanschot Bankiers', 'pronamic_ideal' ),
71 1
					IssuerIdIDeal::BUNQ                  => \__( 'Bunq', 'pronamic_ideal' ),
72 1
					IssuerIdIDeal::MONEYOU               => \__( 'Moneyou', 'pronamic_ideal' ),
73 1
					IssuerIdIDeal::HANDELSBANKEN         => \__( 'Handelsbanken', 'pronamic_ideal' ),
74
				),
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 1
	public function get_supported_payment_methods() {
86
		return array(
87 1
			PaymentMethods::IDEAL,
88
		);
89
	}
90
91
	/**
92
	 * Is payment method required to start transaction?
93
	 *
94
	 * @see Core_Gateway::payment_method_is_required()
95
	 * @return true
96
	 */
97 1
	public function payment_method_is_required() {
98 1
		return true;
99
	}
100
101
	/**
102
	 * Start.
103
	 *
104
	 * @param Payment $payment Payment.
105
	 * @return void
106
	 * @throws \InvalidArgumentException Throws exception if payment ID or currency is empty.
107
	 * @see Plugin::start()
108
	 */
109 1
	public function start( Payment $payment ) {
110 1
		$header = new RequestHeader( $this->config->get_business_id() );
111
112 1
		$payment_id = $payment->get_id();
113
114 1
		if ( null === $payment_id ) {
115
			throw new \InvalidArgumentException( 'Can not start payment with empty ID.' );
116
		}
117
118 1
		$currency_code = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
119
120 1
		if ( null === $currency_code ) {
121
			throw new \InvalidArgumentException( 'Can not start payment with empty currency code.' );
122
		}
123
124 1
		$tracking_code = TrackingCode::from_id( $payment_id );
125
126 1
		$transaction = new Transaction(
127 1
			$this->config->get_store_id(),
128 1
			$payment->get_total_amount()->get_value(),
129
			$currency_code,
130
			$tracking_code
131
		);
132
133 1
		$transaction->set_purchase_id( \strval( $payment->get_id() ) );
134 1
		$transaction->set_return_url( $payment->get_return_url() );
135 1
		$transaction->set_brand_id( BrandId::from_core( $payment->get_method() ) );
136
137 1
		$payment_request = new PaymentRequest( $header, $transaction );
138
139
		// iDEAL.
140 1
		if ( BrandId::IDEAL === $transaction->get_brand_id() ) {
141
			$bank = new BankDetails();
142
143
			$bank->set_issuer_id( $payment->get_issuer() );
144
145
			$payment_request->set_bank( $bank );
146
		}
147
148 1
		$payment->set_meta( 'payvision_business_id', $this->config->get_business_id() );
149 1
		$payment->set_meta( 'payvision_tracking_code', \strval( $tracking_code ) );
150
151
		// Create payment.
152 1
		$object = $this->client->send_request( 'POST', '/gateway/v3/payments', \wp_json_encode( $payment_request ) );
153
154 1
		$payment_response = PaymentResponse::from_json( $object );
155
156 1
		$error = $payment_response->get_error();
157
158 1
		if ( null !== $error ) {
159
			throw $error;
160
		}
161
162 1
		if ( null !== $payment_response->redirect ) {
163 1
			if ( null !== $payment_response->redirect->url ) {
164 1
				$payment->set_action_url( $payment_response->redirect->url );
165
			}
166
		}
167
168 1
		if ( null !== $payment_response->transaction ) {
169 1
			$payment->set_transaction_id( $payment_response->transaction->id );
170
		}
171 1
	}
172
173
	/**
174
	 * Update status of the specified payment.
175
	 *
176
	 * @param Payment $payment Payment.
177
	 * @return void
178
	 */
179 5
	public function update_status( Payment $payment ) {
180 5
		$id = $payment->get_transaction_id();
181
182
		// Get payment.
183 5
		$object = $this->client->send_request(
184 5
			'GET',
185 5
			'/gateway/v3/payments/' . $id,
186
			array(
187 5
				'businessId' => $payment->get_meta( 'payvision_business_id' ),
188
			)
189
		);
190
191 1
		$response = PaymentResponse::from_json( $object );
192
193
		// Update payment status.
194 1
		$result_code = $response->get_result();
195
196 1
		$status = ResultCode::to_core( $result_code );
197
198 1
		if ( null !== $status ) {
199 1
			$payment->set_status( $status );
200
		}
201
202
		// Add error as note.
203 1
		$error = $response->get_error();
204
205 1
		if ( null !== $error ) {
206
			$payment->add_note( \sprintf( '%s: %s', $error->get_code(), $error->get_message() ) );
207
		}
208 1
	}
209
}
210