Failed Conditions
Push — master ( 3c3894...5bb23b )
by Remco
11:03 queued 04:47
created

Gateway::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
ccs 0
cts 11
cp 0
crap 6
rs 9.9666
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\EMS\ECommerce;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods as Core_PaymentMethods;
7
use Pronamic\WordPress\Pay\Payments\PaymentStatus;
8
use Pronamic\WordPress\Pay\Payments\Payment;
9
10
/**
11
 * Title: EMS e-Commerce
12
 * Description:
13
 * Copyright: 2005-2021 Pronamic
14
 * Company: Pronamic
15
 *
16
 * @author Reüel van der Steege
17
 * @version 2.0.4
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 EMS e-Commerce gateway
30
	 *
31
	 * @param Config $config Config.
32
	 * @return void
33
	 */
34
	public function __construct( Config $config ) {
35
		parent::__construct( $config );
36
37
		$this->set_method( self::METHOD_HTML_FORM );
38
39
		// Client.
40
		$this->client = new Client();
41
42
		$action_url = Client::ACTION_URL_PRODUCTION;
43
44
		if ( self::MODE_TEST === $config->mode ) {
45
			$action_url = Client::ACTION_URL_TEST;
46
		}
47
48
		$this->client->set_action_url( $action_url );
49
		$this->client->set_storename( $config->storename );
50
		$this->client->set_secret( $config->secret );
51
	}
52
53
	/**
54
	 * Get supported payment methods.
55
	 *
56
	 * @return array<string>
57
	 *
58
	 * @see Core_Gateway::get_supported_payment_methods()
59
	 */
60
	public function get_supported_payment_methods() {
61
		return array(
62
			Core_PaymentMethods::BANCONTACT,
63
			Core_PaymentMethods::IDEAL,
64
			Core_PaymentMethods::PAYPAL,
65
			Core_PaymentMethods::SOFORT,
66
		);
67
	}
68
69
	/**
70
	 * Start
71
	 *
72
	 * @param Payment $payment Payment.
73
	 * @return void
74
	 *
75
	 * @see Core_Gateway::start()
76
	 */
77
	public function start( Payment $payment ) {
78
		$payment->set_action_url( $this->client->get_action_url() );
79
	}
80
81
	/**
82
	 * Get the output HTML
83
	 *
84
	 * @param Payment $payment Payment.
85
	 * @return array
86
	 *
87
	 * @see     Core_Gateway::get_output_html()
88
	 * @since   1.0.0
89
	 * @version 2.0.4
90
	 */
91
	public function get_output_fields( Payment $payment ) {
92
		$this->client->set_payment_id( (int) $payment->get_id() );
93
		$this->client->set_currency_numeric_code( (string) $payment->get_total_amount()->get_currency()->get_numeric_code() );
94
		$this->client->set_order_id( $payment->format_string( $this->config->order_id ) );
95
		$this->client->set_return_url( home_url( '/' ) );
96
		$this->client->set_notification_url( home_url( '/' ) );
97
		$this->client->set_amount( $payment->get_total_amount()->get_minor_units() );
0 ignored issues
show
Bug introduced by
$payment->get_total_amount()->get_minor_units() of type Pronamic\WordPress\Number\Number is incompatible with the type double expected by parameter $amount of Pronamic\WordPress\Pay\G...ce\Client::set_amount(). ( Ignorable by Annotation )

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

97
		$this->client->set_amount( /** @scrutinizer ignore-type */ $payment->get_total_amount()->get_minor_units() );
Loading history...
98
		$this->client->set_issuer_id( $payment->get_issuer() );
99
100
		// Language.
101
		if ( null !== $payment->get_customer() ) {
102
			$this->client->set_language( $payment->get_customer()->get_locale() );
103
		}
104
105
		// Payment method.
106
		$payment_method = PaymentMethods::transform( $payment->get_method() );
107
108
		if ( null === $payment_method && '' !== $payment->get_method() ) {
109
			// Leap of faith if the WordPress payment method could not transform to a EMS method?
110
			$payment_method = $payment->get_method();
111
		}
112
113
		$this->client->set_payment_method( $payment_method );
114
115
		return $this->client->get_fields();
116
	}
117
118
	/**
119
	 * Update status of the specified payment
120
	 *
121
	 * @param Payment $payment Payment.
122
	 * @return void
123
	 */
124
	public function update_status( Payment $payment ) {
125
		$approval_code = filter_input( INPUT_POST, 'approval_code', FILTER_SANITIZE_STRING );
126
127
		$input_hash = filter_input( INPUT_POST, 'response_hash' );
128
129
		$hash_values = array(
130
			$this->client->get_secret(),
131
			$approval_code,
132
			filter_input( INPUT_POST, 'chargetotal', FILTER_SANITIZE_STRING ),
133
			filter_input( INPUT_POST, 'currency', FILTER_SANITIZE_STRING ),
134
			filter_input( INPUT_POST, 'txndatetime', FILTER_SANITIZE_STRING ),
135
			$this->client->get_storename(),
136
		);
137
138
		if ( filter_has_var( INPUT_POST, 'notification_hash' ) ) {
139
			$input_hash = filter_input( INPUT_POST, 'notification_hash' );
140
141
			$hash_values = array(
142
				filter_input( INPUT_POST, 'chargetotal', FILTER_SANITIZE_STRING ),
143
				$this->client->get_secret(),
144
				filter_input( INPUT_POST, 'currency', FILTER_SANITIZE_STRING ),
145
				filter_input( INPUT_POST, 'txndatetime', FILTER_SANITIZE_STRING ),
146
				$this->client->get_storename(),
147
				$approval_code,
148
			);
149
		}
150
151
		$hash = Client::compute_hash( $hash_values );
152
153
		// Check if the posted hash is equal to the calculated response or notification hash.
154
		if ( 0 === strcasecmp( $input_hash, $hash ) ) {
155
			$response_code = substr( $approval_code, 0, 1 );
156
157
			switch ( $response_code ) {
158
				case 'Y':
159
					$status = PaymentStatus::SUCCESS;
160
161
					break;
162
				case 'N':
163
					$status = PaymentStatus::FAILURE;
164
165
					$fail_code = filter_input( INPUT_POST, 'fail_rc', FILTER_SANITIZE_NUMBER_INT );
166
167
					if ( '5993' === $fail_code ) {
168
						$status = PaymentStatus::CANCELLED;
169
					}
170
171
					break;
172
173
				default:
174
					$status = PaymentStatus::OPEN;
175
176
					break;
177
			}
178
179
			// Set the status of the payment.
180
			$payment->set_status( $status );
181
182
			$labels = array(
183
				'approval_code'           => __( 'Approval code', 'pronamic_ideal' ),
184
				'oid'                     => __( 'Order ID', 'pronamic_ideal' ),
185
				'refnumber'               => _x( 'Reference number', 'creditcard', 'pronamic_ideal' ),
186
				'status'                  => __( 'Status', 'pronamic_ideal' ),
187
				'txndate_processed'       => __( 'Time of transaction processing', 'pronamic_ideal' ),
188
				'tdate'                   => __( 'Identification for transaction', 'pronamic_ideal' ),
189
				'fail_reason'             => __( 'Fail reason', 'pronamic_ideal' ),
190
				'response_hash'           => __( 'Response hash', 'pronamic_ideal' ),
191
				'processor_response_code' => __( 'Processor response code', 'pronamic_ideal' ),
192
				'fail_rc'                 => __( 'Fail code', 'pronamic_ideal' ),
193
				'terminal_id'             => __( 'Terminal ID', 'pronamic_ideal' ),
194
				'ccbin'                   => __( 'Creditcard issuing bank', 'pronamic_ideal' ),
195
				'cccountry'               => __( 'Creditcard country', 'pronamic_ideal' ),
196
				'ccbrand'                 => __( 'Creditcard brand', 'pronamic_ideal' ),
197
			);
198
199
			$note = '';
200
201
			$note .= '<p>';
202
			$note .= __( 'EMS e-Commerce transaction data in response message:', 'pronamic_ideal' );
203
			$note .= '</p>';
204
205
			$note .= '<dl>';
206
207
			foreach ( $labels as $key => $label ) {
208
				if ( filter_has_var( INPUT_POST, $key ) ) {
209
					$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
210
					$note .= sprintf( '<dd>%s</dd>', esc_html( filter_input( INPUT_POST, $key, FILTER_SANITIZE_STRING ) ) );
211
				}
212
			}
213
214
			$note .= '</dl>';
215
216
			$payment->add_note( $note );
217
		}
218
	}
219
}
220