Issues (9)

src/Gateway.php (1 issue)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods;
7
use Pronamic\WordPress\Pay\Payments\Payment;
8
9
/**
10
 * Title: OmniKassa
11
 * Description:
12
 * Copyright: 2005-2020 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 2.0.3
17
 * @since   1.0.0
18
 */
19
class Gateway extends Core_Gateway {
20
	/**
21
	 * Client.
22
	 *
23
	 * @var Client
24
	 */
25
	protected $client;
26
27
	/**
28
	 * Constructs and initializes an OmniKassa gateway
29
	 *
30
	 * @param Config $config Config.
31
	 */
32
	public function __construct( Config $config ) {
33
		parent::__construct( $config );
34
35
		$this->set_method( self::METHOD_HTML_FORM );
36
37
		// Client.
38
		$this->client = new Client();
39
40
		$action_url = Client::ACTION_URL_PRUDCTION;
41
42
		if ( self::MODE_TEST === $config->mode ) {
43
			$action_url = Client::ACTION_URL_TEST;
44
		}
45
46
		$this->client->set_action_url( $action_url );
47
		$this->client->set_merchant_id( $config->merchant_id );
48
		$this->client->set_key_version( $config->key_version );
49
		$this->client->set_secret_key( $config->secret_key );
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
			PaymentMethods::IDEAL,
60
			PaymentMethods::CREDIT_CARD,
61
			PaymentMethods::DIRECT_DEBIT,
62
			PaymentMethods::BANCONTACT,
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
		$transaction_reference = $payment->get_meta( 'omnikassa_transaction_reference' );
75
76
		if ( empty( $transaction_reference ) ) {
77
			$transaction_reference = md5( uniqid( '', true ) );
78
79
			$payment->set_meta( 'omnikassa_transaction_reference', $transaction_reference );
80
		}
81
82
		$payment->set_transaction_id( $transaction_reference );
83
		$payment->set_action_url( $this->client->get_action_url() );
84
	}
85
86
	/**
87
	 * Get the output HTML
88
	 *
89
	 * @param Payment $payment Payment.
90
	 *
91
	 * @return array
92
	 * @see      Core_Gateway::get_output_html()
93
	 * @since    1.1.2
94
	 * @version  2.0.3
95
	 */
96
	public function get_output_fields( Payment $payment ) {
97
		$language = null;
98
99
		if ( null !== $payment->get_customer() ) {
100
			$language = $payment->get_customer()->get_language();
101
		}
102
103
		$this->client->set_customer_language( LocaleHelper::transform( $language ) );
104
		$this->client->set_currency_numeric_code( $payment->get_total_amount()->get_currency()->get_numeric_code() );
105
		$this->client->set_order_id( $payment->format_string( $this->config->order_id ) );
106
		$this->client->set_normal_return_url( home_url( '/' ) );
107
		$this->client->set_automatic_response_url( home_url( '/' ) );
108
		$this->client->set_amount( $payment->get_total_amount()->get_minor_units() );
109
		$this->client->set_transaction_reference( $payment->get_transaction_id() );
110
111
		switch ( $payment->get_method() ) {
112
			/*
113
			 * If this field is not supplied in the payment request, then
114
			 * by default the customer will be redirected to the Rabo
115
			 * OmniKassa payment page. On the payment page, the
116
			 * customer can choose from the payment methods
117
			 * offered by the Rabo OmniKassa. These are the payment
118
			 * methods: IDEAL, VISA, MASTERCARD,
119
			 * MAESTRO, V PAY and BCMC.
120
			 *
121
			 * Exception: the register services INCASSO (direct debit),
122
			 * ACCEPTGIRO (giro collection form) and REMBOURS
123
			 * (cash on delivery) are not displayed on the Rabo
124
			 * OmniKassa payment page by default.
125
			 * If you wish to offer these register services to the
126
			 * customer on the payment page, then you need to
127
			 * always populate the paymentMeanBrandList field with
128
			 * all the payment methods you wish to offer (provided
129
			 * these have been requested and activated): IDEAL,
130
			 * VISA, MASTERCARD, MAESTRO, VPAY, BCMC,
131
			 * INCASSO, ACCEPTGIRO, REMBOURS.
132
			 *
133
			 * If you let the customer choose the payment method
134
			 * while still in your webshop, then you must populate
135
			 * this field of the payment request with only the selected
136
			 * payment method. Populating this field with only one
137
			 * payment method will instruct the Rabo OmniKassa to
138
			 * redirect the customer directly to the payment page for
139
			 * this payment method.
140
			 */
141
			case PaymentMethods::BANCONTACT:
142
			case PaymentMethods::MISTER_CASH:
0 ignored issues
show
Deprecated Code introduced by
The constant Pronamic\WordPress\Pay\C...entMethods::MISTER_CASH has been deprecated: "Bancontact/Mister Cash" was renamed to just "Bancontact". ( Ignorable by Annotation )

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

142
			case /** @scrutinizer ignore-deprecated */ PaymentMethods::MISTER_CASH:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
143
				$this->client->add_payment_mean_brand( Methods::BCMC );
144
145
				break;
146
			case PaymentMethods::CREDIT_CARD:
147
				$this->client->add_payment_mean_brand( Methods::MAESTRO );
148
				$this->client->add_payment_mean_brand( Methods::MASTERCARD );
149
				$this->client->add_payment_mean_brand( Methods::VISA );
150
				$this->client->add_payment_mean_brand( Methods::VPAY );
151
152
				break;
153
			case PaymentMethods::DIRECT_DEBIT:
154
				$this->client->add_payment_mean_brand( Methods::INCASSO );
155
156
				break;
157
			case PaymentMethods::MAESTRO:
158
				$this->client->add_payment_mean_brand( Methods::MAESTRO );
159
160
				break;
161
			case PaymentMethods::IDEAL:
162
				$this->client->add_payment_mean_brand( Methods::IDEAL );
163
164
				break;
165
			default:
166
				$this->client->add_payment_mean_brand( Methods::IDEAL );
167
				$this->client->add_payment_mean_brand( Methods::VISA );
168
				$this->client->add_payment_mean_brand( Methods::MASTERCARD );
169
				$this->client->add_payment_mean_brand( Methods::MAESTRO );
170
				$this->client->add_payment_mean_brand( Methods::VPAY );
171
				$this->client->add_payment_mean_brand( Methods::BCMC );
172
				$this->client->add_payment_mean_brand( Methods::INCASSO );
173
				$this->client->add_payment_mean_brand( Methods::ACCEPTGIRO );
174
				$this->client->add_payment_mean_brand( Methods::REMBOURS );
175
176
				break;
177
		}
178
179
		return $this->client->get_fields();
180
	}
181
182
	/**
183
	 * Update status of the specified payment
184
	 *
185
	 * @param Payment $payment Payment.
186
	 */
187
	public function update_status( Payment $payment ) {
188
		$input_data = filter_input( INPUT_POST, 'Data' );
189
		$input_seal = filter_input( INPUT_POST, 'Seal', FILTER_SANITIZE_STRING );
190
191
		$data = Client::parse_piped_string( $input_data );
192
193
		$seal = Client::compute_seal( $input_data, $this->config->secret_key );
194
195
		// Check if the posted seal is equal to our seal.
196
		if ( 0 === strcasecmp( $input_seal, $seal ) ) {
197
			$response_code = $data['responseCode'];
198
199
			$status = ResponseCodes::transform( $response_code );
200
201
			// Set the status of the payment.
202
			$payment->set_status( $status );
203
204
			$labels = array(
205
				'amount'               => __( 'Amount', 'pronamic_ideal' ),
206
				'captureDay'           => _x( 'Capture Day', 'creditcard', 'pronamic_ideal' ),
207
				'captureMode'          => _x( 'Capture Mode', 'creditcard', 'pronamic_ideal' ),
208
				'currencyCode'         => __( 'Currency Code', 'pronamic_ideal' ),
209
				'merchantId'           => __( 'Merchant ID', 'pronamic_ideal' ),
210
				'orderId'              => __( 'Order ID', 'pronamic_ideal' ),
211
				'transactionDateTime'  => __( 'Transaction Date Time', 'pronamic_ideal' ),
212
				'transactionReference' => __( 'Transaction Reference', 'pronamic_ideal' ),
213
				'keyVersion'           => __( 'Key Version', 'pronamic_ideal' ),
214
				'authorisationId'      => __( 'Authorisation ID', 'pronamic_ideal' ),
215
				'paymentMeanBrand'     => __( 'Payment Mean Brand', 'pronamic_ideal' ),
216
				'paymentMeanType'      => __( 'Payment Mean Type', 'pronamic_ideal' ),
217
				'responseCode'         => __( 'Response Code', 'pronamic_ideal' ),
218
			);
219
220
			$note = '';
221
222
			$note .= '<p>';
223
			$note .= __( 'OmniKassa transaction data in response message:', 'pronamic_ideal' );
224
			$note .= '</p>';
225
226
			$note .= '<dl>';
227
228
			foreach ( $labels as $key => $label ) {
229
				if ( isset( $data[ $key ] ) ) {
230
					$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
231
					$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
232
				}
233
			}
234
235
			$note .= '</dl>';
236
237
			$payment->add_note( $note );
238
		}
239
	}
240
}
241