Failed Conditions
Push — develop ( f08fef...1e4ebf )
by Reüel
05:37
created

Gateway::get_output_html()   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
eloc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
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
use Pronamic\WordPress\Pay\Plugin;
17
18
/**
19
 * Gateway
20
 *
21
 * @author  Remco Tolsma
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 * @link    https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
25
 */
26
class Gateway extends Core_Gateway {
27
	/**
28
	 * Slug of this gateway.
29
	 *
30
	 * @var string
31
	 */
32
	const SLUG = 'adyen';
33
34
	/**
35
	 * Constructs and initializes an Adyen gateway.
36
	 *
37
	 * @param Config $config Config.
38
	 */
39
	public function __construct( Config $config ) {
40
		parent::__construct( $config );
41
42
		$this->set_method( self::METHOD_HTTP_REDIRECT );
43
		$this->set_slug( self::SLUG );
44
45
		$this->client = new Client( $config->api_key, $config->api_live_url_prefix );
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
		$this->client->set_merchant_account( $config->merchant_account );
47
		$this->client->set_mode( $config->mode );
48
	}
49
50
	/**
51
	 * Start.
52
	 *
53
	 * @param Payment $payment Payment.
54
	 *
55
	 * @see Plugin::start()
56
	 */
57
	public function start( Payment $payment ) {
58
		// Payment request.
59
		$request = new PaymentRequest();
60
61
		$request->merchant_account = $this->config->merchant_account;
62
		$request->return_url       = $payment->get_return_url();
63
		$request->reference        = $payment->get_id();
64
65
		// Amount.
66
		$request->currency     = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
67
		$request->amount_value = $payment->get_total_amount()->get_cents(); // @todo Money +get_minor_units().
68
69
		// Payment method. Take leap of faith for unknown payment method types.
70
		$request->payment_method = array(
71
			'type' => PaymentMethodType::transform( $payment->get_method(), $payment->get_method() ),
72
		);
73
74
		switch ( $payment->get_method() ) {
75
			case PaymentMethods::IDEAL:
76
				$request->payment_method['issuer'] = $payment->get_issuer();
77
78
				break;
79
		}
80
81
		// Shopper.
82
		$request->shopper_statement = $payment->get_description();
83
84
		if ( null !== $payment->get_customer() ) {
85
			$request->shopper_ip               = $payment->get_customer()->get_ip_address();
86
			$request->shopper_gender           = $payment->get_customer()->get_gender();
87
			$request->shopper_locale           = $payment->get_customer()->get_locale();
88
			$request->shopper_reference        = $payment->get_customer()->get_user_id();
89
			$request->shopper_telephone_number = $payment->get_customer()->get_phone();
90
91
			if ( null !== $payment->get_customer()->get_name() ) {
92
				$request->shopper_first_name = $payment->get_customer()->get_name()->get_first_name();
93
				$request->shopper_name_infix = $payment->get_customer()->get_name()->get_middle_name();
94
				$request->shopper_last_name  = $payment->get_customer()->get_name()->get_last_name();
95
			}
96
		}
97
98
		// Create payment.
99
		$result = $this->client->create_payment( $request );
100
101
		if ( ! $result ) {
102
			$this->error = $this->client->get_error();
103
104
			return;
105
		}
106
107
		// Set transaction ID.
108
		if ( isset( $result->pspReference ) ) {
109
			$payment->set_transaction_id( $result->pspReference );
110
		}
111
112
		// Set redirect URL.
113
		if ( isset( $result->redirect->url ) ) {
114
			$payment->set_action_url( $result->redirect->url );
115
		}
116
	}
117
118
	/**
119
	 * Update status of the specified payment.
120
	 *
121
	 * @param Payment $payment Payment.
122
	 *
123
	 * @return void
124
	 */
125
	public function update_status( Payment $payment ) {
126
		if ( ! filter_has_var( INPUT_GET, 'payload' ) ) {
127
			return;
128
		}
129
130
		$payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING );
131
132
		$payment_details = $this->client->get_payment_details( $payload );
133
134
		if ( ! $payment_details ) {
135
			$payment->set_status( Core_Statuses::FAILURE );
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\G...ays\Adyen\Core_Statuses was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
136
137
			$this->error = $this->client->get_error();
138
139
			return;
140
		}
141
142
		$payment->set_status( Statuses::transform( $payment_details->resultCode ) );
143
144
		if ( isset( $payment_details->pspReference ) ) {
145
			$payment->set_transaction_id( $payment_details->pspReference );
146
		}
147
	}
148
149
	/**
150
	 * Is payment method required to start transaction?
151
	 *
152
	 * @see Core_Gateway::payment_method_is_required()
153
	 */
154
	public function payment_method_is_required() {
155
		return true;
156
	}
157
158
	/**
159
	 * Get supported payment methods
160
	 *
161
	 * @see Core_Gateway::get_supported_payment_methods()
162
	 */
163
	public function get_supported_payment_methods() {
164
		return array(
165
			PaymentMethods::IDEAL,
166
			PaymentMethods::SOFORT,
167
		);
168
	}
169
170
	/**
171
	 * Get issuers.
172
	 *
173
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
174
	 */
175
	public function get_issuers() {
176
		$groups = array();
177
178
		$payment_method = PaymentMethodType::transform( PaymentMethods::IDEAL );
179
180
		$result = $this->client->get_issuers( $payment_method );
181
182
		if ( ! $result ) {
0 ignored issues
show
introduced by
The condition $result is always false.
Loading history...
183
			$this->error = $this->client->get_error();
184
185
			return $groups;
186
		}
187
188
		$groups[] = array(
189
			'options' => $result,
190
		);
191
192
		return $groups;
193
	}
194
}
195