Test Failed
Push — develop ( 138c44...8262d9 )
by Remco
11:13
created

Gateway::start()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 25
ccs 0
cts 17
cp 0
crap 20
rs 9.7998
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\Core\Statuses;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Pronamic\WordPress\Pay\G...\EMS\ECommerce\Statuses. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Pronamic\WordPress\Pay\Payments\Payment;
9
10
/**
11
 * Title: EMS e-Commerce
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 EMS e-Commerce 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_HTML_FORM );
37
38
		// Client.
39
		$this->client = new Client();
40
41
		$action_url = Client::ACTION_URL_PRODUCTION;
42
43
		if ( self::MODE_TEST === $config->mode ) {
44
			$action_url = Client::ACTION_URL_TEST;
45
		}
46
47
		$this->client->set_action_url( $action_url );
48
		$this->client->set_storename( $config->storename );
49
		$this->client->set_secret( $config->secret );
50
	}
51
52
	/**
53
	 * Get supported payment methods.
54
	 *
55
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
56
	 */
57
	public function get_supported_payment_methods() {
58
		return array(
59
			Core_PaymentMethods::BANCONTACT,
60
			Core_PaymentMethods::IDEAL,
61
			Core_PaymentMethods::PAYPAL,
62
			Core_PaymentMethods::SOFORT,
63
		);
64
	}
65
66
	/**
67
	 * Start
68
	 *
69
	 * @see Pronamic_WP_Pay_Gateway::start()
70
	 *
71
	 * @param Payment $payment Payment.
72
	 */
73
	public function start( Payment $payment ) {
74
		$payment->set_action_url( $this->client->get_action_url() );
75
76
		$this->client->set_payment_id( $payment->get_id() );
77
		$this->client->set_currency_numeric_code( $payment->get_total_amount()->get_currency()->get_numeric_code() );
78
		$this->client->set_order_id( $payment->format_string( $this->config->order_id ) );
79
		$this->client->set_return_url( home_url( '/' ) );
80
		$this->client->set_notification_url( home_url( '/' ) );
81
		$this->client->set_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

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