Test Failed
Push — develop ( 88a604...f54e3f )
by Reüel
02:40
created

Gateway::get_supported_payment_methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
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
		// Client.
39
		$this->client = new Client();
40
41
		$this->client->set_access_token( $config->access_token );
42
		$this->client->set_merchant_profile( $config->merchant_profile );
43
44
		// Feature supports.
45
		$this->supports = array(
46
			'payment_status_request',
47
		);
48
	}
49
50
	/**
51
	 * Get supported payment methods.
52
	 *
53
	 * @see Core_Gateway::get_supported_payment_methods()
54
	 */
55
	public function get_supported_payment_methods() {
56
		return array(
57
			PaymentMethods::GULDEN,
58
		);
59
	}
60
61
	/**
62
	 * Start.
63
	 *
64
	 * @see Core_Gateway::start()
65
	 *
66
	 * @param Payment $payment The payment.
67
	 */
68
	public function start( Payment $payment ) {
69
		$payment_method = $payment->get_method();
70
		$currency       = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
71
		$amount         = $payment->get_total_amount()->get_value();
72
73
		if ( empty( $payment_method ) ) {
74
			$payment_method = PaymentMethods::GULDEN;
75
		}
76
77
		if ( PaymentMethods::GULDEN === $payment_method ) {
78
			switch ( $currency ) {
79
				case 'EUR':
80
					// Convert to EUR.
81
					$quote = $this->client->get_transaction_quote( 'EUR', 'NLG', $amount, Methods::IDEAL );
82
83
					if ( $quote ) {
84
						$amount   = $quote->data->target_amount->amount;
85
						$currency = 'NLG';
86
					}
87
88
					break;
89
			}
90
		}
91
92
		$transaction = new Transaction();
93
94
		$transaction->payment_id       = $payment->get_id();
95
		$transaction->merchant_profile = $this->config->merchant_profile;
96
		$transaction->description      = $payment->get_description();
97
		$transaction->currency         = $currency;
98
		$transaction->amount           = $amount;
99
		$transaction->payment_method   = Methods::transform( $payment->get_method() );
100
		$transaction->redirect_url     = $payment->get_return_url();
101
		$transaction->callback_url     = add_query_arg( 'nocks_webhook', '', home_url( '/' ) );
0 ignored issues
show
Bug introduced by
The function home_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

101
		$transaction->callback_url     = add_query_arg( 'nocks_webhook', '', /** @scrutinizer ignore-call */ home_url( '/' ) );
Loading history...
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

101
		$transaction->callback_url     = /** @scrutinizer ignore-call */ add_query_arg( 'nocks_webhook', '', home_url( '/' ) );
Loading history...
102
		$transaction->description      = $payment->get_description();
103
104
		if ( null !== $payment->get_customer() ) {
105
			$transaction->locale = $payment->get_customer()->get_locale();
106
		}
107
108
		// Issuer.
109
		if ( Methods::IDEAL === $transaction->payment_method ) {
110
			$transaction->issuer = $payment->get_issuer();
111
		}
112
113
		// Start transaction.
114
		$result = $this->client->start_transaction( $transaction );
115
116
		// Handle errors.
117
		$error = $this->client->get_error();
118
119
		if ( is_wp_error( $error ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

119
		if ( /** @scrutinizer ignore-call */ is_wp_error( $error ) ) {
Loading history...
120
			$this->error = $error;
121
122
			return;
123
		}
124
125
		// Update payment.
126
		if ( isset( $result->data->payments->data[0]->uuid ) ) {
127
			$payment->set_transaction_id( $result->data->uuid );
128
		}
129
130
		if ( isset( $result->data->payments->data[0]->metadata->url ) ) {
131
			$payment->set_action_url( $result->data->payments->data[0]->metadata->url );
132
		}
133
	}
134
135
	/**
136
	 * Update status of the specified payment.
137
	 *
138
	 * @param Payment $payment The payment.
139
	 */
140
	public function update_status( Payment $payment ) {
141
		$transaction_id = $payment->get_transaction_id();
142
143
		$nocks_payment = $this->client->get_transaction( $transaction_id );
144
145
		if ( ! $nocks_payment ) {
146
			$payment->set_status( Core_Statuses::FAILURE );
147
148
			$this->error = $this->client->get_error();
149
150
			return;
151
		}
152
153
		if ( is_object( $nocks_payment ) && isset( $nocks_payment->data->status ) ) {
154
			$status = Statuses::transform( $nocks_payment->data->status );
155
156
			$payment->set_status( $status );
157
		}
158
	}
159
}
160