wp-pay-gateways /
sisow
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Gateway |
||
| 4 | * |
||
| 5 | * @author Pronamic <[email protected]> |
||
| 6 | * @copyright 2005-2018 Pronamic |
||
| 7 | * @license GPL-3.0-or-later |
||
| 8 | * @package Pronamic\WordPress\Pay\Payments |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Pronamic\WordPress\Pay\Gateways\Sisow; |
||
| 12 | |||
| 13 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
| 14 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
| 15 | use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses; |
||
| 16 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
| 17 | use Pronamic\WordPress\Pay\Payments\PaymentLineType; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Title: Sisow gateway |
||
| 21 | * Description: |
||
| 22 | * Copyright: Copyright (c) 2005 - 2018 |
||
| 23 | * Company: Pronamic |
||
| 24 | * |
||
| 25 | * @author Remco Tolsma |
||
| 26 | * @version 2.0.0 |
||
| 27 | * @since 1.0.0 |
||
| 28 | */ |
||
| 29 | class Gateway extends Core_Gateway { |
||
| 30 | /** |
||
| 31 | * Client. |
||
| 32 | * |
||
| 33 | * @var Client |
||
| 34 | */ |
||
| 35 | protected $client; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Constructs and initialize an Sisow gateway |
||
| 39 | * |
||
| 40 | * @param Config $config Config. |
||
| 41 | */ |
||
| 42 | public function __construct( Config $config ) { |
||
| 43 | parent::__construct( $config ); |
||
| 44 | |||
| 45 | $this->supports = array( |
||
| 46 | 'payment_status_request', |
||
| 47 | 'reservation_payments', |
||
| 48 | ); |
||
| 49 | |||
| 50 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||
| 51 | |||
| 52 | $this->client = new Client( $config->merchant_id, $config->merchant_key ); |
||
| 53 | $this->client->set_test_mode( self::MODE_TEST === $config->mode ); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get issuers |
||
| 58 | * |
||
| 59 | * @see Core_Gateway::get_issuers() |
||
| 60 | */ |
||
| 61 | public function get_issuers() { |
||
| 62 | $groups = array(); |
||
| 63 | |||
| 64 | $result = $this->client->get_directory(); |
||
| 65 | |||
| 66 | if ( $result ) { |
||
| 67 | $groups[] = array( |
||
| 68 | 'options' => $result, |
||
| 69 | ); |
||
| 70 | } else { |
||
| 71 | $this->error = $this->client->get_error(); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $groups; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get supported payment methods |
||
| 79 | * |
||
| 80 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||
| 81 | */ |
||
| 82 | public function get_supported_payment_methods() { |
||
| 83 | return array( |
||
| 84 | PaymentMethods::AFTERPAY, |
||
| 85 | PaymentMethods::BANK_TRANSFER, |
||
| 86 | PaymentMethods::BANCONTACT, |
||
| 87 | PaymentMethods::BELFIUS, |
||
| 88 | PaymentMethods::BUNQ, |
||
| 89 | PaymentMethods::IN3, |
||
| 90 | PaymentMethods::CREDIT_CARD, |
||
| 91 | PaymentMethods::FOCUM, |
||
| 92 | PaymentMethods::GIROPAY, |
||
| 93 | PaymentMethods::IDEAL, |
||
| 94 | PaymentMethods::IDEALQR, |
||
| 95 | PaymentMethods::KLARNA_PAY_LATER, |
||
| 96 | PaymentMethods::PAYPAL, |
||
| 97 | PaymentMethods::SOFORT, |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Is payment method required to start transaction? |
||
| 103 | * |
||
| 104 | * @see Core_Gateway::payment_method_is_required() |
||
| 105 | */ |
||
| 106 | public function payment_method_is_required() { |
||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Start |
||
| 112 | * |
||
| 113 | * @see Core_Gateway::start() |
||
| 114 | * |
||
| 115 | * @param Payment $payment Payment. |
||
| 116 | */ |
||
| 117 | public function start( Payment $payment ) { |
||
| 118 | // Order and purchase ID. |
||
| 119 | $order_id = $payment->get_order_id(); |
||
| 120 | $purchase_id = strval( empty( $order_id ) ? $payment->get_id() : $order_id ); |
||
| 121 | |||
| 122 | // Maximum length for purchase ID is 16 characters, otherwise an error will occur: |
||
| 123 | // ideal_sisow_error - purchaseid too long (16). |
||
| 124 | $purchase_id = substr( $purchase_id, 0, 16 ); |
||
| 125 | |||
| 126 | // New transaction request. |
||
| 127 | $request = new TransactionRequest( |
||
| 128 | $this->config->merchant_id, |
||
| 129 | $this->config->shop_id |
||
| 130 | ); |
||
| 131 | |||
| 132 | $request->merge_parameters( |
||
| 133 | array( |
||
| 134 | 'payment' => Methods::transform( $payment->get_method(), $payment->get_method() ), |
||
| 135 | 'purchaseid' => substr( $purchase_id, 0, 16 ), |
||
| 136 | 'entrancecode' => $payment->get_entrance_code(), |
||
| 137 | 'amount' => $payment->get_total_amount()->get_cents(), |
||
| 138 | 'description' => substr( $payment->get_description(), 0, 32 ), |
||
| 139 | 'testmode' => ( self::MODE_TEST === $this->config->mode ) ? 'true' : 'false', |
||
| 140 | 'returnurl' => $payment->get_return_url(), |
||
| 141 | 'cancelurl' => $payment->get_return_url(), |
||
| 142 | 'notifyurl' => $payment->get_return_url(), |
||
| 143 | 'callbackurl' => $payment->get_return_url(), |
||
| 144 | // Other parameters. |
||
| 145 | 'issuerid' => $payment->get_issuer(), |
||
| 146 | 'billing_mail' => $payment->get_email(), |
||
| 147 | ) |
||
| 148 | ); |
||
| 149 | |||
| 150 | // Payment method. |
||
| 151 | $this->set_payment_method( null === $payment->get_method() ? PaymentMethods::IDEAL : $payment->get_method() ); |
||
| 152 | |||
| 153 | // Additional parameters for payment method. |
||
| 154 | if ( PaymentMethods::IDEALQR === $payment->get_method() ) { |
||
| 155 | $request->set_parameter( 'qrcode', 'true' ); |
||
| 156 | } |
||
| 157 | |||
| 158 | // Customer. |
||
| 159 | if ( null !== $payment->get_customer() ) { |
||
| 160 | $customer = $payment->get_customer(); |
||
| 161 | |||
| 162 | $request->merge_parameters( |
||
| 163 | array( |
||
| 164 | 'ipaddress' => $customer->get_ip_address(), |
||
| 165 | 'gender' => $customer->get_gender(), |
||
| 166 | ) |
||
| 167 | ); |
||
| 168 | |||
| 169 | if ( null !== $customer->get_locale() ) { |
||
| 170 | /* |
||
| 171 | * @link https://github.com/wp-pay-gateways/sisow/tree/feature/post-pay/documentation#parameter-locale |
||
| 172 | */ |
||
| 173 | $sisow_locale = strtoupper( substr( $customer->get_locale(), -2 ) ); |
||
| 174 | |||
| 175 | $request->set_parameter( 'locale', $sisow_locale ); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ( null !== $customer->get_birth_date() ) { |
||
| 179 | $request->set_parameter( 'birthdate', $customer->get_birth_date()->format( 'dmY' ) ); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | // Billing address. |
||
| 184 | if ( null !== $payment->get_billing_address() ) { |
||
| 185 | $address = $payment->get_billing_address(); |
||
| 186 | |||
| 187 | if ( null !== $address->get_name() ) { |
||
| 188 | $name = $address->get_name(); |
||
| 189 | |||
| 190 | $request->merge_parameters( |
||
| 191 | array( |
||
| 192 | 'billing_firstname' => $name->get_first_name(), |
||
| 193 | 'billing_lastname' => $name->get_last_name(), |
||
| 194 | ) |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | |||
| 198 | $request->merge_parameters( |
||
| 199 | array( |
||
| 200 | 'billing_mail' => $address->get_email(), |
||
| 201 | 'billing_company' => $address->get_company_name(), |
||
| 202 | 'billing_coc' => $address->get_kvk_number(), |
||
| 203 | 'billing_address1' => $address->get_line_1(), |
||
| 204 | 'billing_address2' => $address->get_line_2(), |
||
| 205 | 'billing_zip' => $address->get_postal_code(), |
||
| 206 | 'billing_city' => $address->get_city(), |
||
| 207 | 'billing_country' => $address->get_country_name(), |
||
| 208 | 'billing_countrycode' => $address->get_country_code(), |
||
| 209 | 'billing_phone' => $address->get_phone(), |
||
| 210 | ) |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | // Shipping address. |
||
| 215 | if ( null !== $payment->get_shipping_address() ) { |
||
| 216 | $address = $payment->get_shipping_address(); |
||
| 217 | |||
| 218 | if ( null !== $address->get_name() ) { |
||
| 219 | $name = $address->get_name(); |
||
| 220 | |||
| 221 | $request->merge_parameters( |
||
| 222 | array( |
||
| 223 | 'shipping_firstname' => $name->get_first_name(), |
||
| 224 | 'shipping_lastname' => $name->get_last_name(), |
||
| 225 | ) |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | |||
| 229 | $request->merge_parameters( |
||
| 230 | array( |
||
| 231 | 'shipping_mail' => $address->get_email(), |
||
| 232 | 'shipping_company' => $address->get_company_name(), |
||
| 233 | 'shipping_address1' => $address->get_line_1(), |
||
| 234 | 'shipping_address2' => $address->get_line_2(), |
||
| 235 | 'shipping_zip' => $address->get_postal_code(), |
||
| 236 | 'shipping_city' => $address->get_city(), |
||
| 237 | 'shipping_country' => $address->get_country_name(), |
||
| 238 | 'shipping_countrycode' => $address->get_country_code(), |
||
| 239 | 'shipping_phone' => $address->get_phone(), |
||
| 240 | ) |
||
| 241 | ); |
||
| 242 | } |
||
| 243 | |||
| 244 | // Lines. |
||
| 245 | if ( null !== $payment->get_lines() ) { |
||
| 246 | $lines = $payment->get_lines(); |
||
| 247 | |||
| 248 | $x = 1; |
||
| 249 | |||
| 250 | foreach ( $lines as $line ) { |
||
| 251 | $net_price = ( null === $line->get_unit_price_excluding_tax() ) ? null : $line->get_unit_price_excluding_tax()->get_cents(); |
||
| 252 | $total = ( null === $line->get_total_amount() ) ? null : $line->get_total_amount()->get_cents(); |
||
| 253 | $net_total = ( null === $line->get_total_amount_excluding_tax() ) ? null : $line->get_total_amount_excluding_tax()->get_cents(); |
||
| 254 | $tax = ( null === $line->get_tax_amount() ) ? null : $line->get_tax_amount()->get_cents(); |
||
| 255 | |||
| 256 | $product_id = $line->get_id(); |
||
| 257 | |||
| 258 | if ( PaymentLineType::SHIPPING === $line->get_type() ) { |
||
| 259 | $product_id = 'shipping'; |
||
| 260 | } elseif ( PaymentLineType::FEE === $line->get_type() ) { |
||
| 261 | $product_id = 'paymentfee'; |
||
| 262 | } |
||
| 263 | |||
| 264 | $request->merge_parameters( |
||
| 265 | array( |
||
| 266 | 'product_id_' . $x => $product_id, |
||
| 267 | 'product_description_' . $x => $line->get_name(), |
||
| 268 | 'product_quantity_' . $x => $line->get_quantity(), |
||
| 269 | 'product_netprice_' . $x => $net_price, |
||
| 270 | 'product_total_' . $x => $total, |
||
| 271 | 'product_nettotal_' . $x => $net_total, |
||
| 272 | 'product_tax_' . $x => $tax, |
||
| 273 | 'product_taxrate_' . $x => $line->get_tax_percentage() * 100, |
||
| 274 | ) |
||
| 275 | ); |
||
| 276 | |||
| 277 | $x++; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | // Create transaction. |
||
| 282 | $result = $this->client->create_transaction( $request ); |
||
| 283 | |||
| 284 | if ( false !== $result ) { |
||
| 285 | $payment->set_transaction_id( $result->id ); |
||
| 286 | $payment->set_action_url( $result->issuer_url ); |
||
| 287 | } else { |
||
| 288 | $this->error = $this->client->get_error(); |
||
| 289 | |||
| 290 | return false; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Update status of the specified payment |
||
| 296 | * |
||
| 297 | * @param Payment $payment Payment. |
||
| 298 | */ |
||
| 299 | public function update_status( Payment $payment ) { |
||
| 300 | $request = new StatusRequest( |
||
| 301 | $payment->get_transaction_id(), |
||
| 302 | $this->config->merchant_id, |
||
| 303 | $this->config->shop_id |
||
| 304 | ); |
||
| 305 | |||
| 306 | $result = $this->client->get_status( $request ); |
||
| 307 | |||
| 308 | if ( false === $result ) { |
||
| 309 | $this->error = $this->client->get_error(); |
||
| 310 | |||
| 311 | return; |
||
| 312 | } |
||
| 313 | |||
| 314 | $transaction = $result; |
||
| 315 | |||
| 316 | $payment->set_status( Statuses::transform( $transaction->status ) ); |
||
| 317 | $payment->set_consumer_name( $transaction->consumer_name ); |
||
| 318 | $payment->set_consumer_account_number( $transaction->consumer_account ); |
||
| 319 | $payment->set_consumer_city( $transaction->consumer_city ); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Create invoice. |
||
| 324 | * |
||
| 325 | * @param Payment $payment Payment. |
||
| 326 | * |
||
| 327 | * @return bool|Transaction |
||
| 328 | */ |
||
| 329 | public function create_invoice( $payment ) { |
||
| 330 | $transaction_id = $payment->get_transaction_id(); |
||
| 331 | |||
| 332 | if ( empty( $transaction_id ) ) { |
||
| 333 | return false; |
||
| 334 | } |
||
| 335 | |||
| 336 | // Invoice request. |
||
| 337 | $request = new InvoiceRequest( |
||
| 338 | $this->config->merchant_id, |
||
| 339 | $this->config->shop_id |
||
| 340 | ); |
||
| 341 | |||
| 342 | $request->set_parameter( 'trxid', $transaction_id ); |
||
| 343 | |||
| 344 | // Create invoice. |
||
| 345 | $result = $this->client->create_invoice( $request ); |
||
| 346 | |||
| 347 | // Handle errors. |
||
| 348 | if ( false === $result ) { |
||
| 349 | $this->error = $this->client->get_error(); |
||
| 350 | |||
| 351 | return false; |
||
| 352 | } |
||
| 353 | |||
| 354 | $payment->set_status( Core_Statuses::SUCCESS ); |
||
| 355 | |||
| 356 | $payment->save(); |
||
| 357 | |||
| 358 | return $result; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Cancel reservation. |
||
| 363 | * |
||
| 364 | * @param Payment $payment Payment. |
||
| 365 | * |
||
| 366 | * @return bool|Reservation |
||
| 367 | */ |
||
| 368 | public function cancel_reservation( $payment ) { |
||
| 369 | $transaction_id = $payment->get_transaction_id(); |
||
| 370 | |||
| 371 | if ( empty( $transaction_id ) ) { |
||
| 372 | return false; |
||
| 373 | } |
||
| 374 | |||
| 375 | // Cancel reservation request. |
||
| 376 | $request = new CancelReservationRequest( |
||
| 377 | $this->config->merchant_id, |
||
| 378 | $this->config->shop_id |
||
| 379 | ); |
||
| 380 | |||
| 381 | $request->set_parameter( 'trxid', $transaction_id ); |
||
| 382 | |||
| 383 | // Cancel reservation. |
||
| 384 | $result = $this->client->cancel_reservation( $request ); |
||
| 385 | |||
| 386 | // Handle errors. |
||
| 387 | if ( false === $result ) { |
||
| 388 | $this->error = $this->client->get_error(); |
||
| 389 | |||
| 390 | return false; |
||
| 391 | } |
||
| 392 | |||
| 393 | if ( isset( $result->status ) ) { |
||
| 394 | $payment->set_status( Statuses::transform( $result->status ) ); |
||
| 395 | |||
| 396 | $payment->save(); |
||
| 397 | } |
||
| 398 | |||
| 399 | return $result; |
||
| 400 | } |
||
| 401 | } |
||
| 402 |