Failed Conditions
Push — feature/webhook-status ( 7f5a3e )
by Reüel
05:57
created

Gateway::get_supported_features()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Nocks;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods;
7
use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses;
8
use Pronamic\WordPress\Pay\Payments\Payment;
9
10
/**
11
 * Title: Nocks gateway
12
 * Description:
13
 * Copyright: 2005-2019 Pronamic
14
 * Company: Pronamic
15
 *
16
 * @author  Reüel van der Steege
17
 * @version 2.0.1
18
 * @since   1.0.0
19
 */
20
class Gateway extends Core_Gateway {
21
	/**
22
	 * Client.
23
	 *
24
	 * @var Client
25
	 */
26
	protected $client;
27
28
	/**
29
	 * Constructs and initializes an Nocks gateway.
30
	 *
31
	 * @param Config $config Config.
32
	 */
33
	public function __construct( Config $config ) {
34
		parent::__construct( $config );
35
36
		$this->set_method( self::METHOD_HTTP_REDIRECT );
37
38
		// Supported features.
39
		$this->supports = self::get_supported_features();
40
41
		// Client.
42
		$this->client = new Client();
43
44
		$this->client->set_access_token( $config->access_token );
45
		$this->client->set_merchant_profile( $config->merchant_profile );
46
	}
47
48
	/**
49
	 * Get supported features.
50
	 *
51
	 * @return array
52
	 */
53
	public static function get_supported_features() {
54
		return array(
55
			'payment_status_request',
56
		);
57
	}
58
59
	/**
60
	 * Get supported payment methods.
61
	 *
62
	 * @see Core_Gateway::get_supported_payment_methods()
63
	 */
64
	public function get_supported_payment_methods() {
65
		return array(
66
			PaymentMethods::GULDEN,
67
		);
68
	}
69
70
	/**
71
	 * Start.
72
	 *
73
	 * @see Core_Gateway::start()
74
	 *
75
	 * @param Payment $payment The payment.
76
	 */
77
	public function start( Payment $payment ) {
78
		$payment_method = $payment->get_method();
79
		$currency       = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
80
		$amount         = $payment->get_total_amount()->get_value();
81
82
		if ( empty( $payment_method ) ) {
83
			$payment_method = PaymentMethods::GULDEN;
84
		}
85
86
		if ( PaymentMethods::GULDEN === $payment_method ) {
87
			switch ( $currency ) {
88
				case 'EUR':
89
					// Convert to EUR.
90
					$quote = $this->client->get_transaction_quote( 'EUR', 'NLG', $amount, Methods::IDEAL );
91
92
					if ( $quote ) {
93
						$amount   = $quote->data->target_amount->amount;
94
						$currency = 'NLG';
95
					}
96
97
					break;
98
			}
99
		}
100
101
		$transaction = new Transaction();
102
103
		$transaction->payment_id       = $payment->get_id();
104
		$transaction->merchant_profile = $this->config->merchant_profile;
105
		$transaction->description      = $payment->get_description();
106
		$transaction->currency         = $currency;
107
		$transaction->amount           = $amount;
108
		$transaction->payment_method   = Methods::transform( $payment->get_method() );
109
		$transaction->redirect_url     = $payment->get_return_url();
110
		$transaction->callback_url     = add_query_arg( 'nocks_webhook', '', home_url( '/' ) );
111
		$transaction->description      = $payment->get_description();
112
113
		if ( null !== $payment->get_customer() ) {
114
			$transaction->locale = $payment->get_customer()->get_locale();
115
		}
116
117
		// Issuer.
118
		if ( Methods::IDEAL === $transaction->payment_method ) {
119
			$transaction->issuer = $payment->get_issuer();
120
		}
121
122
		// Start transaction.
123
		$result = $this->client->start_transaction( $transaction );
124
125
		// Handle errors.
126
		$error = $this->client->get_error();
127
128
		if ( is_wp_error( $error ) ) {
129
			$this->error = $error;
130
131
			return;
132
		}
133
134
		// Update payment.
135
		if ( isset( $result->data->payments->data[0]->uuid ) ) {
136
			$payment->set_transaction_id( $result->data->uuid );
137
		}
138
139
		if ( isset( $result->data->payments->data[0]->metadata->url ) ) {
140
			$payment->set_action_url( $result->data->payments->data[0]->metadata->url );
141
		}
142
	}
143
144
	/**
145
	 * Update status of the specified payment.
146
	 *
147
	 * @param Payment $payment The payment.
148
	 */
149
	public function update_status( Payment $payment ) {
150
		$transaction_id = $payment->get_transaction_id();
151
152
		$nocks_payment = $this->client->get_transaction( $transaction_id );
153
154
		if ( ! $nocks_payment ) {
155
			$payment->set_status( Core_Statuses::FAILURE );
156
157
			$this->error = $this->client->get_error();
158
159
			return;
160
		}
161
162
		if ( is_object( $nocks_payment ) && isset( $nocks_payment->data->status ) ) {
163
			$status = Statuses::transform( $nocks_payment->data->status );
164
165
			$payment->set_status( $status );
166
		}
167
	}
168
}
169