diff --git a/modules/ppcp-local-alternative-payment-methods/resources/js/trustly-payment-method.js b/modules/ppcp-local-alternative-payment-methods/resources/js/trustly-payment-method.js new file mode 100644 index 000000000..92caace56 --- /dev/null +++ b/modules/ppcp-local-alternative-payment-methods/resources/js/trustly-payment-method.js @@ -0,0 +1,18 @@ +import { registerPaymentMethod } from '@woocommerce/blocks-registry'; +import { APM } from './apm-block'; + +const config = wc.wcSettings.getSetting( 'ppcp-trustly_data' ); + +registerPaymentMethod( { + name: config.id, + label:
, + content: , + edit:
, + ariaLabel: config.title, + canMakePayment: () => { + return true; + }, + supports: { + features: config.supports, + }, +} ); diff --git a/modules/ppcp-local-alternative-payment-methods/services.php b/modules/ppcp-local-alternative-payment-methods/services.php index 8cdb7e2b5..325f19ffa 100644 --- a/modules/ppcp-local-alternative-payment-methods/services.php +++ b/modules/ppcp-local-alternative-payment-methods/services.php @@ -27,33 +27,38 @@ return array( return array( 'bancontact' => array( 'id' => BancontactGateway::ID, - 'countries' => array('BE'), - 'currencies' => array('EUR'), + 'countries' => array('BE',), + 'currencies' => array('EUR',), ), 'blik' => array( 'id' => BlikGateway::ID, - 'countries' => array('PL'), - 'currencies' => array('PLN'), + 'countries' => array('PL',), + 'currencies' => array('PLN',), ), 'eps' => array( 'id' => EPSGateway::ID, - 'countries' => array('AT'), - 'currencies' => array('EUR'), + 'countries' => array('AT',), + 'currencies' => array('EUR',), ), 'ideal' => array( 'id' => IDealGateway::ID, - 'countries' => array('NL'), - 'currencies' => array('EUR'), + 'countries' => array('NL',), + 'currencies' => array('EUR',), ), 'mybank' => array( 'id' => MyBankGateway::ID, - 'countries' => array('IT'), - 'currencies' => array('EUR'), + 'countries' => array('IT',), + 'currencies' => array('EUR',), ), 'p24' => array( 'id' => P24Gateway::ID, - 'countries' => array('PL'), - 'currencies' => array('EUR', 'PLN'), + 'countries' => array('PL',), + 'currencies' => array('EUR', 'PLN',), + ), + 'trustly' => array( + 'id' => TrustlyGateway::ID, + 'countries' => array('AT', 'DE', 'DK', 'EE', 'ES', 'FI', 'GB', 'LT', 'LV', 'NL', 'NO', 'SE',), + 'currencies' => array('EUR', 'DKK', 'SEK', 'GBP', 'NOK',), ), ); }, @@ -105,6 +110,14 @@ return array( $container->get( 'wcgateway.transaction-url-provider' ) ); }, + 'ppcp-local-apms.trustly.wc-gateway' => static function ( ContainerInterface $container ): TrustlyGateway { + return new TrustlyGateway( + $container->get( 'api.endpoint.orders' ), + $container->get( 'api.factory.purchase-unit' ), + $container->get( 'wcgateway.processor.refunds' ), + $container->get( 'wcgateway.transaction-url-provider' ) + ); + }, 'ppcp-local-apms.bancontact.payment-method' => static function( ContainerInterface $container ): BancontactPaymentMethod { return new BancontactPaymentMethod( $container->get( 'ppcp-local-apms.url' ), @@ -147,4 +160,11 @@ return array( $container->get( 'ppcp-local-apms.p24.wc-gateway' ) ); }, + 'ppcp-local-apms.trustly.payment-method' => static function( ContainerInterface $container ): TrustlyPaymentMethod { + return new TrustlyPaymentMethod( + $container->get( 'ppcp-local-apms.url' ), + $container->get( 'ppcp.asset-version' ), + $container->get( 'ppcp-local-apms.trustly.wc-gateway' ) + ); + }, ); diff --git a/modules/ppcp-local-alternative-payment-methods/src/BancontactGateway.php b/modules/ppcp-local-alternative-payment-methods/src/BancontactGateway.php index 29c8c35d4..f2bb039d2 100644 --- a/modules/ppcp-local-alternative-payment-methods/src/BancontactGateway.php +++ b/modules/ppcp-local-alternative-payment-methods/src/BancontactGateway.php @@ -138,7 +138,7 @@ class BancontactGateway extends WC_Payment_Gateway { 'intent' => 'CAPTURE', 'payment_source' => array( 'bancontact' => array( - 'country_code' => 'BE', + 'country_code' => $wc_order->get_billing_country(), 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), ), ), diff --git a/modules/ppcp-local-alternative-payment-methods/src/BlikGateway.php b/modules/ppcp-local-alternative-payment-methods/src/BlikGateway.php index 39284e8bd..5eeea02c9 100644 --- a/modules/ppcp-local-alternative-payment-methods/src/BlikGateway.php +++ b/modules/ppcp-local-alternative-payment-methods/src/BlikGateway.php @@ -138,7 +138,7 @@ class BlikGateway extends WC_Payment_Gateway { 'intent' => 'CAPTURE', 'payment_source' => array( 'blik' => array( - 'country_code' => 'PL', + 'country_code' => $wc_order->get_billing_country(), 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), 'email' => $wc_order->get_billing_email(), ), diff --git a/modules/ppcp-local-alternative-payment-methods/src/EPSGateway.php b/modules/ppcp-local-alternative-payment-methods/src/EPSGateway.php index ef3094463..a8dc07c20 100644 --- a/modules/ppcp-local-alternative-payment-methods/src/EPSGateway.php +++ b/modules/ppcp-local-alternative-payment-methods/src/EPSGateway.php @@ -138,7 +138,7 @@ class EPSGateway extends WC_Payment_Gateway { 'intent' => 'CAPTURE', 'payment_source' => array( 'eps' => array( - 'country_code' => 'AT', + 'country_code' => $wc_order->get_billing_country(), 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), ), ), diff --git a/modules/ppcp-local-alternative-payment-methods/src/IDealGateway.php b/modules/ppcp-local-alternative-payment-methods/src/IDealGateway.php index 2b52bef28..02b58db8f 100644 --- a/modules/ppcp-local-alternative-payment-methods/src/IDealGateway.php +++ b/modules/ppcp-local-alternative-payment-methods/src/IDealGateway.php @@ -135,7 +135,7 @@ class IDealGateway extends WC_Payment_Gateway { $amount = $purchase_unit->amount()->to_array(); $payment_source = array( - 'country_code' => 'NL', + 'country_code' => $wc_order->get_billing_country(), 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), ); // TODO get "bic" from gateway settings. @@ -159,7 +159,6 @@ class IDealGateway extends WC_Payment_Gateway { ), ), 'application_context' => array( - //'locale' => 'en-AT', 'return_url' => $this->get_return_url( $wc_order ), 'cancel_url' => add_query_arg( 'cancelled', 'true', $this->get_return_url( $wc_order ) ), ), diff --git a/modules/ppcp-local-alternative-payment-methods/src/MyBankGateway.php b/modules/ppcp-local-alternative-payment-methods/src/MyBankGateway.php index f89a9fe05..28c66d80b 100644 --- a/modules/ppcp-local-alternative-payment-methods/src/MyBankGateway.php +++ b/modules/ppcp-local-alternative-payment-methods/src/MyBankGateway.php @@ -138,7 +138,7 @@ class MyBankGateway extends WC_Payment_Gateway { 'intent' => 'CAPTURE', 'payment_source' => array( 'mybank' => array( - 'country_code' => 'IT', + 'country_code' => $wc_order->get_billing_country(), 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), ), ), diff --git a/modules/ppcp-local-alternative-payment-methods/src/P24Gateway.php b/modules/ppcp-local-alternative-payment-methods/src/P24Gateway.php index c38b8df1d..e219f611e 100644 --- a/modules/ppcp-local-alternative-payment-methods/src/P24Gateway.php +++ b/modules/ppcp-local-alternative-payment-methods/src/P24Gateway.php @@ -138,7 +138,7 @@ class P24Gateway extends WC_Payment_Gateway { 'intent' => 'CAPTURE', 'payment_source' => array( 'p24' => array( - 'country_code' => 'PL', + 'country_code' => $wc_order->get_billing_country(), 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), 'email' => $wc_order->get_billing_email(), ), diff --git a/modules/ppcp-local-alternative-payment-methods/src/TrustlyGateway.php b/modules/ppcp-local-alternative-payment-methods/src/TrustlyGateway.php new file mode 100644 index 000000000..71f97d608 --- /dev/null +++ b/modules/ppcp-local-alternative-payment-methods/src/TrustlyGateway.php @@ -0,0 +1,249 @@ +id = self::ID; + + $this->supports = array( + 'refunds', + 'products', + ); + + $this->method_title = __( 'Trustly', 'woocommerce-paypal-payments' ); + $this->method_description = __( 'Trustly', 'woocommerce-paypal-payments' ); + + $this->title = $this->get_option( 'title', __( 'Trustly', 'woocommerce-paypal-payments' ) ); + $this->description = $this->get_option( 'description', '' ); + + $this->icon = esc_url( 'https://www.paypalobjects.com/images/checkout/alternative_payments/paypal_trustly_color.svg' ); + + $this->init_form_fields(); + $this->init_settings(); + + add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); + + $this->orders_endpoint = $orders_endpoint; + $this->purchase_unit_factory = $purchase_unit_factory; + $this->refund_processor = $refund_processor; + $this->transaction_url_provider = $transaction_url_provider; + } + + /** + * Initialize the form fields. + */ + public function init_form_fields() { + $this->form_fields = array( + 'enabled' => array( + 'title' => __( 'Enable/Disable', 'woocommerce-paypal-payments' ), + 'type' => 'checkbox', + 'label' => __( 'Trustly', 'woocommerce-paypal-payments' ), + 'default' => 'no', + 'desc_tip' => true, + 'description' => __( 'Enable/Disable Trustly payment gateway.', 'woocommerce-paypal-payments' ), + ), + 'title' => array( + 'title' => __( 'Title', 'woocommerce-paypal-payments' ), + 'type' => 'text', + 'default' => $this->title, + 'desc_tip' => true, + 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce-paypal-payments' ), + ), + 'description' => array( + 'title' => __( 'Description', 'woocommerce-paypal-payments' ), + 'type' => 'text', + 'default' => $this->description, + 'desc_tip' => true, + 'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce-paypal-payments' ), + ), + ); + } + + /** + * Processes the order. + * + * @param int $order_id The WC order ID. + * @return array + */ + public function process_payment( $order_id ) { + $wc_order = wc_get_order( $order_id ); + $wc_order->update_status( 'on-hold', __( 'Awaiting Trustly to confirm the payment.', 'woocommerce-paypal-payments' ) ); + + $purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order ); + $amount = $purchase_unit->amount()->to_array(); + + $request_body = array( + 'intent' => 'CAPTURE', + 'payment_source' => array( + 'trustly' => array( + 'country_code' => $wc_order->get_billing_country(), + 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), + ), + ), + 'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL', + 'purchase_units' => array( + array( + 'reference_id' => $purchase_unit->reference_id(), + 'amount' => array( + 'currency_code' => $amount['currency_code'], + 'value' => $amount['value'], + ), + 'custom_id' => $purchase_unit->custom_id(), + 'invoice_id' => $purchase_unit->invoice_id(), + ), + ), + 'application_context' => array( + 'locale' => $this->valid_bcp47_code(), + 'return_url' => $this->get_return_url( $wc_order ), + 'cancel_url' => add_query_arg( 'cancelled', 'true', $this->get_return_url( $wc_order ) ), + ), + ); + + try { + $response = $this->orders_endpoint->create( $request_body ); + } catch ( RuntimeException $exception ) { + $wc_order->update_status( + 'failed', + $exception->getMessage() + ); + + return array( + 'result' => 'failure', + 'redirect' => wc_get_checkout_url(), + ); + } + + $body = json_decode( $response['body'] ); + + $payer_action = ''; + foreach ( $body->links as $link ) { + if ( $link->rel === 'payer-action' ) { + $payer_action = $link->href; + } + } + + WC()->cart->empty_cart(); + + return array( + 'result' => 'success', + 'redirect' => esc_url( $payer_action ), + ); + } + + /** + * Process refund. + * + * If the gateway declares 'refunds' support, this will allow it to refund. + * a passed in amount. + * + * @param int $order_id Order ID. + * @param float $amount Refund amount. + * @param string $reason Refund reason. + * @return boolean True or false based on success, or a WP_Error object. + */ + public function process_refund( $order_id, $amount = null, $reason = '' ) { + $order = wc_get_order( $order_id ); + if ( ! is_a( $order, \WC_Order::class ) ) { + return false; + } + return $this->refund_processor->process( $order, (float) $amount, (string) $reason ); + } + + /** + * Return transaction url for this gateway and given order. + * + * @param \WC_Order $order WC order to get transaction url by. + * + * @return string + */ + public function get_transaction_url( $order ): string { + $this->view_transaction_url = $this->transaction_url_provider->get_transaction_url_base( $order ); + + return parent::get_transaction_url( $order ); + } + + /** + * Returns a PayPal-supported BCP-47 code, for example de-DE-formal becomes de-DE. + * + * @return string + */ + private function valid_bcp47_code() { + $locale = str_replace( '_', '-', get_user_locale() ); + + if ( preg_match( '/^[a-z]{2}(?:-[A-Z][a-z]{3})?(?:-(?:[A-Z]{2}))?$/', $locale ) ) { + return $locale; + } + + $parts = explode( '-', $locale ); + if ( count( $parts ) === 3 ) { + $ret = substr( $locale, 0, strrpos( $locale, '-' ) ); + if ( false !== $ret ) { + return $ret; + } + } + + return 'en'; + } +} diff --git a/modules/ppcp-local-alternative-payment-methods/src/TrustlyPaymentMethod.php b/modules/ppcp-local-alternative-payment-methods/src/TrustlyPaymentMethod.php new file mode 100644 index 000000000..8cbda2ac9 --- /dev/null +++ b/modules/ppcp-local-alternative-payment-methods/src/TrustlyPaymentMethod.php @@ -0,0 +1,97 @@ +module_url = $module_url; + $this->version = $version; + $this->gateway = $gateway; + + $this->name = TrustlyGateway::ID; + } + + /** + * {@inheritDoc} + */ + public function initialize() {} + + /** + * {@inheritDoc} + */ + public function is_active() { + return true; + } + + /** + * {@inheritDoc} + */ + public function get_payment_method_script_handles() { + wp_register_script( + 'ppcp-trustly-payment-method', + trailingslashit( $this->module_url ) . 'assets/js/trustly-payment-method.js', + array(), + $this->version, + true + ); + + return array( 'ppcp-trustly-payment-method' ); + } + + /** + * {@inheritDoc} + */ + public function get_payment_method_data() { + return array( + 'id' => $this->name, + 'title' => $this->gateway->title, + 'description' => $this->gateway->description, + 'icon' => esc_url( 'https://www.paypalobjects.com/images/checkout/alternative_payments/paypal_trustly_color.svg' ), + ); + } +} diff --git a/modules/ppcp-local-alternative-payment-methods/webpack.config.js b/modules/ppcp-local-alternative-payment-methods/webpack.config.js index 3c59b9f67..a4366ebd8 100644 --- a/modules/ppcp-local-alternative-payment-methods/webpack.config.js +++ b/modules/ppcp-local-alternative-payment-methods/webpack.config.js @@ -27,6 +27,9 @@ module.exports = { 'p24-payment-method': path.resolve( './resources/js/p24-payment-method.js' ), + 'trustly-payment-method': path.resolve( + './resources/js/trustly-payment-method.js' + ), }, output: { path: path.resolve( __dirname, 'assets/' ),