Issues (8)

src/Gateway.php (1 issue)

Labels
Severity
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-2022 Pronamic
14
 * Company: Pronamic
15
 *
16
 * @author Reüel van der Steege
17
 * @version 3.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
	 * @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() );
98
		$this->client->set_issuer_id( $payment->get_meta( 'issuer' ) );
0 ignored issues
show
It seems like $payment->get_meta('issuer') can also be of type false; however, parameter $issuer_id of Pronamic\WordPress\Pay\G...Client::set_issuer_id() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

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