diff --git a/CHANGELOG.md b/CHANGELOG.md index 35cf134..52c1526 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased][unreleased] +## [3.0.1] - 2022-02-16 +- Added support for Klarna Pay Now and Klarna Pay Over Time. +- Added support for Afterpay and the Adyen `afterpaytouch` payment method indicator. +- Updated drop-in error handling ([#2](https://github.com/pronamic/wp-pronamic-pay-adyen/issues/2)). + ## [3.0.0] - 2022-01-11 ### Changed - Updated to https://github.com/pronamic/wp-pay-core/releases/tag/4.0.0. @@ -114,7 +119,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 1.0.0 - 2019-03-28 - First release. -[unreleased]: https://github.com/wp-pay-gateways/adyen/compare/3.0.0...HEAD +[unreleased]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/3.0.1...HEAD +[3.0.1]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/3.0.0...3.0.1 [3.0.0]: https://github.com/wp-pay-gateways/adyen/compare/2.0.4...3.0.0 [2.0.4]: https://github.com/wp-pay-gateways/adyen/compare/2.0.3...2.0.4 [2.0.3]: https://github.com/wp-pay-gateways/adyen/compare/2.0.2...2.0.3 diff --git a/js/dist/checkout-drop-in.js b/js/dist/checkout-drop-in.js index a5cc9ce..48f1a82 100644 --- a/js/dist/checkout-drop-in.js +++ b/js/dist/checkout-drop-in.js @@ -3,57 +3,24 @@ /* global AdyenCheckout, pronamicPayAdyenCheckout */ (function () { 'use strict'; + /** + * Send request using Fetch API. + */ - var checkout = new AdyenCheckout(pronamicPayAdyenCheckout.configuration); - - var validate_http_status = function validate_http_status(response) { - if (response.status >= 200 && response.status < 300) { - return Promise.resolve(response); - } - - return Promise.reject(new Error(response.statusText)); - }; - - var get_json = function get_json(response) { - return response.json(); + var send_request = function send_request(url, data) { + return fetch(url, { + method: 'POST', + cache: 'no-cache', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }); }; - - if (pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay) { - pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay.onValidateMerchant = function (resolve, reject, validationUrl) { - send_request(pronamicPayAdyenCheckout.applePayMerchantValidationUrl, { - validation_url: validationUrl - }).then(validate_http_status).then(get_json).then(function (data) { - // Handle Pronamic Pay error. - if (data.error) { - return Promise.reject(new Error(data.error)); - } // Handle Adyen error. - - - if (data.statusMessage) { - return Promise.reject(new Error(data.statusMessage)); - } - - return resolve(data); - }).catch(function (error) { - dropin.setStatus('error', { - message: error.message - }); - setTimeout(function () { - dropin.setStatus('ready'); - }, 5000); - return reject(); - }); - }; - } - - if (pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal) { - pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal.onCancel = function (data, dropin) { - dropin.setStatus('ready'); - }; - } /** * Parse JSON and check response status. - * + * + * @param response Fetch request response. * @link https://stackoverflow.com/questions/47267221/fetch-response-json-and-response-status */ @@ -69,9 +36,90 @@ return data; }); }; + /** + * Process response. + * + * @param data Object from JSON response. + */ + + + var process_response = function process_response(data) { + // Handle action object. + if (data.action) { + dropin.handleAction(data.action); + return; + } // Handle result code. + + + if (data.resultCode) { + paymentResult(data); + } + }; + /** + * Handle error. + * + * @param error + */ + + var handle_error = function handle_error(error) { + // Check syntax error name. + if ('SyntaxError' === error.name) { + error.message = pronamicPayAdyenCheckout.syntaxError; + } // Show error message. + + + dropin.setStatus('error', { + message: error.message + }); + }; + /** + * Get payment methods configuration. + * + * @return object + */ + + + var getPaymentMethodsConfiguration = function getPaymentMethodsConfiguration() { + // Compliment Apple Pay configuration. + if (pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay) { + pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay.onValidateMerchant = function (resolve, reject, validationUrl) { + send_request(pronamicPayAdyenCheckout.applePayMerchantValidationUrl, { + validation_url: validationUrl + }).then(validate_response).then(function (data) { + // Handle Apple error. + if (data.statusMessage) { + throw new Error(data.statusMessage, { + cause: data + }); + } + + resolve(data); + }).catch(function (error) { + handle_error(error); // Reject to dismiss Apple Pay overlay. + + reject(error); + }); + }; + } // Compliment PayPal configuration. + + + if (pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal) { + pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal.onCancel = function (data, dropin) { + dropin.setStatus('ready'); + }; + } + + return pronamicPayAdyenCheckout.paymentMethodsConfiguration; + }; + /** + * Adyen Checkout. + */ + + + var checkout = new AdyenCheckout(pronamicPayAdyenCheckout.configuration); var dropin = checkout.create('dropin', { - paymentMethodsConfiguration: pronamicPayAdyenCheckout.paymentMethodsConfiguration, + paymentMethodsConfiguration: getPaymentMethodsConfiguration(), onSelect: function onSelect(dropin) { var configuration = pronamicPayAdyenCheckout.configuration; @@ -79,60 +127,23 @@ dropin.submit(); } }, - onSubmit: function onSubmit(state, dropin) { + onSubmit: function onSubmit(state) { // Set loading status to prevent duplicate submits. dropin.setStatus('loading'); - send_request(pronamicPayAdyenCheckout.paymentsUrl, state.data).then(validate_response).then(function (data) { - // Handle action object. - if (data.action) { - dropin.handleAction(data.action); - return; - } // Handle result code. - - - if (data.resultCode) { - paymentResult(data); - } - }).catch(function (error) { - dropin.setStatus('error', { - message: error.message - }); - }); + send_request(pronamicPayAdyenCheckout.paymentsUrl, state.data).then(validate_response).then(process_response).catch(handle_error); }, - onAdditionalDetails: function onAdditionalDetails(state, dropin) { - send_request(pronamicPayAdyenCheckout.paymentsDetailsUrl, state.data).then(validate_http_status).then(get_json).then(function (data) { - // Handle action object. - if (data.action) { - dropin.handleAction(data.action); - } // Handle result code. - - - if (data.resultCode) { - paymentResult(data); - } - }).catch(function (error) { - throw Error(error); - }); + onAdditionalDetails: function onAdditionalDetails(state) { + send_request(pronamicPayAdyenCheckout.paymentsDetailsUrl, state.data).then(validate_response).then(process_response).catch(handle_error); } }).mount('#pronamic-pay-checkout'); - - var send_request = function send_request(url, data) { - return fetch(url, { - method: 'POST', - cache: 'no-cache', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(data) - }); - }; + /** + * Handle payment result. + * + * @param response Object from JSON response data. + * @link https://docs.adyen.com/checkout/drop-in-web#step-6-present-payment-result + */ var paymentResult = function paymentResult(response) { - /* - * Handle payment result - * - * @link https://docs.adyen.com/checkout/drop-in-web#step-6-present-payment-result - */ switch (response.resultCode) { case 'Authorised': // The payment was successful. @@ -153,11 +164,10 @@ * You'll receive a `refusalReason` in the same response, indicating the cause of the error. */ if (response.refusalReason) { - dropin.setStatus('error', { - message: response.refusalReason - }); + throw new Error(response.refusalReason); } + throw new Error(pronamicPayAdyenCheckout.unknownError); break; case 'Pending': @@ -187,12 +197,11 @@ /* * Inform the shopper that the payment was refused. Ask the shopper to try the payment again using a different payment method or card. */ - dropin.setStatus('error', { - message: pronamicPayAdyenCheckout.paymentRefused + ' (' + response.refusalReason + ')' - }); - setTimeout(function () { - dropin.setStatus('ready'); - }, 8000); + if (response.refusalReason) { + throw new Error(pronamicPayAdyenCheckout.paymentRefused + ' (' + response.refusalReason + ')'); + } + + throw new Error(pronamicPayAdyenCheckout.paymentRefused); break; case 'Received': diff --git a/js/dist/checkout-drop-in.js.map b/js/dist/checkout-drop-in.js.map index ea78105..604a292 100644 --- a/js/dist/checkout-drop-in.js.map +++ b/js/dist/checkout-drop-in.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/checkout-drop-in.js"],"names":["checkout","AdyenCheckout","pronamicPayAdyenCheckout","configuration","validate_http_status","response","status","Promise","resolve","reject","Error","statusText","get_json","json","paymentMethodsConfiguration","applepay","onValidateMerchant","validationUrl","send_request","applePayMerchantValidationUrl","validation_url","then","data","error","statusMessage","catch","dropin","setStatus","message","setTimeout","paypal","onCancel","validate_response","cause","create","onSelect","showPayButton","submit","onSubmit","state","paymentsUrl","action","handleAction","resultCode","paymentResult","onAdditionalDetails","paymentsDetailsUrl","mount","url","fetch","method","cache","headers","body","JSON","stringify","paymentAuthorised","window","location","href","paymentReturnUrl","refusalReason","paymentRefused","paymentReceived"],"mappings":";;AAAA;AACA,CAAE,YAAY;AACb;;AAEA,MAAMA,QAAQ,GAAG,IAAIC,aAAJ,CAAmBC,wBAAwB,CAACC,aAA5C,CAAjB;;AAEA,MAAMC,oBAAoB,GAAG,SAAvBA,oBAAuB,CAAAC,QAAQ,EAAI;AACxC,QAAKA,QAAQ,CAACC,MAAT,IAAmB,GAAnB,IAA0BD,QAAQ,CAACC,MAAT,GAAkB,GAAjD,EAAuD;AACtD,aAAOC,OAAO,CAACC,OAAR,CAAiBH,QAAjB,CAAP;AACA;;AAED,WAAOE,OAAO,CAACE,MAAR,CAAgB,IAAIC,KAAJ,CAAWL,QAAQ,CAACM,UAApB,CAAhB,CAAP;AACA,GAND;;AAQA,MAAMC,QAAQ,GAAG,SAAXA,QAAW,CAAAP,QAAQ;AAAA,WAAIA,QAAQ,CAACQ,IAAT,EAAJ;AAAA,GAAzB;;AAEA,MAAKX,wBAAwB,CAACY,2BAAzB,CAAqDC,QAA1D,EAAqE;AACpEb,IAAAA,wBAAwB,CAACY,2BAAzB,CAAqDC,QAArD,CAA8DC,kBAA9D,GAAmF,UAAER,OAAF,EAAWC,MAAX,EAAmBQ,aAAnB,EAAsC;AACxHC,MAAAA,YAAY,CAAEhB,wBAAwB,CAACiB,6BAA3B,EAA0D;AAAEC,QAAAA,cAAc,EAAEH;AAAlB,OAA1D,CAAZ,CACEI,IADF,CACQjB,oBADR,EAEEiB,IAFF,CAEQT,QAFR,EAGES,IAHF,CAGQ,UAAAC,IAAI,EAAI;AACd;AACA,YAAKA,IAAI,CAACC,KAAV,EAAkB;AACjB,iBAAOhB,OAAO,CAACE,MAAR,CAAgB,IAAIC,KAAJ,CAAWY,IAAI,CAACC,KAAhB,CAAhB,CAAP;AACA,SAJa,CAMd;;;AACA,YAAKD,IAAI,CAACE,aAAV,EAA0B;AACzB,iBAAOjB,OAAO,CAACE,MAAR,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;AAEA;AACJ;AACA;AACA;AACA;AACA;AACA;AACI;;AACD,WAAK,SAAL;AACC;;AAEA;AACJ;AACA;AACIlC,QAAAA,MAAM,CAACC,SAAP,CAAkB,OAAlB,EAA2B;AAAEC,UAAAA,OAAO,EAAE1B,wBAAwB,CAAC4D,cAAzB,GAA0C,IAA1C,GAAiDzD,QAAQ,CAACwD,aAA1D,GAA0E;AAArF,SAA3B;AAEAhC,QAAAA,UAAU,CACT,YAAM;AACLH,UAAAA,MAAM,CAACC,SAAP,CAAkB,OAAlB;AACA,SAHQ,EAIT,IAJS,CAAV;AAOA;;AACD,WAAK,UAAL;AACC;AACAD,QAAAA,MAAM,CAACC,SAAP,CAAkB,SAAlB,EAA6B;AAAEC,UAAAA,OAAO,EAAE1B,wBAAwB,CAAC6D;AAApC,SAA7B;AAEA;AACJ;AACA;;AACIlC,QAAAA,UAAU,CACT,YAAM;AACL4B,UAAAA,MAAM,CAACC,QAAP,CAAgBC,IAAhB,GAAuBzD,wBAAwB,CAAC0D,gBAAhD;AACA,SAHQ,EAIT,IAJS,CAAV;AAOA;AAxEF;AA0EA,GAhFD;AAiFA,CArND","sourcesContent":["/* global AdyenCheckout, pronamicPayAdyenCheckout */\n( function () {\n\t'use strict';\n\n\tconst checkout = new AdyenCheckout( pronamicPayAdyenCheckout.configuration );\n\n\tconst validate_http_status = response => {\n\t\tif ( response.status >= 200 && response.status < 300 ) {\n\t\t\treturn Promise.resolve( response );\n\t\t}\n\n\t\treturn Promise.reject( new Error( response.statusText ) );\n\t};\n\n\tconst get_json = response => response.json();\n\n\tif ( pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay ) {\n\t\tpronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay.onValidateMerchant = ( resolve, reject, validationUrl ) => {\n\t\t\tsend_request( pronamicPayAdyenCheckout.applePayMerchantValidationUrl, { validation_url: validationUrl } )\n\t\t\t\t.then( validate_http_status )\n\t\t\t\t.then( get_json )\n\t\t\t\t.then( data => {\n\t\t\t\t\t// Handle Pronamic Pay error.\n\t\t\t\t\tif ( data.error ) {\n\t\t\t\t\t\treturn Promise.reject( new Error( data.error ) );\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle Adyen error.\n\t\t\t\t\tif ( data.statusMessage ) {\n\t\t\t\t\t\treturn Promise.reject( new Error( data.statusMessage ) );\n\t\t\t\t\t}\n\n\t\t\t\t\treturn resolve( data );\n\t\t\t\t} )\n\t\t\t\t.catch( error => {\n\t\t\t\t\tdropin.setStatus( 'error', { message: error.message } );\n\n\t\t\t\t\tsetTimeout( () => {dropin.setStatus( 'ready' );}, 5000 );\n\n\t\t\t\t\treturn reject();\n\t\t\t\t} );\n\t\t};\n\t}\n\n\tif ( pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal ) {\n\t\tpronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal.onCancel = ( data, dropin ) => {\n\t\t\tdropin.setStatus( 'ready' );\n\t\t};\n\t}\n\n\t/**\n\t * Parse JSON and check response status.\n\t * \n\t * @link https://stackoverflow.com/questions/47267221/fetch-response-json-and-response-status\n\t */\n\tconst validate_response = response => {\n\t\treturn response.json().then( data => {\n\t\t\tif ( 200 !== response.status ) {\n\t\t\t\tthrow new Error( data.message, {\n\t\t\t\t\tcause: data\n\t\t\t\t} );\n\t\t\t}\n\n\t\t\treturn data;\n\t\t} );\n\t};\n\n\tconst dropin = checkout.create( 'dropin', {\n\t\tpaymentMethodsConfiguration: pronamicPayAdyenCheckout.paymentMethodsConfiguration,\n\t\tonSelect: ( dropin ) => {\n\t\t\tlet configuration = pronamicPayAdyenCheckout.configuration;\n\n\t\t\tif ( false === configuration.showPayButton ) {\n\t\t\t\tdropin.submit();\n\t\t\t}\n\t\t},\n\t\tonSubmit: ( state, dropin ) => {\n\t\t\t// Set loading status to prevent duplicate submits.\n\t\t\tdropin.setStatus( 'loading' );\n\n\t\t\tsend_request( pronamicPayAdyenCheckout.paymentsUrl, state.data )\n\t\t\t.then( validate_response )\n\t\t\t.then( data => {\n\t\t\t\t// Handle action object.\n\t\t\t\tif ( data.action ) {\n\t\t\t\t\tdropin.handleAction( data.action );\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Handle result code.\n\t\t\t\tif ( data.resultCode ) {\n\t\t\t\t\tpaymentResult( data );\n\t\t\t\t}\n\t\t\t} )\n\t\t\t.catch( error => {\n\t\t\t\tdropin.setStatus( 'error', { message: error.message } );\n\t\t\t} );\n\t\t},\n\t\tonAdditionalDetails: ( state, dropin ) => {\n\t\t\tsend_request( pronamicPayAdyenCheckout.paymentsDetailsUrl, state.data )\n\t\t\t.then( validate_http_status )\n\t\t\t.then( get_json )\n\t\t\t.then( data => {\n\t\t\t\t// Handle action object.\n\t\t\t\tif ( data.action ) {\n\t\t\t\t\tdropin.handleAction( data.action );\n\t\t\t\t}\n\n\t\t\t\t// Handle result code.\n\t\t\t\tif ( data.resultCode ) {\n\t\t\t\t\tpaymentResult( data );\n\t\t\t\t}\n\t\t\t} )\n\t\t\t.catch( error => {\n\t\t\t\tthrow Error( error );\n\t\t\t} );\n\t\t}\n\t} )\n\t.mount( '#pronamic-pay-checkout' );\n\n\tconst send_request = ( url, data ) => {\n\t\treturn fetch(\n\t\t\turl,\n\t\t\t{\n\t\t\t\tmethod: 'POST',\n\t\t\t\tcache: 'no-cache',\n\t\t\t\theaders: { 'Content-Type': 'application/json' },\n\t\t\t\tbody: JSON.stringify( data )\n\t\t\t}\n\t\t);\n\t};\n\n\tconst paymentResult = ( response ) => {\n\t\t/*\n\t\t * Handle payment result\n\t\t *\n\t\t * @link https://docs.adyen.com/checkout/drop-in-web#step-6-present-payment-result\n\t\t */\n\t\tswitch ( response.resultCode ) {\n\t\t\tcase 'Authorised':\n\t\t\t\t// The payment was successful.\n\t\t\t\tdropin.setStatus( 'success', { message: pronamicPayAdyenCheckout.paymentAuthorised } );\n\n\t\t\t\t/*\n\t\t\t\t * Inform the shopper that the payment was successful.\n\t\t\t\t */\n\t\t\t\twindow.location.href = pronamicPayAdyenCheckout.paymentReturnUrl;\n\n\t\t\t\tbreak;\n\t\t\tcase 'Error':\n\t\t\t\t// Inform the shopper that there was an error processing their payment.\n\n\t\t\t\t/*\n\t\t\t\t * You'll receive a `refusalReason` in the same response, indicating the cause of the error.\n\t\t\t\t */\n\t\t\t\tif ( response.refusalReason ) {\n\t\t\t\t\tdropin.setStatus( 'error', { message: response.refusalReason } );\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\tcase 'Pending':\n\t\t\t\t// The shopper has completed the payment but the final result is not yet known.\n\n\t\t\t\t/*\n\t\t\t\t * Inform the shopper that you've received their order, and are waiting for the payment to be completed.\n\t\t\t\t */\n\t\t\t\twindow.location.href = pronamicPayAdyenCheckout.paymentReturnUrl;\n\n\t\t\t\tbreak;\n\t\t\tcase 'PresentToShopper':\n\t\t\t\t// Present the voucher or the QR code to the shopper.\n\n\t\t\t\t/*\n\t\t\t\t * For a voucher payment method, inform the shopper that you are waiting for their payment. You will receive the final result of the payment in an AUTHORISATION notification.\n\t\t\t\t *\n\t\t\t\t * For a qrCode payment method, wait for the AUTHORISATION notification before presenting the payment result to the shopper.\n\t\t\t\t *\n\t\t\t\t * @todo\n\t\t\t\t */\n\t\t\t\tbreak;\n\t\t\tcase 'Refused':\n\t\t\t\t// The payment was refused.\n\n\t\t\t\t/*\n\t\t\t\t * Inform the shopper that the payment was refused. Ask the shopper to try the payment again using a different payment method or card.\n\t\t\t\t */\n\t\t\t\tdropin.setStatus( 'error', { message: pronamicPayAdyenCheckout.paymentRefused + ' (' + response.refusalReason + ')' } );\n\n\t\t\t\tsetTimeout(\n\t\t\t\t\t() => {\n\t\t\t\t\t\tdropin.setStatus( 'ready' );\n\t\t\t\t\t},\n\t\t\t\t\t8000\n\t\t\t\t);\n\n\t\t\t\tbreak;\n\t\t\tcase 'Received':\n\t\t\t\t// For some payment methods, it can take some time before the final status of the payment is known.\n\t\t\t\tdropin.setStatus( 'success', { message: pronamicPayAdyenCheckout.paymentReceived } );\n\n\t\t\t\t/*\n\t\t\t\t * Inform the shopper that you've received their order, and are waiting for the payment to clear.\n\t\t\t\t */\n\t\t\t\tsetTimeout(\n\t\t\t\t\t() => {\n\t\t\t\t\t\twindow.location.href = pronamicPayAdyenCheckout.paymentReturnUrl;\n\t\t\t\t\t},\n\t\t\t\t\t3000\n\t\t\t\t);\n\n\t\t\t\tbreak;\n\t\t}\n\t};\n} )();\n"],"file":"checkout-drop-in.js"} \ No newline at end of file +{"version":3,"sources":["../src/checkout-drop-in.js"],"names":["send_request","url","data","fetch","method","cache","headers","body","JSON","stringify","validate_response","response","json","then","status","Error","message","cause","process_response","action","dropin","handleAction","resultCode","paymentResult","handle_error","error","name","pronamicPayAdyenCheckout","syntaxError","setStatus","getPaymentMethodsConfiguration","paymentMethodsConfiguration","applepay","onValidateMerchant","resolve","reject","validationUrl","applePayMerchantValidationUrl","validation_url","statusMessage","catch","paypal","onCancel","checkout","AdyenCheckout","configuration","create","onSelect","showPayButton","submit","onSubmit","state","paymentsUrl","onAdditionalDetails","paymentsDetailsUrl","mount","paymentAuthorised","window","location","href","paymentReturnUrl","refusalReason","unknownError","paymentRefused","paymentReceived","setTimeout"],"mappings":";;AAAA;AACA,CAAE,YAAY;AACb;AAEA;AACD;AACA;;AACC,MAAMA,YAAY,GAAG,SAAfA,YAAe,CAAEC,GAAF,EAAOC,IAAP,EAAiB;AACrC,WAAOC,KAAK,CACXF,GADW,EAEX;AACCG,MAAAA,MAAM,EAAE,MADT;AAECC,MAAAA,KAAK,EAAE,UAFR;AAGCC,MAAAA,OAAO,EAAE;AAAE,wBAAgB;AAAlB,OAHV;AAICC,MAAAA,IAAI,EAAEC,IAAI,CAACC,SAAL,CAAgBP,IAAhB;AAJP,KAFW,CAAZ;AASA,GAVD;AAYA;AACD;AACA;AACA;AACA;AACA;;;AACC,MAAMQ,iBAAiB,GAAG,SAApBA,iBAAoB,CAAAC,QAAQ,EAAI;AACrC,WAAOA,QAAQ,CAACC,IAA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C,gBAAhD;AAEA;;AACD,WAAK,kBAAL;AACC;;AAEA;AACJ;AACA;AACA;AACA;AACA;AACA;AACI;;AACD,WAAK,SAAL;AACC;;AAEA;AACJ;AACA;AACI,YAAKjD,QAAQ,CAACkD,aAAd,EAA8B;AAC7B,gBAAM,IAAI9C,KAAJ,CAAWY,wBAAwB,CAACoC,cAAzB,GAA0C,IAA1C,GAAiDpD,QAAQ,CAACkD,aAA1D,GAA0E,GAArF,CAAN;AACA;;AAED,cAAM,IAAI9C,KAAJ,CAAWY,wBAAwB,CAACoC,cAApC,CAAN;AAEA;;AACD,WAAK,UAAL;AACC;AACA3C,QAAAA,MAAM,CAACS,SAAP,CAAkB,SAAlB,EAA6B;AAAEb,UAAAA,OAAO,EAAEW,wBAAwB,CAACqC;AAApC,SAA7B;AAEA;AACJ;AACA;;AACIC,QAAAA,UAAU,CACT,YAAM;AACLR,UAAAA,MAAM,CAACC,QAAP,CAAgBC,IAAhB,GAAuBhC,wBAAwB,CAACiC,gBAAhD;AACA,SAHQ,EAIT,IAJS,CAAV;AAOA;AAvEF;AAyEA,GA1ED;AA2EA,CA3OD","sourcesContent":["/* global AdyenCheckout, pronamicPayAdyenCheckout */\n( function () {\n\t'use strict';\n\n\t/**\n\t * Send request using Fetch API.\n\t */\n\tconst send_request = ( url, data ) => {\n\t\treturn fetch(\n\t\t\turl,\n\t\t\t{\n\t\t\t\tmethod: 'POST',\n\t\t\t\tcache: 'no-cache',\n\t\t\t\theaders: { 'Content-Type': 'application/json' },\n\t\t\t\tbody: JSON.stringify( data )\n\t\t\t}\n\t\t);\n\t};\n\n\t/**\n\t * Parse JSON and check response status.\n\t *\n\t * @param response Fetch request response.\n\t * @link https://stackoverflow.com/questions/47267221/fetch-response-json-and-response-status\n\t */\n\tconst validate_response = response => {\n\t\treturn response.json()\n\t\t\t.then( data => {\n\t\t\t\tif ( 200 !== response.status ) {\n\t\t\t\t\tthrow new Error( data.message, {\n\t\t\t\t\t\tcause: data\n\t\t\t\t\t} );\n\t\t\t\t}\n\n\t\t\t\treturn data;\n\t\t\t} );\n\t};\n\n\t/**\n\t * Process response.\n\t *\n\t * @param data Object from JSON response.\n\t */\n\tconst process_response = data => {\n\t\t// Handle action object.\n\t\tif ( data.action ) {\n\t\t\tdropin.handleAction( data.action );\n\n\t\t\treturn;\n\t\t}\n\n\t\t// Handle result code.\n\t\tif ( data.resultCode ) {\n\t\t\tpaymentResult( data );\n\t\t}\n\t}\n\n\t/**\n\t * Handle error.\n\t *\n\t * @param error\n\t */\n\tconst handle_error = error => {\n\t\t// Check syntax error name.\n\t\tif ( 'SyntaxError' === error.name ) {\n\t\t\terror.message = pronamicPayAdyenCheckout.syntaxError;\n\t\t}\n\n\t\t// Show error message.\n\t\tdropin.setStatus( 'error', { message: error.message } );\n\t}\n\n\t/**\n\t * Get payment methods configuration.\n\t *\n\t * @return object\n\t */\n\tconst getPaymentMethodsConfiguration = () => {\n\t\t// Compliment Apple Pay configuration.\n\t\tif ( pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay ) {\n\t\t\tpronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay.onValidateMerchant = ( resolve, reject, validationUrl ) => {\n\t\t\t\tsend_request(\n\t\t\t\t\tpronamicPayAdyenCheckout.applePayMerchantValidationUrl,\n\t\t\t\t\t{\n\t\t\t\t\t\tvalidation_url: validationUrl\n\t\t\t\t\t}\n\t\t\t\t)\n\t\t\t\t.then( validate_response )\n\t\t\t\t.then( data => {\n\t\t\t\t\t// Handle Apple error.\n\t\t\t\t\tif ( data.statusMessage ) {\n\t\t\t\t\t\tthrow new Error( data.statusMessage, {\n\t\t\t\t\t\t\tcause: data\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve( data );\n\t\t\t\t} )\n\t\t\t\t.catch( error => {\n\t\t\t\t\thandle_error( error );\n\n\t\t\t\t\t// Reject to dismiss Apple Pay overlay.\n\t\t\t\t\treject( error );\n\t\t\t\t} );\n\t\t\t};\n\t\t}\n\n\t\t// Compliment PayPal configuration.\n\t\tif ( pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal ) {\n\t\t\tpronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal.onCancel = ( data, dropin ) => {\n\t\t\t\tdropin.setStatus( 'ready' );\n\t\t\t};\n\t\t}\n\n\t\treturn pronamicPayAdyenCheckout.paymentMethodsConfiguration;\n\t};\n\n\t/**\n\t * Adyen Checkout.\n\t */\n\tconst checkout = new AdyenCheckout( pronamicPayAdyenCheckout.configuration );\n\n\tconst dropin = checkout.create( 'dropin', {\n\t\tpaymentMethodsConfiguration: getPaymentMethodsConfiguration(),\n\t\tonSelect: dropin => {\n\t\t\tlet configuration = pronamicPayAdyenCheckout.configuration;\n\n\t\t\tif ( false === configuration.showPayButton ) {\n\t\t\t\tdropin.submit();\n\t\t\t}\n\t\t},\n\t\tonSubmit: ( state ) => {\n\t\t\t// Set loading status to prevent duplicate submits.\n\t\t\tdropin.setStatus( 'loading' );\n\n\t\t\tsend_request(\n\t\t\t\tpronamicPayAdyenCheckout.paymentsUrl,\n\t\t\t\tstate.data\n\t\t\t)\n\t\t\t.then( validate_response )\n\t\t\t.then( process_response )\n\t\t\t.catch( handle_error );\n\t\t},\n\t\tonAdditionalDetails: ( state ) => {\n\t\t\tsend_request(\n\t\t\t\tpronamicPayAdyenCheckout.paymentsDetailsUrl,\n\t\t\t\tstate.data\n\t\t\t)\n\t\t\t.then( validate_response )\n\t\t\t.then( process_response )\n\t\t\t.catch( handle_error );\n\t\t}\n\t} )\n\t.mount( '#pronamic-pay-checkout' );\n\n\t/**\n\t * Handle payment result.\n\t *\n\t * @param response Object from JSON response data.\n\t * @link https://docs.adyen.com/checkout/drop-in-web#step-6-present-payment-result\n\t */\n\tconst paymentResult = response => {\n\t\tswitch ( response.resultCode ) {\n\t\t\tcase 'Authorised':\n\t\t\t\t// The payment was successful.\n\t\t\t\tdropin.setStatus( 'success', { message: pronamicPayAdyenCheckout.paymentAuthorised } );\n\n\t\t\t\t/*\n\t\t\t\t * Inform the shopper that the payment was successful.\n\t\t\t\t */\n\t\t\t\twindow.location.href = pronamicPayAdyenCheckout.paymentReturnUrl;\n\n\t\t\t\tbreak;\n\t\t\tcase 'Error':\n\t\t\t\t// Inform the shopper that there was an error processing their payment.\n\n\t\t\t\t/*\n\t\t\t\t * You'll receive a `refusalReason` in the same response, indicating the cause of the error.\n\t\t\t\t */\n\t\t\t\tif ( response.refusalReason ) {\n\t\t\t\t\tthrow new Error( response.refusalReason );\n\t\t\t\t}\n\n\t\t\t\tthrow new Error( pronamicPayAdyenCheckout.unknownError );\n\n\t\t\t\tbreak;\n\t\t\tcase 'Pending':\n\t\t\t\t// The shopper has completed the payment but the final result is not yet known.\n\n\t\t\t\t/*\n\t\t\t\t * Inform the shopper that you've received their order, and are waiting for the payment to be completed.\n\t\t\t\t */\n\t\t\t\twindow.location.href = pronamicPayAdyenCheckout.paymentReturnUrl;\n\n\t\t\t\tbreak;\n\t\t\tcase 'PresentToShopper':\n\t\t\t\t// Present the voucher or the QR code to the shopper.\n\n\t\t\t\t/*\n\t\t\t\t * For a voucher payment method, inform the shopper that you are waiting for their payment. You will receive the final result of the payment in an AUTHORISATION notification.\n\t\t\t\t *\n\t\t\t\t * For a qrCode payment method, wait for the AUTHORISATION notification before presenting the payment result to the shopper.\n\t\t\t\t *\n\t\t\t\t * @todo\n\t\t\t\t */\n\t\t\t\tbreak;\n\t\t\tcase 'Refused':\n\t\t\t\t// The payment was refused.\n\n\t\t\t\t/*\n\t\t\t\t * Inform the shopper that the payment was refused. Ask the shopper to try the payment again using a different payment method or card.\n\t\t\t\t */\n\t\t\t\tif ( response.refusalReason ) {\n\t\t\t\t\tthrow new Error( pronamicPayAdyenCheckout.paymentRefused + ' (' + response.refusalReason + ')' );\n\t\t\t\t}\n\n\t\t\t\tthrow new Error( pronamicPayAdyenCheckout.paymentRefused );\n\n\t\t\t\tbreak;\n\t\t\tcase 'Received':\n\t\t\t\t// For some payment methods, it can take some time before the final status of the payment is known.\n\t\t\t\tdropin.setStatus( 'success', { message: pronamicPayAdyenCheckout.paymentReceived } );\n\n\t\t\t\t/*\n\t\t\t\t * Inform the shopper that you've received their order, and are waiting for the payment to clear.\n\t\t\t\t */\n\t\t\t\tsetTimeout(\n\t\t\t\t\t() => {\n\t\t\t\t\t\twindow.location.href = pronamicPayAdyenCheckout.paymentReturnUrl;\n\t\t\t\t\t},\n\t\t\t\t\t3000\n\t\t\t\t);\n\n\t\t\t\tbreak;\n\t\t}\n\t};\n} )();\n"],"file":"checkout-drop-in.js"} \ No newline at end of file diff --git a/js/src/checkout-drop-in.js b/js/src/checkout-drop-in.js index 4690052..dc825cc 100644 --- a/js/src/checkout-drop-in.js +++ b/js/src/checkout-drop-in.js @@ -2,141 +2,164 @@ ( function () { 'use strict'; - const checkout = new AdyenCheckout( pronamicPayAdyenCheckout.configuration ); + /** + * Send request using Fetch API. + */ + const send_request = ( url, data ) => { + return fetch( + url, + { + method: 'POST', + cache: 'no-cache', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify( data ) + } + ); + }; - const validate_http_status = response => { - if ( response.status >= 200 && response.status < 300 ) { - return Promise.resolve( response ); - } + /** + * Parse JSON and check response status. + * + * @param response Fetch request response. + * @link https://stackoverflow.com/questions/47267221/fetch-response-json-and-response-status + */ + const validate_response = response => { + return response.json() + .then( data => { + if ( 200 !== response.status ) { + throw new Error( data.message, { + cause: data + } ); + } - return Promise.reject( new Error( response.statusText ) ); + return data; + } ); }; - const get_json = response => response.json(); + /** + * Process response. + * + * @param data Object from JSON response. + */ + const process_response = data => { + // Handle action object. + if ( data.action ) { + dropin.handleAction( data.action ); - if ( pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay ) { - pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay.onValidateMerchant = ( resolve, reject, validationUrl ) => { - send_request( pronamicPayAdyenCheckout.applePayMerchantValidationUrl, { validation_url: validationUrl } ) - .then( validate_http_status ) - .then( get_json ) - .then( data => { - // Handle Pronamic Pay error. - if ( data.error ) { - return Promise.reject( new Error( data.error ) ); - } + return; + } + + // Handle result code. + if ( data.resultCode ) { + paymentResult( data ); + } + } + + /** + * Handle error. + * + * @param error + */ + const handle_error = error => { + // Check syntax error name. + if ( 'SyntaxError' === error.name ) { + error.message = pronamicPayAdyenCheckout.syntaxError; + } + + // Show error message. + dropin.setStatus( 'error', { message: error.message } ); + } - // Handle Adyen error. + /** + * Get payment methods configuration. + * + * @return object + */ + const getPaymentMethodsConfiguration = () => { + // Compliment Apple Pay configuration. + if ( pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay ) { + pronamicPayAdyenCheckout.paymentMethodsConfiguration.applepay.onValidateMerchant = ( resolve, reject, validationUrl ) => { + send_request( + pronamicPayAdyenCheckout.applePayMerchantValidationUrl, + { + validation_url: validationUrl + } + ) + .then( validate_response ) + .then( data => { + // Handle Apple error. if ( data.statusMessage ) { - return Promise.reject( new Error( data.statusMessage ) ); + throw new Error( data.statusMessage, { + cause: data + } ); } - return resolve( data ); + resolve( data ); } ) .catch( error => { - dropin.setStatus( 'error', { message: error.message } ); - - setTimeout( () => {dropin.setStatus( 'ready' );}, 5000 ); + handle_error( error ); - return reject(); + // Reject to dismiss Apple Pay overlay. + reject( error ); } ); - }; - } + }; + } - if ( pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal ) { - pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal.onCancel = ( data, dropin ) => { - dropin.setStatus( 'ready' ); - }; - } + // Compliment PayPal configuration. + if ( pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal ) { + pronamicPayAdyenCheckout.paymentMethodsConfiguration.paypal.onCancel = ( data, dropin ) => { + dropin.setStatus( 'ready' ); + }; + } + + return pronamicPayAdyenCheckout.paymentMethodsConfiguration; + }; /** - * Parse JSON and check response status. - * - * @link https://stackoverflow.com/questions/47267221/fetch-response-json-and-response-status + * Adyen Checkout. */ - const validate_response = response => { - return response.json().then( data => { - if ( 200 !== response.status ) { - throw new Error( data.message, { - cause: data - } ); - } - - return data; - } ); - }; + const checkout = new AdyenCheckout( pronamicPayAdyenCheckout.configuration ); const dropin = checkout.create( 'dropin', { - paymentMethodsConfiguration: pronamicPayAdyenCheckout.paymentMethodsConfiguration, - onSelect: ( dropin ) => { + paymentMethodsConfiguration: getPaymentMethodsConfiguration(), + onSelect: dropin => { let configuration = pronamicPayAdyenCheckout.configuration; if ( false === configuration.showPayButton ) { dropin.submit(); } }, - onSubmit: ( state, dropin ) => { + onSubmit: ( state ) => { // Set loading status to prevent duplicate submits. dropin.setStatus( 'loading' ); - send_request( pronamicPayAdyenCheckout.paymentsUrl, state.data ) + send_request( + pronamicPayAdyenCheckout.paymentsUrl, + state.data + ) .then( validate_response ) - .then( data => { - // Handle action object. - if ( data.action ) { - dropin.handleAction( data.action ); - - return; - } - - // Handle result code. - if ( data.resultCode ) { - paymentResult( data ); - } - } ) - .catch( error => { - dropin.setStatus( 'error', { message: error.message } ); - } ); + .then( process_response ) + .catch( handle_error ); }, - onAdditionalDetails: ( state, dropin ) => { - send_request( pronamicPayAdyenCheckout.paymentsDetailsUrl, state.data ) - .then( validate_http_status ) - .then( get_json ) - .then( data => { - // Handle action object. - if ( data.action ) { - dropin.handleAction( data.action ); - } - - // Handle result code. - if ( data.resultCode ) { - paymentResult( data ); - } - } ) - .catch( error => { - throw Error( error ); - } ); + onAdditionalDetails: ( state ) => { + send_request( + pronamicPayAdyenCheckout.paymentsDetailsUrl, + state.data + ) + .then( validate_response ) + .then( process_response ) + .catch( handle_error ); } } ) .mount( '#pronamic-pay-checkout' ); - const send_request = ( url, data ) => { - return fetch( - url, - { - method: 'POST', - cache: 'no-cache', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify( data ) - } - ); - }; - - const paymentResult = ( response ) => { - /* - * Handle payment result - * - * @link https://docs.adyen.com/checkout/drop-in-web#step-6-present-payment-result - */ + /** + * Handle payment result. + * + * @param response Object from JSON response data. + * @link https://docs.adyen.com/checkout/drop-in-web#step-6-present-payment-result + */ + const paymentResult = response => { switch ( response.resultCode ) { case 'Authorised': // The payment was successful. @@ -155,9 +178,11 @@ * You'll receive a `refusalReason` in the same response, indicating the cause of the error. */ if ( response.refusalReason ) { - dropin.setStatus( 'error', { message: response.refusalReason } ); + throw new Error( response.refusalReason ); } + throw new Error( pronamicPayAdyenCheckout.unknownError ); + break; case 'Pending': // The shopper has completed the payment but the final result is not yet known. @@ -185,14 +210,11 @@ /* * Inform the shopper that the payment was refused. Ask the shopper to try the payment again using a different payment method or card. */ - dropin.setStatus( 'error', { message: pronamicPayAdyenCheckout.paymentRefused + ' (' + response.refusalReason + ')' } ); + if ( response.refusalReason ) { + throw new Error( pronamicPayAdyenCheckout.paymentRefused + ' (' + response.refusalReason + ')' ); + } - setTimeout( - () => { - dropin.setStatus( 'ready' ); - }, - 8000 - ); + throw new Error( pronamicPayAdyenCheckout.paymentRefused ); break; case 'Received': diff --git a/package.json b/package.json index 42bce34..d67eb6f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "adyen", - "version": "3.0.0", + "version": "3.0.1", "description": "Adyen driver for the WordPress payment processing library.", "repository": { "type": "git", diff --git a/src/DropInGateway.php b/src/DropInGateway.php index 2f96c9e..2eac286 100644 --- a/src/DropInGateway.php +++ b/src/DropInGateway.php @@ -60,6 +60,7 @@ public function __construct( Config $config ) { */ public function get_supported_payment_methods() { return array( + PaymentMethods::AFTERPAY_COM, PaymentMethods::ALIPAY, PaymentMethods::APPLE_PAY, PaymentMethods::BANCONTACT, @@ -71,6 +72,8 @@ public function get_supported_payment_methods() { PaymentMethods::GOOGLE_PAY, PaymentMethods::IDEAL, PaymentMethods::KLARNA_PAY_LATER, + PaymentMethods::KLARNA_PAY_NOW, + PaymentMethods::KLARNA_PAY_OVER_TIME, PaymentMethods::MB_WAY, PaymentMethods::SOFORT, PaymentMethods::SWISH, @@ -308,6 +311,8 @@ public function payment_redirect( Payment $payment ) { 'paymentAuthorised' => __( 'Payment completed successfully.', 'pronamic_ideal' ), 'paymentReceived' => __( 'The order has been received and we are waiting for the payment to clear.', 'pronamic_ideal' ), 'paymentRefused' => __( 'The payment has been refused. Please try again using a different method or card.', 'pronamic_ideal' ), + 'syntaxError' => __( 'Received an invalid response while processing your request. Please try reloading this page.', 'pronamic_ideal' ), + 'unknownError' => __( 'An unknown error occurred while processing your request. Please try reloading this page.', 'pronamic_ideal' ), ) ); @@ -355,7 +360,7 @@ public function update_status( Payment $payment ) { PaymentResultHelper::update_payment( $payment, $payment_result_response ); } catch ( \Exception $e ) { $note = sprintf( - /* translators: %s: exception message */ + /* translators: %s: exception message */ __( 'Error getting payment result: %s', 'pronamic_ideal' ), $e->getMessage() ); diff --git a/src/PaymentMethodType.php b/src/PaymentMethodType.php index 50dad24..5ef697d 100644 --- a/src/PaymentMethodType.php +++ b/src/PaymentMethodType.php @@ -34,12 +34,25 @@ class PaymentMethodType { /** * Constant for the 'afterpay_default' payment method type. * - * Note: this is for Afterpay (afterpay.com) and not for the AfterPay (afterpay.nl). + * Note: this is for Afterpay (afterpay.com) and not for AfterPay (afterpay.nl). * + * @deprecated We have deprecated this constant because we can no longer find it in the Adyen documentation. * @var string */ const AFTERPAY = 'afterpay_default'; + /** + * Constant for the 'afterpaytouch' payment method type. + * + * Note: this is for Afterpay (afterpay.com) and not for AfterPay (afterpay.nl). + * + * @link https://en.wikipedia.org/wiki/Afterpay + * @link https://docs.adyen.com/payment-methods/afterpaytouch/api-only + * @link https://docs.adyen.com/payment-methods/afterpaytouch + * @var string + */ + const AFTERPAY_TOUCH = 'afterpaytouch'; + /** * Constant for the 'alipay' payment method type. * @@ -118,12 +131,29 @@ class PaymentMethodType { const IDEAL = 'ideal'; /** - * Constant for the 'klarna' payment method type. + * Constant for the 'klarna' payment method type, for Klarna — Pay later. * + * @link https://docs.adyen.com/payment-methods/klarna/api-only#make-a-payment * @var string */ const KLARNA = 'klarna'; + /** + * Constant for the 'klarna_paynow' payment method type, for Klarna — Pay Now. + * + * @link https://docs.adyen.com/payment-methods/klarna/api-only#make-a-payment + * @var string + */ + const KLARNA_PAY_NOW = 'klarna_paynow'; + + /** + * Constant for the 'klarna_account' payment method type, for Klarna — Pay over time. + * + * @link https://docs.adyen.com/payment-methods/klarna/api-only#make-a-payment + * @var string + */ + const KLARNA_ACCOUNT = 'klarna_account'; + /** * Constant for the 'maestro' payment method type. * @@ -193,25 +223,27 @@ class PaymentMethodType { * @var array */ private static $map = array( - PaymentMethods::AFTERPAY_COM => self::AFTERPAY, - PaymentMethods::ALIPAY => self::ALIPAY, - PaymentMethods::APPLE_PAY => self::APPLE_PAY, - PaymentMethods::BANCONTACT => self::BANCONTACT, - PaymentMethods::BLIK => self::BLIK, - PaymentMethods::CREDIT_CARD => self::SCHEME, - PaymentMethods::DIRECT_DEBIT => self::SEPA_DIRECT_DEBIT, - PaymentMethods::EPS => self::EPS, - PaymentMethods::GIROPAY => self::GIROPAY, - PaymentMethods::GOOGLE_PAY => self::GOOGLE_PAY, - PaymentMethods::IDEAL => self::IDEAL, - PaymentMethods::KLARNA_PAY_LATER => self::KLARNA, - PaymentMethods::MAESTRO => self::MAESTRO, - PaymentMethods::MB_WAY => self::MB_WAY, - PaymentMethods::PAYPAL => self::PAYPAL, - PaymentMethods::SOFORT => self::DIRECT_EBANKING, - PaymentMethods::SWISH => self::SWISH, - PaymentMethods::TWINT => self::TWINT, - PaymentMethods::VIPPS => self::VIPPS, + PaymentMethods::AFTERPAY_COM => self::AFTERPAY_TOUCH, + PaymentMethods::ALIPAY => self::ALIPAY, + PaymentMethods::APPLE_PAY => self::APPLE_PAY, + PaymentMethods::BANCONTACT => self::BANCONTACT, + PaymentMethods::BLIK => self::BLIK, + PaymentMethods::CREDIT_CARD => self::SCHEME, + PaymentMethods::DIRECT_DEBIT => self::SEPA_DIRECT_DEBIT, + PaymentMethods::EPS => self::EPS, + PaymentMethods::GIROPAY => self::GIROPAY, + PaymentMethods::GOOGLE_PAY => self::GOOGLE_PAY, + PaymentMethods::IDEAL => self::IDEAL, + PaymentMethods::KLARNA_PAY_LATER => self::KLARNA, + PaymentMethods::KLARNA_PAY_NOW => self::KLARNA_PAY_NOW, + PaymentMethods::KLARNA_PAY_OVER_TIME => self::KLARNA_ACCOUNT, + PaymentMethods::MAESTRO => self::MAESTRO, + PaymentMethods::MB_WAY => self::MB_WAY, + PaymentMethods::PAYPAL => self::PAYPAL, + PaymentMethods::SOFORT => self::DIRECT_EBANKING, + PaymentMethods::SWISH => self::SWISH, + PaymentMethods::TWINT => self::TWINT, + PaymentMethods::VIPPS => self::VIPPS, ); /**