Test Failed
Push — develop ( 2a4df7...d3992e )
by Reüel
04:51
created

Gateway::payment_method_is_required()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
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 = array(
47
			'payment_status_request',
48
		);
49
50
		// Client.
51
		$this->client = new Client( $config->api_key );
52
	}
53
54
	/**
55
	 * Get issuers
56
	 *
57
	 * @see Core_Gateway::get_issuers()
58
	 */
59
	public function get_issuers() {
60
		$groups = array();
61
62
		$result = $this->client->get_issuers();
63
64
		if ( is_array( $result ) ) {
65
			$groups[] = array(
66
				'options' => $result,
67
			);
68
		}
69
70
		$error = $this->client->get_error();
71
72
		if ( is_wp_error( $error ) ) {
73
			$this->error = $error;
74
		}
75
76
		return $groups;
77
	}
78
79
	/**
80
	 * Get supported payment methods
81
	 *
82
	 * @see Core_Gateway::get_supported_payment_methods()
83
	 */
84
	public function get_supported_payment_methods() {
85
		return array(
86
			Core_PaymentMethods::BANCONTACT,
87
			Core_PaymentMethods::BANK_TRANSFER,
88
			Core_PaymentMethods::CREDIT_CARD,
89
			Core_PaymentMethods::IDEAL,
90
			Core_PaymentMethods::PAYCONIQ,
91
			Core_PaymentMethods::PAYPAL,
92
			Core_PaymentMethods::SOFORT,
93
		);
94
	}
95
96
	/**
97
	 * Start
98
	 *
99
	 * @param Payment $payment Payment.
100
	 *
101
	 * @see Core_Gateway::start()
102
	 */
103
	public function start( Payment $payment ) {
104
		$request = new OrderRequest();
105
106
		$request->currency          = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
107
		$request->amount            = $payment->get_total_amount()->get_cents();
0 ignored issues
show
Deprecated Code introduced by
The function Pronamic\WordPress\Money\Money::get_cents() has been deprecated: 1.2.2 Use `Money::get_minor_units()` instead. ( Ignorable by Annotation )

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

107
		$request->amount            = /** @scrutinizer ignore-deprecated */ $payment->get_total_amount()->get_cents();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
108
		$request->merchant_order_id = $payment->get_order_id();
109
		$request->description       = $payment->get_description();
110
		$request->return_url        = $payment->get_return_url();
111
112
		// To make the 'Test' meta box work, set payment method to iDEAL if an issuer_id has been set.
113
		$issuer = $payment->get_issuer();
114
115
		$payment_method = $payment->get_method();
116
117
		if ( Core_PaymentMethods::IDEAL === $payment_method ) {
118
			$request->issuer = $issuer;
119
		}
120
121
		$request->method = PaymentMethods::transform( $payment_method );
122
123
		$order = $this->client->create_order( $request );
124
125
		if ( $order ) {
126
			$payment->set_transaction_id( $order->id );
127
128
			// Set action URL to order pay URL.
129
			if ( isset( $order->order_url ) ) {
130
				$payment->set_action_url( $order->order_url );
131
			}
132
133
			// Set action URL to transction payment URL (only if payment method is set).
134
			if ( isset( $order->transactions[0]->payment_url ) ) {
135
				$payment->set_action_url( $order->transactions[0]->payment_url );
136
			}
137
138
			// Bank transfer instructions.
139
			if ( Core_PaymentMethods::BANK_TRANSFER === $payment_method && isset( $order->transactions[0]->id ) ) {
140
				$payment->set_action_url(
141
					\sprintf(
142
						'https://api.kassacompleet.nl/pay/%s/completed/',
143
						esc_html( $order->transactions[0]->id )
144
					)
145
				);
146
			}
147
		}
148
149
		$error = $this->client->get_error();
150
151
		if ( is_wp_error( $error ) ) {
152
			$this->error = $error;
153
		}
154
	}
155
156
	/**
157
	 * Update status of the specified payment
158
	 *
159
	 * @param Payment $payment Payment.
160
	 */
161
	public function update_status( Payment $payment ) {
162
		$transaction_id = $payment->get_transaction_id();
163
164
		if ( empty( $transaction_id ) ) {
165
			return;
166
		}
167
168
		$order = $this->client->get_order( $transaction_id );
169
170
		if ( ! is_object( $order ) ) {
171
			return;
172
		}
173
174
		$payment->set_status( Statuses::transform( $order->status ) );
175
176
		if ( isset( $order->transactions[0]->payment_method_details ) ) {
177
			$details = $order->transactions[0]->payment_method_details;
178
179
			if ( isset( $details->consumer_name ) ) {
180
				$payment->set_consumer_name( $details->consumer_name );
181
			}
182
183
			if ( isset( $details->consumer_iban ) ) {
184
				$payment->set_consumer_iban( $details->consumer_iban );
185
			}
186
		}
187
188
		$error = $this->client->get_error();
189
190
		if ( is_wp_error( $error ) ) {
191
			$this->error = $error;
192
		}
193
	}
194
}
195