wp-pay-gateways /
adyen
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Gateway |
||
| 4 | * |
||
| 5 | * @author Pronamic <[email protected]> |
||
| 6 | * @copyright 2005-2019 Pronamic |
||
| 7 | * @license GPL-3.0-or-later |
||
| 8 | * @package Pronamic\WordPress\Pay\Gateways\Adyen |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Pronamic\WordPress\Pay\Gateways\Adyen; |
||
| 12 | |||
| 13 | use Locale; |
||
| 14 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
| 15 | use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses; |
||
| 16 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
| 17 | use Pronamic\WordPress\Pay\Core\Util; |
||
|
0 ignored issues
–
show
|
|||
| 18 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
| 19 | use Pronamic\WordPress\Pay\Plugin; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Gateway |
||
| 23 | * |
||
| 24 | * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php |
||
| 25 | * |
||
| 26 | * @author Remco Tolsma |
||
| 27 | * @version 1.0.0 |
||
| 28 | * @since 1.0.0 |
||
| 29 | */ |
||
| 30 | class Gateway extends Core_Gateway { |
||
| 31 | /** |
||
| 32 | * Slug of this gateway. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | const SLUG = 'adyen'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Web SDK version. |
||
| 40 | * |
||
| 41 | * @link https://docs.adyen.com/developers/checkout/web-sdk/release-notes-web-sdk |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | const SDK_VERSION = '1.9.2'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Client. |
||
| 49 | * |
||
| 50 | * @var Client |
||
| 51 | */ |
||
| 52 | public $client; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructs and initializes an Adyen gateway. |
||
| 56 | * |
||
| 57 | * @param Config $config Config. |
||
| 58 | */ |
||
| 59 | 1 | public function __construct( Config $config ) { |
|
| 60 | 1 | parent::__construct( $config ); |
|
| 61 | |||
| 62 | 1 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
|
| 63 | 1 | $this->set_slug( self::SLUG ); |
|
| 64 | |||
| 65 | 1 | $this->client = new Client( $config ); |
|
| 66 | 1 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Get supported payment methods |
||
| 70 | * |
||
| 71 | * @see Core_Gateway::get_supported_payment_methods() |
||
| 72 | */ |
||
| 73 | 1 | public function get_supported_payment_methods() { |
|
| 74 | return array( |
||
| 75 | 1 | PaymentMethods::BANCONTACT, |
|
| 76 | 1 | PaymentMethods::CREDIT_CARD, |
|
| 77 | 1 | PaymentMethods::DIRECT_DEBIT, |
|
| 78 | 1 | PaymentMethods::GIROPAY, |
|
| 79 | 1 | PaymentMethods::IDEAL, |
|
| 80 | 1 | PaymentMethods::MAESTRO, |
|
| 81 | 1 | PaymentMethods::SOFORT, |
|
| 82 | ); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Start. |
||
| 87 | * |
||
| 88 | * @see Plugin::start() |
||
| 89 | * |
||
| 90 | * @param Payment $payment Payment. |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public function start( Payment $payment ) { |
||
| 94 | // Amount. |
||
| 95 | $amount = AmountTransformer::transform( $payment->get_total_amount() ); |
||
| 96 | |||
| 97 | // Payment method type. |
||
| 98 | $payment_method_type = PaymentMethodType::transform( $payment->get_method() ); |
||
| 99 | |||
| 100 | // Country. |
||
| 101 | $locale = get_locale(); |
||
| 102 | |||
| 103 | $customer = $payment->get_customer(); |
||
| 104 | |||
| 105 | if ( null !== $customer ) { |
||
| 106 | $locale = $customer->get_locale(); |
||
| 107 | } |
||
| 108 | |||
| 109 | $locale = strval( $locale ); |
||
| 110 | |||
| 111 | $country_code = Locale::getRegion( $locale ); |
||
| 112 | |||
| 113 | /* |
||
| 114 | * API Integration |
||
| 115 | * |
||
| 116 | * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments |
||
| 117 | */ |
||
| 118 | $api_integration_payment_method_types = array( |
||
| 119 | PaymentMethodType::IDEAL, |
||
| 120 | PaymentMethodType::DIRECT_EBANKING, |
||
| 121 | ); |
||
| 122 | |||
| 123 | if ( in_array( $payment_method_type, $api_integration_payment_method_types, true ) ) { |
||
| 124 | $payment_method = new PaymentMethod( $payment_method_type ); |
||
| 125 | |||
| 126 | if ( PaymentMethodType::IDEAL === $payment_method_type ) { |
||
| 127 | $payment_method = new PaymentMethodIDeal( $payment_method_type, $payment->get_issuer() ); |
||
| 128 | } |
||
| 129 | |||
| 130 | // API integration. |
||
| 131 | $payment_request = new PaymentRequest( |
||
| 132 | $amount, |
||
| 133 | $this->config->get_merchant_account(), |
||
| 134 | strval( $payment->get_id() ), |
||
| 135 | $payment->get_return_url(), |
||
| 136 | $payment_method |
||
| 137 | ); |
||
| 138 | |||
| 139 | $payment_request->set_country_code( $country_code ); |
||
| 140 | |||
| 141 | PaymentRequestHelper::complement( $payment, $payment_request ); |
||
| 142 | |||
| 143 | $payment_response = $this->client->create_payment( $payment_request ); |
||
| 144 | |||
| 145 | $payment->set_transaction_id( $payment_response->get_psp_reference() ); |
||
| 146 | |||
| 147 | $redirect = $payment_response->get_redirect(); |
||
| 148 | |||
| 149 | if ( null !== $redirect ) { |
||
| 150 | $payment->set_action_url( $redirect->get_url() ); |
||
| 151 | } |
||
| 152 | |||
| 153 | return; |
||
| 154 | } |
||
| 155 | |||
| 156 | /* |
||
| 157 | * SDK Integration |
||
| 158 | * |
||
| 159 | * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession |
||
| 160 | */ |
||
| 161 | $payment_session_request = new PaymentSessionRequest( |
||
| 162 | $amount, |
||
| 163 | $this->config->get_merchant_account(), |
||
| 164 | strval( $payment->get_id() ), |
||
| 165 | $payment->get_return_url(), |
||
| 166 | $country_code |
||
| 167 | ); |
||
| 168 | |||
| 169 | PaymentRequestHelper::complement( $payment, $payment_session_request ); |
||
| 170 | |||
| 171 | $payment_session_request->set_origin( home_url() ); |
||
| 172 | $payment_session_request->set_sdk_version( self::SDK_VERSION ); |
||
| 173 | |||
| 174 | if ( null !== $payment_method_type ) { |
||
| 175 | $payment_session_request->set_allowed_payment_methods( array( $payment_method_type ) ); |
||
| 176 | } |
||
| 177 | |||
| 178 | $payment_session_response = $this->client->create_payment_session( $payment_session_request ); |
||
| 179 | |||
| 180 | $payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION ); |
||
| 181 | $payment->set_meta( 'adyen_payment_session', $payment_session_response->get_payment_session() ); |
||
| 182 | |||
| 183 | $payment->set_action_url( $payment->get_pay_redirect_url() ); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Payment redirect. |
||
| 188 | * |
||
| 189 | * @param Payment $payment Payment. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function payment_redirect( Payment $payment ) { |
||
| 194 | $sdk_version = $payment->get_meta( 'adyen_sdk_version' ); |
||
| 195 | $payment_session = $payment->get_meta( 'adyen_payment_session' ); |
||
| 196 | |||
| 197 | if ( empty( $sdk_version ) || empty( $payment_session ) ) { |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | |||
| 201 | $url = sprintf( |
||
| 202 | 'https://checkoutshopper-%s.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.%s.min.js', |
||
| 203 | ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ), |
||
| 204 | $sdk_version |
||
| 205 | ); |
||
| 206 | |||
| 207 | wp_register_script( |
||
| 208 | 'pronamic-pay-adyen-checkout', |
||
| 209 | $url, |
||
| 210 | array( |
||
| 211 | 'jquery', |
||
| 212 | ), |
||
| 213 | $sdk_version, |
||
| 214 | false |
||
| 215 | ); |
||
| 216 | |||
| 217 | wp_localize_script( |
||
| 218 | 'pronamic-pay-adyen-checkout', |
||
| 219 | 'pronamicPayAdyenCheckout', |
||
| 220 | array( |
||
| 221 | 'paymentsResultUrl' => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/result/' . $payment->config_id ), |
||
| 222 | 'paymentReturnUrl' => $payment->get_return_url(), |
||
| 223 | 'paymentSession' => $payment_session, |
||
| 224 | 'configObject' => array( |
||
| 225 | 'context' => ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ), |
||
| 226 | ), |
||
| 227 | ) |
||
| 228 | ); |
||
| 229 | |||
| 230 | // No cache. |
||
| 231 | Util::no_cache(); |
||
| 232 | |||
| 233 | require __DIR__ . '/../views/checkout.php'; |
||
| 234 | |||
| 235 | exit; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Update status of the specified payment. |
||
| 240 | * |
||
| 241 | * @param Payment $payment Payment. |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | public function update_status( Payment $payment ) { |
||
| 246 | // Process payload on return. |
||
| 247 | if ( ! filter_has_var( INPUT_GET, 'payload' ) ) { |
||
| 248 | return; |
||
| 249 | } |
||
| 250 | |||
| 251 | $payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING ); |
||
| 252 | |||
| 253 | $payment_result_request = new PaymentResultRequest( $payload ); |
||
| 254 | |||
| 255 | $payment_result_response = $this->client->get_payment_result( $payment_result_request ); |
||
| 256 | |||
| 257 | PaymentResultHelper::update_payment( $payment, $payment_result_response ); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get available payment methods. |
||
| 262 | * |
||
| 263 | * @see Core_Gateway::get_available_payment_methods() |
||
| 264 | */ |
||
| 265 | public function get_available_payment_methods() { |
||
| 266 | $core_payment_methods = array(); |
||
| 267 | |||
| 268 | $payment_methods_response = $this->client->get_payment_methods(); |
||
| 269 | |||
| 270 | foreach ( $payment_methods_response->get_payment_methods() as $payment_method ) { |
||
| 271 | $core_payment_method = PaymentMethodType::to_wp( $payment_method->get_type() ); |
||
| 272 | |||
| 273 | $core_payment_methods[] = $core_payment_method; |
||
| 274 | } |
||
| 275 | |||
| 276 | $core_payment_methods = array_filter( $core_payment_methods ); |
||
| 277 | $core_payment_methods = array_unique( $core_payment_methods ); |
||
| 278 | |||
| 279 | return $core_payment_methods; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get issuers. |
||
| 284 | * |
||
| 285 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function get_issuers() { |
||
| 289 | $issuers = array(); |
||
| 290 | |||
| 291 | $payment_methods_response = $this->client->get_payment_methods(); |
||
| 292 | |||
| 293 | $payment_methods = $payment_methods_response->get_payment_methods(); |
||
| 294 | |||
| 295 | // Limit to iDEAL payment methods. |
||
| 296 | $payment_methods = array_filter( |
||
| 297 | $payment_methods, |
||
| 298 | /** |
||
| 299 | * Check if payment method is iDEAL. |
||
| 300 | * |
||
| 301 | * @param PaymentMethod $payment_method Payment method. |
||
| 302 | * @return boolean True if payment method is iDEAL, false otherwise. |
||
| 303 | */ |
||
| 304 | function( $payment_method ) { |
||
| 305 | return ( PaymentMethodType::IDEAL === $payment_method->get_type() ); |
||
| 306 | } |
||
| 307 | ); |
||
| 308 | |||
| 309 | foreach ( $payment_methods as $payment_method ) { |
||
| 310 | $details = $payment_method->get_details(); |
||
| 311 | |||
| 312 | if ( is_array( $details ) ) { |
||
| 313 | foreach ( $details as $detail ) { |
||
| 314 | if ( 'issuer' === $detail->key && 'select' === $detail->type ) { |
||
| 315 | foreach ( $detail->items as $item ) { |
||
| 316 | $issuers[ $item->id ] = $item->name; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | if ( empty( $issuers ) ) { |
||
| 324 | return $issuers; |
||
| 325 | } |
||
| 326 | |||
| 327 | return array( |
||
| 328 | array( |
||
| 329 | 'options' => $issuers, |
||
| 330 | ), |
||
| 331 | ); |
||
| 332 | } |
||
| 333 | } |
||
| 334 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: