diff --git a/src/Plugin.php b/src/Plugin.php index f1821cfe..bc8f5de1 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -264,9 +264,11 @@ public function __construct( $args = [] ) { // Integrations. $this->integrations = []; - add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ], 0 ); + $this->payment_methods = new PaymentMethodsCollection(); - // Register styles. + add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ], 0 ); + add_action( 'init', [ $this, 'register_payment_methods' ], 0 ); + add_action( 'init', [ $this, 'register_integrations' ], 0 ); add_action( 'init', [ $this, 'register_styles' ], 9 ); // If WordPress is loaded check on returns and maybe redirect requests. @@ -293,819 +295,890 @@ public function __construct( $args = [] ) { } require_once $args['action_scheduler']; + } - /** - * Pronamic Pay logos. - * - * @link https://github.com/pronamic/wp-pay-logos - */ - $image_service = new ImageService(); + /** + * Get payment methods. + * + * @param array $args Query arguments. + * @return PaymentMethodsCollection + */ + public function get_payment_methods( $args = [] ) { + return $this->payment_methods->query( $args ); + } - /** - * Get Buy Now, Pay Later disclaimer. - * - * @link https://github.com/pronamic/pronamic-pay/issues/70 - * @param string $provider Provider. - * @return string - */ - /* translators: %s: provider */ - $bnpl_disclaimer_template = \__( 'You must be at least 18+ to use this service. If you pay on time, you will avoid additional costs and ensure that you can use %s services again in the future. By continuing, you accept the Terms and Conditions and confirm that you have read the Privacy Statement and Cookie Statement.', 'pronamic_ideal' ); + /** + * Get the version number of this plugin. + * + * @return string The version number of this plugin. + */ + public function get_version() { + return $this->version; + } - /** - * Payment methods. - */ - $this->payment_methods = new PaymentMethodsCollection(); + /** + * Get plugin file path. + * + * @return string + */ + public function get_file() { + return self::$file; + } - // AfterPay.nl. - $payment_method_afterpay_nl = new PaymentMethod( PaymentMethods::AFTERPAY_NL ); + /** + * Get option. + * + * @param string $option Name of option to retrieve. + * @return string|null + */ + public function get_option( $option ) { + if ( array_key_exists( $option, $this->options ) ) { + return $this->options[ $option ]; + } - $payment_method_afterpay_nl->descriptions = [ - /** - * AfterPay method description. - * - * @link https://www.afterpay.nl/en/customers/where-can-i-pay-with-afterpay - */ - 'default' => \__( 'AfterPay is one of the largest and most popular post-payment system in the Benelux. Millions of Dutch and Belgians use AfterPay to pay for products.', 'pronamic_ideal' ), - ]; + return null; + } - $payment_method_afterpay_nl->images = [ - 'woocommerce' => $image_service->get_path( 'methods/afterpay-nl/method-afterpay-nl-wc-51x32.svg' ), - ]; + /** + * Get the plugin dir path. + * + * @return string + */ + public function get_plugin_dir_path() { + return plugin_dir_path( $this->get_file() ); + } - $this->payment_methods->add( $payment_method_afterpay_nl ); + /** + * Update payment. + * + * @param Payment $payment The payment to update. + * @param bool $can_redirect Flag to indicate if redirect is allowed after the payment update. + * @return void + */ + public static function update_payment( $payment = null, $can_redirect = true ) { + if ( empty( $payment ) ) { + return; + } - // AfterPay.com. - $payment_method_afterpay_com = new PaymentMethod( PaymentMethods::AFTERPAY_COM ); + // Gateway. + $gateway = $payment->get_gateway(); - $payment_method_afterpay_com->descriptions = [ - /** - * Afterpay method description. - * - * @link https://en.wikipedia.org/wiki/Afterpay - * @link https://docs.adyen.com/payment-methods/afterpaytouch - */ - 'default' => \__( 'Afterpay is a popular buy now, pay later service in Australia, New Zealand, the United States, and Canada.', 'pronamic_ideal' ), - ]; + if ( null === $gateway ) { + return; + } - $payment_method_afterpay_com->images = [ - 'woocommerce' => $image_service->get_path( 'methods/afterpay-com/method-afterpay-com-wc-51x32.svg' ), - ]; + // Update status. + try { + $gateway->update_status( $payment ); - $this->payment_methods->add( $payment_method_afterpay_com ); + // Update payment in data store. + $payment->save(); + } catch ( \Exception $error ) { + $message = $error->getMessage(); - // Alipay. - $payment_method_alipay = new PaymentMethod( PaymentMethods::ALIPAY ); + // Maybe include error code in message. + $code = $error->getCode(); - $payment_method_alipay->images = [ - 'woocommerce' => $image_service->get_path( 'methods/alipay/method-alipay-wc-51x32.svg' ), - ]; + if ( $code > 0 ) { + $message = \sprintf( '%s: %s', $code, $message ); + } - $this->payment_methods->add( $payment_method_alipay ); + // Add note. + $payment->add_note( $message ); + } - // American Express. - $payment_method_american_express = new PaymentMethod( PaymentMethods::AMERICAN_EXPRESS ); + // Maybe redirect. + if ( ! $can_redirect ) { + return; + } - $payment_method_american_express->images = [ - 'woocommerce' => $image_service->get_path( 'methods/american-express/method-american-express-wc-51x32.svg' ), - ]; + /* + * If WordPress is doing cron we can't redirect. + * + * @link https://github.com/pronamic/wp-pronamic-ideal/commit/bb967a3e7804ecfbd83dea110eb8810cbad097d7 + * @link https://github.com/pronamic/wp-pronamic-ideal/commit/3ab4a7c1fc2cef0b6f565f8205da42aa1203c3c5 + */ + if ( \wp_doing_cron() ) { + return; + } - $this->payment_methods->add( $payment_method_american_express ); + /* + * If WordPress CLI is running we can't redirect. + * + * @link https://basecamp.com/1810084/projects/10966871/todos/346407847 + * @link https://github.com/woocommerce/woocommerce/blob/3.5.3/includes/class-woocommerce.php#L381-L383 + */ + if ( defined( 'WP_CLI' ) && WP_CLI ) { + return; + } - // Apple Pay. - $payment_method_apple_pay = new PaymentMethod( PaymentMethods::APPLE_PAY ); + // Redirect. + $url = $payment->get_return_redirect_url(); - $payment_method_apple_pay->images = [ - 'woocommerce' => $image_service->get_path( 'methods/apple-pay/method-apple-pay-wc-51x32.svg' ), - ]; + wp_redirect( $url ); - $this->payment_methods->add( $payment_method_apple_pay ); + exit; + } - // Bancontact. - $payment_method_bancontact = new PaymentMethod( PaymentMethods::BANCONTACT ); + /** + * Handle returns. + * + * @return void + */ + public function handle_returns() { + // phpcs:disable WordPress.Security.NonceVerification.Recommended + if ( + ! \array_key_exists( 'payment', $_GET ) + || + ! \array_key_exists( 'key', $_GET ) + ) { + return; + } - $payment_method_bancontact->images = [ - 'woocommerce' => $image_service->get_path( 'methods/bancontact/method-bancontact-wc-51x32.svg' ), - ]; + $payment_id = (int) $_GET['payment']; - $this->payment_methods->add( $payment_method_bancontact ); + $payment = get_pronamic_payment( $payment_id ); - // Bank Transfer. - $payment_method_bank_transfer = new PaymentMethod( PaymentMethods::BANK_TRANSFER ); + if ( null === $payment ) { + return; + } - $payment_method_bank_transfer->images = [ - 'woocommerce' => $image_service->get_path( 'methods/bank-transfer/method-bank-transfer-wc-51x32.svg' ), - ]; + // Check if payment key is valid. + $key = \sanitize_text_field( \wp_unslash( $_GET['key'] ) ); - $this->payment_methods->add( $payment_method_bank_transfer ); + if ( $key !== $payment->key ) { + wp_safe_redirect( home_url() ); - // Belfius Direct Net. - $payment_method_belfius = new PaymentMethod( PaymentMethods::BELFIUS ); + exit; + } - $payment_method_belfius->images = [ - 'woocommerce' => $image_service->get_path( 'methods/belfius/method-belfius-wc-51x32.svg' ), - ]; + // phpcs:enable WordPress.Security.NonceVerification.Recommended - $this->payment_methods->add( $payment_method_belfius ); + // Check if we should redirect. + $should_redirect = true; - // Billie. - $payment_method_billie = new PaymentMethod( PaymentMethods::BILLIE ); + /** + * Filter whether or not to allow redirects on payment return. + * + * @param bool $should_redirect Flag to indicate if redirect is allowed on handling payment return. + * @param Payment $payment Payment. + */ + $should_redirect = apply_filters( 'pronamic_pay_return_should_redirect', $should_redirect, $payment ); - $payment_method_billie->images = [ - 'woocommerce' => $image_service->get_path( 'methods/billie/method-billie-wc-51x32.svg' ), - ]; + try { + self::update_payment( $payment, $should_redirect ); + } catch ( \Exception $e ) { + self::render_exception( $e ); - $this->payment_methods->add( $payment_method_billie ); + exit; + } + } - // Billink. - $payment_method_billink = new PaymentMethod( PaymentMethods::BILLINK ); + /** + * Maybe redirect. + * + * @return void + */ + public function maybe_redirect() { + // phpcs:disable WordPress.Security.NonceVerification.Recommended + if ( ! \array_key_exists( 'payment_redirect', $_GET ) || ! \array_key_exists( 'key', $_GET ) ) { + return; + } - $payment_method_billie->images = [ - 'woocommerce' => $image_service->get_path( 'methods/billink/method-billink-wc-51x32.svg' ), - ]; + // Get payment. + $payment_id = (int) $_GET['payment_redirect']; - $this->payment_methods->add( $payment_method_billink ); - - // Bitcoin. - $payment_method_bitcoin = new PaymentMethod( PaymentMethods::BITCOIN ); + $payment = get_pronamic_payment( $payment_id ); - $payment_method_bitcoin->images = [ - 'woocommerce' => $image_service->get_path( 'methods/bitcoin/method-bitcoin-wc-51x32.svg' ), - ]; + if ( null === $payment ) { + return; + } - $this->payment_methods->add( $payment_method_bitcoin ); + // Validate key. + $key = \sanitize_text_field( \wp_unslash( $_GET['key'] ) ); - // BLIK. - $payment_method_blik = new PaymentMethod( PaymentMethods::BLIK ); + if ( $key !== $payment->key || empty( $payment->key ) ) { + return; + } - $payment_method_blik->images = [ - 'woocommerce' => $image_service->get_path( 'methods/blik/method-blik-wc-51x32.svg' ), - ]; + // phpcs:enable WordPress.Security.NonceVerification.Recommended - $this->payment_methods->add( $payment_method_blik ); + Core_Util::no_cache(); - // Bunq. - $payment_method_bunq = new PaymentMethod( PaymentMethods::BUNQ ); + $gateway = $payment->get_gateway(); - $payment_method_bunq->images = [ - 'woocommerce' => $image_service->get_path( 'methods/bunq/method-bunq-wc-51x32.svg' ), - ]; + if ( null !== $gateway ) { + // Give gateway a chance to handle redirect. + $gateway->payment_redirect( $payment ); - $this->payment_methods->add( $payment_method_bunq ); + // Handle HTML form redirect. + if ( $gateway->is_html_form() ) { + $gateway->redirect( $payment ); + } + } - // In3. - $payment_method_in3 = new PaymentMethod( PaymentMethods::IN3 ); + // Redirect to payment action URL. + $action_url = $payment->get_action_url(); - $payment_method_in3->descriptions = [ - 'customer' => \sprintf( $bnpl_disclaimer_template, $payment_method_in3->name ), - ]; + if ( ! empty( $action_url ) ) { + wp_redirect( $action_url ); - $payment_method_in3->images = [ - 'woocommerce' => $image_service->get_path( 'methods/in3/method-in3-wc-51x32.svg' ), - ]; + exit; + } + } - $this->payment_methods->add( $payment_method_in3 ); + /** + * Get number payments. + * + * @link https://developer.wordpress.org/reference/functions/wp_count_posts/ + * + * @return int|false + */ + public static function get_number_payments() { + $number = false; - // Capayable. - $payment_method_capayable = new PaymentMethod( PaymentMethods::CAPAYABLE ); + $count = wp_count_posts( 'pronamic_payment' ); - $payment_method_capayable->images = []; + if ( isset( $count->payment_completed ) ) { + $number = intval( $count->payment_completed ); + } - $this->payment_methods->add( $payment_method_capayable ); + return $number; + } - // Card. - $payment_method_card = new PaymentMethod( PaymentMethods::CARD ); + /** + * Plugins loaded. + * + * @link https://developer.wordpress.org/reference/hooks/plugins_loaded/ + * @return void + */ + public function plugins_loaded() { + // Settings. + $this->settings = new Settings( $this ); - $payment_method_card->descriptions = [ - 'default' => \__( 'The most popular payment method in the world. Offers customers a safe and trusted way to pay online. Customers can pay for their order quickly and easily with their card, without having to worry about their security. It is possible to charge a payment surcharge for card costs.', 'pronamic_ideal' ), - ]; + // Data Stores. + $this->gateways_data_store = new GatewaysDataStoreCPT(); + $this->payments_data_store = new PaymentsDataStoreCPT(); + $this->subscriptions_data_store = new SubscriptionsDataStoreCPT(); - $payment_method_card->images = [ - 'woocommerce' => $image_service->get_path( 'methods/credit-card/method-credit-card-wc-51x32.svg' ), - ]; + // Post Types. + $this->gateway_post_type = new GatewayPostType(); + $this->payment_post_type = new PaymentPostType(); + $this->subscription_post_type = new SubscriptionPostType(); - $this->payment_methods->add( $payment_method_card ); + // Privacy Manager. + $this->privacy_manager = new PrivacyManager(); - // Credit card. - $payment_method_credit_card = new PaymentMethod( PaymentMethods::CREDIT_CARD ); + // Webhook Logger. + $this->webhook_logger = new WebhookLogger(); + $this->webhook_logger->setup(); - $payment_method_credit_card->descriptions = [ - 'default' => \__( 'The most popular payment method in the world. Offers customers a safe and trusted way to pay online. Customers can pay for their order quickly and easily with their credit card, without having to worry about their security. It is possible to charge a payment surcharge for credit card costs.', 'pronamic_ideal' ), - ]; + // Modules. + $this->payments_module = new Payments\PaymentsModule( $this ); + $this->subscriptions_module = new Subscriptions\SubscriptionsModule( $this ); + $this->tracking_module = new TrackingModule(); - $payment_method_credit_card->images = [ - 'woocommerce' => $image_service->get_path( 'methods/credit-card/method-credit-card-wc-51x32.svg' ), - ]; + // Blocks module. + if ( function_exists( 'register_block_type' ) ) { + $this->blocks_module = new Blocks\BlocksModule(); + $this->blocks_module->setup(); + } - $this->payment_methods->add( $payment_method_credit_card ); + // Admin. + if ( is_admin() ) { + $this->admin = new Admin\AdminModule( $this ); + } - // Direct debit. - $payment_method_direct_debit = new PaymentMethod( PaymentMethods::DIRECT_DEBIT ); + new Admin\Install( $this ); - $payment_method_direct_debit->images = [ - 'woocommerce' => $image_service->get_path( 'methods/direct-debit/method-direct-debit-wc-51x32.svg' ), + $controllers = [ + new PagesController(), + new HomeUrlController(), + new ActionSchedulerController(), ]; - $this->payment_methods->add( $payment_method_direct_debit ); + foreach ( $controllers as $controller ) { + $controller->setup(); + } - /* translators: %s: payment method */ - $description_template = \__( 'By using this payment method you authorize us via %s to debit payments from your bank account.', 'pronamic_ideal' ); + // Filters. + \add_filter( 'pronamic_payment_redirect_url', [ $this, 'payment_redirect_url' ], 10, 2 ); - // Direct debit (mandate via Bancontact). - $payment_method_direct_debit_bancontact = new PaymentMethod( PaymentMethods::DIRECT_DEBIT_BANCONTACT ); + // Actions. + \add_action( 'pronamic_pay_pre_create_payment', [ __CLASS__, 'complement_payment' ], 10, 1 ); + } - $payment_method_direct_debit_bancontact->descriptions = [ - 'customer' => \sprintf( $description_template, $payment_method_direct_debit_bancontact->name ), - ]; + /** + * Default date time format. + * + * @param string $format Format. + * + * @return string + */ + public function datetime_format( $format ) { + $format = _x( 'D j M Y \a\t H:i', 'default datetime format', 'pronamic_ideal' ); - $payment_method_direct_debit_bancontact->images = [ - 'woocommerce' => $image_service->get_path( 'methods/direct-debit-bancontact/method-direct-debit-bancontact-wc-107x32.svg' ), - ]; + return $format; + } - $this->payment_methods->add( $payment_method_direct_debit_bancontact ); + /** + * Get default error message. + * + * @return string + */ + public static function get_default_error_message() { + return __( 'Something went wrong with the payment. Please try again or pay another way.', 'pronamic_ideal' ); + } - // Direct debit (mandate via Bancontact). - $payment_method_direct_debit_ideal = new PaymentMethod( PaymentMethods::DIRECT_DEBIT_IDEAL ); + /** + * Register styles. + * + * @since 2.1.6 + * @return void + */ + public function register_styles() { + $min = \SCRIPT_DEBUG ? '' : '.min'; - $payment_method_direct_debit_ideal->descriptions = [ - 'customer' => \sprintf( $description_template, $payment_method_direct_debit_ideal->name ), - ]; + \wp_register_style( + 'pronamic-pay-redirect', + \plugins_url( 'css/redirect' . $min . '.css', __DIR__ ), + [], + $this->get_version() + ); + } - $payment_method_direct_debit_ideal->images = [ - 'woocommerce' => $image_service->get_path( 'methods/direct-debit-ideal/method-direct-debit-ideal-wc-107x32.svg' ), + /** + * Get config select options. + * + * @param null|string $payment_method The gateway configuration options for the specified payment method. + * + * @return array + */ + public static function get_config_select_options( $payment_method = null ) { + $args = [ + 'post_type' => 'pronamic_gateway', + 'orderby' => 'post_title', + 'order' => 'ASC', + 'nopaging' => true, ]; - $this->payment_methods->add( $payment_method_direct_debit_ideal ); + if ( null !== $payment_method ) { + $config_ids = PaymentMethods::get_config_ids( $payment_method ); - // Direct debit (mandate via SOFORT). - $payment_method_direct_debit_sofort = new PaymentMethod( PaymentMethods::DIRECT_DEBIT_SOFORT ); + $args['post__in'] = empty( $config_ids ) ? [ 0 ] : $config_ids; + } - $payment_method_direct_debit_sofort->descriptions = [ - 'customer' => \sprintf( $description_template, $payment_method_direct_debit_sofort->name ), - ]; + $query = new WP_Query( $args ); - $payment_method_direct_debit_sofort->images = [ - 'woocommerce' => $image_service->get_path( 'methods/direct-debit-sofort/method-direct-debit-sofort-wc-107x32.svg' ), - ]; + $options = [ __( '— Select Configuration —', 'pronamic_ideal' ) ]; - $this->payment_methods->add( $payment_method_direct_debit_sofort ); + foreach ( $query->posts as $post ) { + if ( ! \is_object( $post ) ) { + continue; + } - // EPS. - $payment_method_eps = new PaymentMethod( PaymentMethods::EPS ); + $id = $post->ID; - $payment_method_eps->images = [ - 'woocommerce' => $image_service->get_path( 'methods/eps/method-eps-wc-51x32.svg' ), - ]; + $options[ $id ] = \get_the_title( $id ); + } - $this->payment_methods->add( $payment_method_eps ); + return $options; + } - // Focum. - $payment_method_focum = new PaymentMethod( PaymentMethods::FOCUM ); + /** + * Render exception. + * + * @param \Exception $exception An exception. + * @return void + */ + public static function render_exception( // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Parameter is used in include. + \Exception $exception + ) { + include __DIR__ . '/../views/exception.php'; + } - $payment_method_eps->images = [ - 'woocommerce' => $image_service->get_path( 'methods/focum/method-focum-wc-51x32.svg' ), - ]; - - $this->payment_methods->add( $payment_method_focum ); + /** + * Get gateway. + * + * @link https://wordpress.org/support/article/post-status/#default-statuses + * + * @param int $config_id A gateway configuration ID. + * @param array $args Extra arguments. + * + * @return null|Gateway + */ + public static function get_gateway( $config_id, $args = [] ) { + // Get gateway from data store. + $gateway = \pronamic_pay_plugin()->gateways_data_store->get_gateway( $config_id ); - // IDEAL. - $payment_method_ideal = new PaymentMethod( PaymentMethods::IDEAL ); + // Use gateway identifier from arguments to get new gateway. + if ( null === $gateway && ! empty( $args ) ) { + // Get integration. + $args = wp_parse_args( + $args, + [ + 'gateway_id' => \get_post_meta( $config_id, '_pronamic_gateway_id', true ), + ] + ); - $payment_method_ideal->descriptions = [ - 'customer' => \__( 'With iDEAL you can easily pay online in the secure environment of your own bank.', 'pronamic_ideal' ), - ]; + $integration = pronamic_pay_plugin()->gateway_integrations->get_integration( $args['gateway_id'] ); - $payment_method_ideal->images = [ - 'woocommerce' => $image_service->get_path( 'methods/ideal/method-ideal-wc-51x32.svg' ), - ]; + // Get new gateway. + if ( null !== $integration ) { + $gateway = $integration->get_gateway( $config_id ); + } + } - $this->payment_methods->add( $payment_method_ideal ); + return $gateway; + } - // IDEAL QR. - $payment_method_ideal_qr = new PaymentMethod( PaymentMethods::IDEALQR ); + /** + * Complement payment. + * + * @param Payment $payment Payment. + * @return void + */ + public static function complement_payment( Payment $payment ) { + // Key. + if ( null === $payment->key ) { + $payment->key = uniqid( 'pay_' ); + } - $payment_method_ideal_qr->images = [ - 'woocommerce' => $image_service->get_path( 'methods/ideal-qr/method-ideal-qr-wc-51x32.svg' ), - ]; + $origin_id = $payment->get_origin_id(); - $this->payment_methods->add( $payment_method_ideal_qr ); + if ( null === $origin_id ) { + // Queried object. + $queried_object = \get_queried_object(); + $queried_object_id = \get_queried_object_id(); - // Giropay. - $payment_method_giropay = new PaymentMethod( PaymentMethods::GIROPAY ); + if ( null !== $queried_object && $queried_object_id > 0 ) { + $origin_id = $queried_object_id; + } - $payment_method_giropay->images = [ - 'woocommerce' => $image_service->get_path( 'methods/giropay/method-giropay-wc-51x32.svg' ), - ]; + // Referer. + $referer = \wp_get_referer(); - $this->payment_methods->add( $payment_method_giropay ); + if ( null === $origin_id && false !== $referer ) { + $referer_host = \wp_parse_url( $referer, \PHP_URL_HOST ); - // Google Pay. - $payment_method_google_pay = new PaymentMethod( PaymentMethods::GOOGLE_PAY ); + if ( null === $referer_host ) { + $referer = \home_url( $referer ); + } - $payment_method_google_pay->images = [ - 'woocommerce' => $image_service->get_path( 'methods/google-pay/method-google-pay-wc-51x32.svg' ), - ]; + $post_id = \url_to_postid( $referer ); - $this->payment_methods->add( $payment_method_google_pay ); + if ( $post_id > 0 ) { + $origin_id = $post_id; + } + } - // KBC/CBC Payment Button. - $payment_method_kbc = new PaymentMethod( PaymentMethods::KBC ); + // Set origin ID. + $payment->set_origin_id( $origin_id ); + } - $payment_method_kbc->images = [ - 'woocommerce' => $image_service->get_path( 'methods/kbc/method-kbc-wc-51x32.svg' ), - ]; + // Customer. + $customer = $payment->get_customer(); - $this->payment_methods->add( $payment_method_kbc ); + if ( null === $customer ) { + $customer = new Customer(); - // Klarna. - $payment_method_klarna = new PaymentMethod( PaymentMethods::KLARNA ); + $payment->set_customer( $customer ); + } - $payment_method_klarna->images = [ - 'woocommerce' => $image_service->get_path( 'methods/klarna/method-klarna-wc-51x32.svg' ), - ]; + CustomerHelper::complement_customer( $customer ); - $this->payment_methods->add( $payment_method_klarna ); + // Billing address. + $billing_address = $payment->get_billing_address(); - // Klarna Pay Later. - $payment_method_klarna_pay_later = new PaymentMethod( PaymentMethods::KLARNA_PAY_LATER ); + if ( null !== $billing_address ) { + AddressHelper::complement_address( $billing_address ); + } - $payment_method_klarna_pay_later->images = [ - 'woocommerce' => $image_service->get_path( 'methods/klarna-pay-later/method-klarna-pay-later-wc-51x32.svg' ), - ]; + // Shipping address. + $shipping_address = $payment->get_shipping_address(); - $payment_method_klarna_pay_later->descriptions = [ - 'customer' => \sprintf( $bnpl_disclaimer_template, $payment_method_klarna_pay_later->name ), - ]; + if ( null !== $shipping_address ) { + AddressHelper::complement_address( $shipping_address ); + } - $this->payment_methods->add( $payment_method_klarna_pay_later ); + // Version. + if ( null === $payment->get_version() ) { + $payment->set_version( pronamic_pay_plugin()->get_version() ); + } - // Klarna Pay Now. - $payment_method_klarna_pay_now = new PaymentMethod( PaymentMethods::KLARNA_PAY_NOW ); + // Post data. + self::process_payment_post_data( $payment ); - $payment_method_klarna_pay_now->images = [ - 'woocommerce' => $image_service->get_path( 'methods/klarna-pay-now/method-klarna-pay-now-wc-51x32.svg' ), - ]; + // Gender. + if ( null !== $customer->get_gender() ) { + $payment->delete_meta( 'gender' ); + } - $this->payment_methods->add( $payment_method_klarna_pay_now ); + // Date of birth. + if ( null !== $customer->get_birth_date() ) { + $payment->delete_meta( 'birth_date' ); + } - // Klarna Pay Over Time. - $payment_method_klarna_pay_over_time = new PaymentMethod( PaymentMethods::KLARNA_PAY_OVER_TIME ); + /** + * If an issuer has been specified and the payment + * method is unknown, we set the payment method to + * iDEAL. This may not be correct in all cases, + * but for now Pronamic Pay works this way. + * + * @link https://github.com/wp-pay-extensions/gravityforms/blob/2.4.0/src/Processor.php#L251-L256 + * @link https://github.com/wp-pay-extensions/contact-form-7/blob/1.0.0/src/Pronamic.php#L181-L187 + * @link https://github.com/wp-pay-extensions/formidable-forms/blob/2.1.0/src/Extension.php#L318-L329 + * @link https://github.com/wp-pay-extensions/ninjaforms/blob/1.2.0/src/PaymentGateway.php#L80-L83 + * @link https://github.com/wp-pay/core/blob/2.4.0/src/Forms/FormProcessor.php#L131-L134 + */ + $issuer = $payment->get_meta( 'issuer' ); - $payment_method_klarna_pay_over_time->images = [ - 'woocommerce' => $image_service->get_path( 'methods/klarna-pay-over-time/method-klarna-pay-over-time-wc-51x32.svg' ), - ]; + $payment_method = $payment->get_payment_method(); - $this->payment_methods->add( $payment_method_klarna_pay_over_time ); + if ( null !== $issuer && null === $payment_method ) { + $payment->set_payment_method( PaymentMethods::IDEAL ); + } - // Maestro. - $payment_method_maestro = new PaymentMethod( PaymentMethods::MAESTRO ); + // Consumer bank details. + $consumer_bank_details_name = $payment->get_meta( 'consumer_bank_details_name' ); + $consumer_bank_details_iban = $payment->get_meta( 'consumer_bank_details_iban' ); - $payment_method_maestro->images = [ - 'woocommerce' => $image_service->get_path( 'methods/maestro/method-maestro-wc-51x32.svg' ), - ]; + if ( null !== $consumer_bank_details_name || null !== $consumer_bank_details_iban ) { + $consumer_bank_details = $payment->get_consumer_bank_details(); - $this->payment_methods->add( $payment_method_maestro ); + if ( null === $consumer_bank_details ) { + $consumer_bank_details = new BankAccountDetails(); + } - // Mastercard. - $payment_method_mastercard = new PaymentMethod( PaymentMethods::MASTERCARD ); + if ( null === $consumer_bank_details->get_name() ) { + $consumer_bank_details->set_name( $consumer_bank_details_name ); + } - $payment_method_mastercard->images = [ - 'woocommerce' => $image_service->get_path( 'methods/mastercard/method-mastercard-wc-51x32.svg' ), - ]; + if ( null === $consumer_bank_details->get_iban() ) { + $consumer_bank_details->set_iban( $consumer_bank_details_iban ); + } - $this->payment_methods->add( $payment_method_mastercard ); + $payment->set_consumer_bank_details( $consumer_bank_details ); + } - // MB WAY. - $payment_method_mb_way = new PaymentMethod( PaymentMethods::MB_WAY ); + // Payment lines payment. + $lines = $payment->get_lines(); - $payment_method_mb_way->images = [ - 'woocommerce' => $image_service->get_path( 'methods/mb-way/method-mb-way-wc-51x32.svg' ), - ]; + if ( null !== $lines ) { + foreach ( $lines as $line ) { + $line->set_payment( $payment ); + } + } + } - $this->payment_methods->add( $payment_method_mb_way ); + /** + * Process payment input data. + * + * @param Payment $payment Payment. + * @return void + */ + private static function process_payment_post_data( Payment $payment ) { + $gateway = $payment->get_gateway(); - // MyBank. - $payment_method_mybank = new PaymentMethod( PaymentMethods::MYBANK ); + if ( null === $gateway ) { + return; + } - $this->payment_methods->add( $payment_method_mybank ); + $payment_method = $payment->get_payment_method(); - // Payconiq. - $payment_method_payconiq = new PaymentMethod( PaymentMethods::PAYCONIQ ); + if ( null === $payment_method ) { + return; + } - $payment_method_payconiq->images = [ - 'woocommerce' => $image_service->get_path( 'methods/payconiq/method-payconiq-wc-51x32.svg' ), - ]; + $payment_method = $gateway->get_payment_method( $payment_method ); - $this->payment_methods->add( $payment_method_payconiq ); + if ( null === $payment_method ) { + return; + } - // PayPal. - $payment_method_paypal = new PaymentMethod( PaymentMethods::PAYPAL ); + foreach ( $payment_method->get_fields() as $field ) { + $id = $field->get_id(); - $payment_method_paypal->images = [ - 'woocommerce' => $image_service->get_path( 'methods/paypal/method-paypal-wc-51x32.svg' ), - ]; + // phpcs:ignore WordPress.Security.NonceVerification.Missing + if ( \array_key_exists( $id, $_POST ) ) { + // phpcs:ignore WordPress.Security.NonceVerification.Missing + $value = \sanitize_text_field( \wp_unslash( $_POST[ $id ] ) ); - $this->payment_methods->add( $payment_method_paypal ); - - // Przelewy24. - $payment_method_przelewy24 = new PaymentMethod( PaymentMethods::PRZELEWY24 ); - - $payment_method_przelewy24->images = [ - 'woocommerce' => $image_service->get_path( 'methods/przelewy24/method-przelewy24-wc-51x32.svg' ), - ]; - - $this->payment_methods->add( $payment_method_przelewy24 ); - - // Riverty. - $payment_method_riverty = new PaymentMethod( PaymentMethods::RIVERTY ); - - $payment_method_riverty->descriptions = [ - 'default' => \__( 'Riverty (formerly AfterPay) is a payment service that allows customers to pay after receiving the product.', 'pronamic_ideal' ), - 'customer' => \sprintf( $bnpl_disclaimer_template, $payment_method_riverty->name ), - ]; - - $payment_method_riverty->images = [ - 'woocommerce' => $image_service->get_path( 'methods/riverty/method-riverty-wc-51x32.svg' ), - ]; + if ( '' !== $field->meta_key ) { + $payment->set_meta( $field->meta_key, $value ); + } + } + } + } - $this->payment_methods->add( $payment_method_riverty ); + /** + * Get default gateway configuration ID. + * + * @return int|null + */ + private static function get_default_config_id() { + $value = (int) \get_option( 'pronamic_pay_config_id' ); - // Santander. - $payment_method_santander = new PaymentMethod( PaymentMethods::SANTANDER ); + if ( 0 === $value ) { + return null; + } - $payment_method_santander->images = [ - 'woocommerce' => $image_service->get_path( 'methods/santander/method-santander-wc-51x32.svg' ), - ]; + if ( 'publish' !== \get_post_status( $value ) ) { + return null; + } - $this->payment_methods->add( $payment_method_santander ); + return $value; + } - // SOFORT Banking. - $payment_method_sofort = new PaymentMethod( PaymentMethods::SOFORT ); + /** + * Start payment. + * + * @param Payment $payment The payment to start at the specified gateway. + * @return Payment + * @throws \Exception Throws exception if gateway payment start fails. + */ + public static function start_payment( Payment $payment ) { + // Set default or filtered config ID. + $config_id = $payment->get_config_id(); - $payment_method_sofort->images = [ - 'woocommerce' => $image_service->get_path( 'methods/sofort/method-sofort-wc-51x32.svg' ), - ]; + if ( null === $config_id ) { + $config_id = self::get_default_config_id(); + } - $this->payment_methods->add( $payment_method_sofort ); + /** + * Filters the payment gateway configuration ID. + * + * @param null|int $config_id Gateway configuration ID. + * @param Payment $payment Payment. + */ + $config_id = \apply_filters( 'pronamic_payment_gateway_configuration_id', $config_id, $payment ); - // SprayPay. - $payment_method_spraypay = new PaymentMethod( PaymentMethods::SPRAYPAY ); + if ( null !== $config_id ) { + $payment->set_config_id( $config_id ); + } - $payment_method_spraypay->images = [ - 'woocommerce' => $image_service->get_path( 'methods/spraypay/method-spraypay-wc-51x32.svg' ), - ]; + /** + * Merge tags. + * + * @link https://github.com/pronamic/wp-pronamic-pay/issues/358 + * @link https://github.com/pronamic/wp-pronamic-pay-woocommerce/issues/43 + */ + $payment->set_description( $payment->format_string( (string) $payment->get_description() ) ); - $this->payment_methods->add( $payment_method_spraypay ); + // Save payment. + $payment->save(); - // Swish. - $payment_method_swish = new PaymentMethod( PaymentMethods::SWISH ); + // Periods. + $periods = $payment->get_periods(); - $payment_method_swish->images = [ - 'woocommerce' => $image_service->get_path( 'methods/swish/method-swish-wc-51x32.svg' ), - ]; + if ( null !== $periods ) { + foreach ( $periods as $period ) { + $subscription = $period->get_phase()->get_subscription(); - $this->payment_methods->add( $payment_method_swish ); + $subscription->set_next_payment_date( \max( $subscription->get_next_payment_date(), $period->get_end_date() ) ); + } + } - // TWINT. - $payment_method_twint = new PaymentMethod( PaymentMethods::TWINT ); + // Subscriptions. + $subscriptions = $payment->get_subscriptions(); - $payment_method_twint->images = [ - 'woocommerce' => $image_service->get_path( 'methods/twint/method-twint-wc-51x32.svg' ), - ]; + foreach ( $subscriptions as $subscription ) { + $subscription->save(); + } - $this->payment_methods->add( $payment_method_twint ); + // Gateway. + $gateway = $payment->get_gateway(); - // V PAY. - $payment_method_v_pay = new PaymentMethod( PaymentMethods::V_PAY ); + if ( null === $gateway ) { + $payment->add_note( + \sprintf( + /* translators: %d: Gateway configuration ID */ + \__( 'Payment failed because gateway configuration with ID `%d` does not exist.', 'pronamic_ideal' ), + $config_id + ) + ); - $payment_method_v_pay->images = [ - 'woocommerce' => $image_service->get_path( 'methods/v-pay/method-v-pay-wc-51x32.svg' ), - ]; + $payment->set_status( PaymentStatus::FAILURE ); - $this->payment_methods->add( $payment_method_v_pay ); + $payment->save(); - // Vipps. - $payment_method_vipps = new PaymentMethod( PaymentMethods::VIPPS ); + return $payment; + } - $payment_method_vipps->images = [ - 'woocommerce' => $image_service->get_path( 'methods/vipps/method-vipps-wc-51x32.svg' ), - ]; + // Mode. + $payment->set_mode( $gateway->get_mode() ); - $this->payment_methods->add( $payment_method_vipps ); + // Subscriptions. + $subscriptions = $payment->get_subscriptions(); - // Visa. - $payment_method_visa = new PaymentMethod( PaymentMethods::VISA ); + // Start payment at the gateway. + try { + self::pronamic_service( $payment ); - $payment_method_visa->images = [ - 'woocommerce' => $image_service->get_path( 'methods/visa/method-visa-wc-51x32.svg' ), - ]; + $gateway->start( $payment ); + } catch ( \Exception $exception ) { + $message = $exception->getMessage(); - $this->payment_methods->add( $payment_method_visa ); - } + // Maybe include error code in message. + $code = $exception->getCode(); - /** - * Get payment methods. - * - * @param array $args Query arguments. - * @return PaymentMethodsCollection - */ - public function get_payment_methods( $args = [] ) { - return $this->payment_methods->query( $args ); - } + if ( $code > 0 ) { + $message = \sprintf( '%s: %s', $code, $message ); + } - /** - * Get the version number of this plugin. - * - * @return string The version number of this plugin. - */ - public function get_version() { - return $this->version; - } + $payment->add_note( $message ); - /** - * Get plugin file path. - * - * @return string - */ - public function get_file() { - return self::$file; - } + $payment->set_status( PaymentStatus::FAILURE ); - /** - * Get option. - * - * @param string $option Name of option to retrieve. - * @return string|null - */ - public function get_option( $option ) { - if ( array_key_exists( $option, $this->options ) ) { - return $this->options[ $option ]; + throw $exception; + } finally { + $payment->save(); } - return null; - } + // Schedule payment status check. + if ( $gateway->supports( 'payment_status_request' ) ) { + StatusChecker::schedule_event( $payment ); + } - /** - * Get the plugin dir path. - * - * @return string - */ - public function get_plugin_dir_path() { - return plugin_dir_path( $this->get_file() ); + return $payment; } /** - * Update payment. + * The Pronamic Pay service forms an abstraction layer for the various supported + * WordPress plugins and Payment Service Providers (PSP. Optionally, a risk analysis + * can be performed before payment. * - * @param Payment $payment The payment to update. - * @param bool $can_redirect Flag to indicate if redirect is allowed after the payment update. + * @param Payment $payment Payment. * @return void */ - public static function update_payment( $payment = null, $can_redirect = true ) { - if ( empty( $payment ) ) { - return; - } - - // Gateway. - $gateway = $payment->get_gateway(); - - if ( null === $gateway ) { + private static function pronamic_service( Payment $payment ) { + if ( null === self::$pronamic_service_url ) { return; } - // Update status. try { - $gateway->update_status( $payment ); - - // Update payment in data store. - $payment->save(); - } catch ( \Exception $error ) { - $message = $error->getMessage(); + $body = [ + 'license' => \get_option( 'pronamic_pay_license_key' ), + 'payment' => \wp_json_encode( $payment->get_json() ), + ]; - // Maybe include error code in message. - $code = $error->getCode(); + $map = [ + 'query' => 'GET', + 'body' => 'POST', + 'server' => 'SERVER', + ]; - if ( $code > 0 ) { - $message = \sprintf( '%s: %s', $code, $message ); + foreach ( $map as $parameter => $key ) { + $name = '_' . $key; + + $body[ $parameter ] = $GLOBALS[ $name ]; } - // Add note. - $payment->add_note( $message ); - } + $response = Http::post( + self::$pronamic_service_url, + [ + 'body' => $body, + ] + ); - // Maybe redirect. - if ( ! $can_redirect ) { - return; - } + $data = $response->json(); - /* - * If WordPress is doing cron we can't redirect. - * - * @link https://github.com/pronamic/wp-pronamic-ideal/commit/bb967a3e7804ecfbd83dea110eb8810cbad097d7 - * @link https://github.com/pronamic/wp-pronamic-ideal/commit/3ab4a7c1fc2cef0b6f565f8205da42aa1203c3c5 - */ - if ( \wp_doing_cron() ) { - return; - } + if ( ! \is_object( $data ) ) { + return; + } - /* - * If WordPress CLI is running we can't redirect. - * - * @link https://basecamp.com/1810084/projects/10966871/todos/346407847 - * @link https://github.com/woocommerce/woocommerce/blob/3.5.3/includes/class-woocommerce.php#L381-L383 - */ - if ( defined( 'WP_CLI' ) && WP_CLI ) { + if ( \property_exists( $data, 'id' ) ) { + $payment->set_meta( 'pronamic_pay_service_id', $data->id ); + } + + if ( \property_exists( $data, 'risk_score' ) ) { + $payment->set_meta( 'pronamic_pay_risk_score', $data->risk_score ); + } + } catch ( \Exception $e ) { return; } - - // Redirect. - $url = $payment->get_return_redirect_url(); - - wp_redirect( $url ); - - exit; } /** - * Handle returns. + * Create refund. * + * @param Refund $refund Refund. * @return void + * @throws \Exception Throws exception on error. */ - public function handle_returns() { - // phpcs:disable WordPress.Security.NonceVerification.Recommended - if ( - ! \array_key_exists( 'payment', $_GET ) - || - ! \array_key_exists( 'key', $_GET ) - ) { - return; - } - - $payment_id = (int) $_GET['payment']; + public static function create_refund( Refund $refund ) { + $payment = $refund->get_payment(); - $payment = get_pronamic_payment( $payment_id ); + $gateway = $payment->get_gateway(); - if ( null === $payment ) { - return; + if ( null === $gateway ) { + throw new \Exception( + \esc_html__( 'Unable to process refund as gateway could not be found.', 'pronamic_ideal' ) + ); } - // Check if payment key is valid. - $key = \sanitize_text_field( \wp_unslash( $_GET['key'] ) ); - - if ( $key !== $payment->key ) { - wp_safe_redirect( home_url() ); - - exit; - } + try { + $gateway->create_refund( $refund ); - // phpcs:enable WordPress.Security.NonceVerification.Recommended + $payment->refunds[] = $refund; - // Check if we should redirect. - $should_redirect = true; + $refunded_amount = $payment->get_refunded_amount(); - /** - * Filter whether or not to allow redirects on payment return. - * - * @param bool $should_redirect Flag to indicate if redirect is allowed on handling payment return. - * @param Payment $payment Payment. - */ - $should_redirect = apply_filters( 'pronamic_pay_return_should_redirect', $should_redirect, $payment ); + $refunded_amount = $refunded_amount->add( $refund->get_amount() ); - try { - self::update_payment( $payment, $should_redirect ); - } catch ( \Exception $e ) { - self::render_exception( $e ); + $payment->set_refunded_amount( $refunded_amount ); + } catch ( \Exception $exception ) { + $payment->add_note( $exception->getMessage() ); - exit; + throw $exception; + } finally { + $payment->save(); } } /** - * Maybe redirect. + * Payment redirect URL. * - * @return void + * @param string $url Redirect URL. + * @param Payment $payment Payment. + * @return string */ - public function maybe_redirect() { - // phpcs:disable WordPress.Security.NonceVerification.Recommended - if ( ! \array_key_exists( 'payment_redirect', $_GET ) || ! \array_key_exists( 'key', $_GET ) ) { - return; - } - - // Get payment. - $payment_id = (int) $_GET['payment_redirect']; - - $payment = get_pronamic_payment( $payment_id ); - - if ( null === $payment ) { - return; - } - - // Validate key. - $key = \sanitize_text_field( \wp_unslash( $_GET['key'] ) ); - - if ( $key !== $payment->key || empty( $payment->key ) ) { - return; - } - - // phpcs:enable WordPress.Security.NonceVerification.Recommended - - Core_Util::no_cache(); - - $gateway = $payment->get_gateway(); - - if ( null !== $gateway ) { - // Give gateway a chance to handle redirect. - $gateway->payment_redirect( $payment ); - - // Handle HTML form redirect. - if ( $gateway->is_html_form() ) { - $gateway->redirect( $payment ); - } - } - - // Redirect to payment action URL. - $action_url = $payment->get_action_url(); + public function payment_redirect_url( $url, Payment $payment ) { + $source = $payment->get_source(); - if ( ! empty( $action_url ) ) { - wp_redirect( $action_url ); + /** + * Filters the payment redirect URL by plugin integration source. + * + * @param string $url Redirect URL. + * @param Payment $payment Payment. + */ + $url = \apply_filters( 'pronamic_payment_redirect_url_' . $source, $url, $payment ); - exit; - } + return $url; } /** - * Get number payments. - * - * @link https://developer.wordpress.org/reference/functions/wp_count_posts/ + * Is debug mode. * - * @return int|false + * @link https://github.com/easydigitaldownloads/easy-digital-downloads/blob/2.9.26/includes/misc-functions.php#L26-L38 + * @return bool True if debug mode is enabled, false otherwise. */ - public static function get_number_payments() { - $number = false; - - $count = wp_count_posts( 'pronamic_payment' ); + public function is_debug_mode() { + $value = \get_option( 'pronamic_pay_debug_mode', false ); - if ( isset( $count->payment_completed ) ) { - $number = intval( $count->payment_completed ); + if ( defined( 'PRONAMIC_PAY_DEBUG' ) && PRONAMIC_PAY_DEBUG ) { + $value = true; } - return $number; + return (bool) $value; } /** - * Plugins loaded. + * Register integrations. * - * @link https://developer.wordpress.org/reference/hooks/plugins_loaded/ * @return void */ - public function plugins_loaded() { - // Settings. - $this->settings = new Settings( $this ); - - // Data Stores. - $this->gateways_data_store = new GatewaysDataStoreCPT(); - $this->payments_data_store = new PaymentsDataStoreCPT(); - $this->subscriptions_data_store = new SubscriptionsDataStoreCPT(); - - // Post Types. - $this->gateway_post_type = new GatewayPostType(); - $this->payment_post_type = new PaymentPostType(); - $this->subscription_post_type = new SubscriptionPostType(); - - // Privacy Manager. - $this->privacy_manager = new PrivacyManager(); - - // Webhook Logger. - $this->webhook_logger = new WebhookLogger(); - $this->webhook_logger->setup(); - - // Modules. - $this->payments_module = new Payments\PaymentsModule( $this ); - $this->subscriptions_module = new Subscriptions\SubscriptionsModule( $this ); - $this->tracking_module = new TrackingModule(); - - // Blocks module. - if ( function_exists( 'register_block_type' ) ) { - $this->blocks_module = new Blocks\BlocksModule(); - $this->blocks_module->setup(); - } - - // Admin. - if ( is_admin() ) { - $this->admin = new Admin\AdminModule( $this ); - } - - new Admin\Install( $this ); - - $controllers = [ - new PagesController(), - new HomeUrlController(), - new ActionSchedulerController(), - ]; - - foreach ( $controllers as $controller ) { - $controller->setup(); - } - + public function register_integrations(): void { $gateways = []; /** @@ -1141,576 +1214,514 @@ public function plugins_loaded() { // Maybes. PaymentMethods::maybe_update_active_payment_methods(); - - // Filters. - \add_filter( 'pronamic_payment_redirect_url', [ $this, 'payment_redirect_url' ], 10, 2 ); - - // Actions. - \add_action( 'pronamic_pay_pre_create_payment', [ __CLASS__, 'complement_payment' ], 10, 1 ); } /** - * Default date time format. - * - * @param string $format Format. + * Register payment methods. * - * @return string + * @return void */ - public function datetime_format( $format ) { - $format = _x( 'D j M Y \a\t H:i', 'default datetime format', 'pronamic_ideal' ); + public function register_payment_methods(): void { + /** + * Pronamic Pay logos. + * + * @link https://github.com/pronamic/wp-pay-logos + */ + $image_service = new ImageService(); - return $format; - } + /** + * Get Buy Now, Pay Later disclaimer. + * + * @link https://github.com/pronamic/pronamic-pay/issues/70 + * @param string $provider Provider. + * @return string + */ + /* translators: %s: provider */ + $bnpl_disclaimer_template = \__( 'You must be at least 18+ to use this service. If you pay on time, you will avoid additional costs and ensure that you can use %s services again in the future. By continuing, you accept the Terms and Conditions and confirm that you have read the Privacy Statement and Cookie Statement.', 'pronamic_ideal' ); - /** - * Get default error message. - * - * @return string - */ - public static function get_default_error_message() { - return __( 'Something went wrong with the payment. Please try again or pay another way.', 'pronamic_ideal' ); - } + // AfterPay.nl. + $payment_method_afterpay_nl = new PaymentMethod( PaymentMethods::AFTERPAY_NL ); - /** - * Register styles. - * - * @since 2.1.6 - * @return void - */ - public function register_styles() { - $min = \SCRIPT_DEBUG ? '' : '.min'; + $payment_method_afterpay_nl->descriptions = [ + /** + * AfterPay method description. + * + * @link https://www.afterpay.nl/en/customers/where-can-i-pay-with-afterpay + */ + 'default' => \__( 'AfterPay is one of the largest and most popular post-payment system in the Benelux. Millions of Dutch and Belgians use AfterPay to pay for products.', 'pronamic_ideal' ), + ]; - \wp_register_style( - 'pronamic-pay-redirect', - \plugins_url( 'css/redirect' . $min . '.css', __DIR__ ), - [], - $this->get_version() - ); - } + $payment_method_afterpay_nl->images = [ + 'woocommerce' => $image_service->get_path( 'methods/afterpay-nl/method-afterpay-nl-wc-51x32.svg' ), + ]; - /** - * Get config select options. - * - * @param null|string $payment_method The gateway configuration options for the specified payment method. - * - * @return array - */ - public static function get_config_select_options( $payment_method = null ) { - $args = [ - 'post_type' => 'pronamic_gateway', - 'orderby' => 'post_title', - 'order' => 'ASC', - 'nopaging' => true, + $this->payment_methods->add( $payment_method_afterpay_nl ); + + // AfterPay.com. + $payment_method_afterpay_com = new PaymentMethod( PaymentMethods::AFTERPAY_COM ); + + $payment_method_afterpay_com->descriptions = [ + /** + * Afterpay method description. + * + * @link https://en.wikipedia.org/wiki/Afterpay + * @link https://docs.adyen.com/payment-methods/afterpaytouch + */ + 'default' => \__( 'Afterpay is a popular buy now, pay later service in Australia, New Zealand, the United States, and Canada.', 'pronamic_ideal' ), ]; - if ( null !== $payment_method ) { - $config_ids = PaymentMethods::get_config_ids( $payment_method ); + $payment_method_afterpay_com->images = [ + 'woocommerce' => $image_service->get_path( 'methods/afterpay-com/method-afterpay-com-wc-51x32.svg' ), + ]; - $args['post__in'] = empty( $config_ids ) ? [ 0 ] : $config_ids; - } + $this->payment_methods->add( $payment_method_afterpay_com ); - $query = new WP_Query( $args ); + // Alipay. + $payment_method_alipay = new PaymentMethod( PaymentMethods::ALIPAY ); - $options = [ __( '— Select Configuration —', 'pronamic_ideal' ) ]; + $payment_method_alipay->images = [ + 'woocommerce' => $image_service->get_path( 'methods/alipay/method-alipay-wc-51x32.svg' ), + ]; - foreach ( $query->posts as $post ) { - if ( ! \is_object( $post ) ) { - continue; - } + $this->payment_methods->add( $payment_method_alipay ); - $id = $post->ID; + // American Express. + $payment_method_american_express = new PaymentMethod( PaymentMethods::AMERICAN_EXPRESS ); - $options[ $id ] = \get_the_title( $id ); - } + $payment_method_american_express->images = [ + 'woocommerce' => $image_service->get_path( 'methods/american-express/method-american-express-wc-51x32.svg' ), + ]; - return $options; - } + $this->payment_methods->add( $payment_method_american_express ); - /** - * Render exception. - * - * @param \Exception $exception An exception. - * @return void - */ - public static function render_exception( // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Parameter is used in include. - \Exception $exception - ) { - include __DIR__ . '/../views/exception.php'; - } + // Apple Pay. + $payment_method_apple_pay = new PaymentMethod( PaymentMethods::APPLE_PAY ); - /** - * Get gateway. - * - * @link https://wordpress.org/support/article/post-status/#default-statuses - * - * @param int $config_id A gateway configuration ID. - * @param array $args Extra arguments. - * - * @return null|Gateway - */ - public static function get_gateway( $config_id, $args = [] ) { - // Get gateway from data store. - $gateway = \pronamic_pay_plugin()->gateways_data_store->get_gateway( $config_id ); + $payment_method_apple_pay->images = [ + 'woocommerce' => $image_service->get_path( 'methods/apple-pay/method-apple-pay-wc-51x32.svg' ), + ]; - // Use gateway identifier from arguments to get new gateway. - if ( null === $gateway && ! empty( $args ) ) { - // Get integration. - $args = wp_parse_args( - $args, - [ - 'gateway_id' => \get_post_meta( $config_id, '_pronamic_gateway_id', true ), - ] - ); + $this->payment_methods->add( $payment_method_apple_pay ); - $integration = pronamic_pay_plugin()->gateway_integrations->get_integration( $args['gateway_id'] ); + // Bancontact. + $payment_method_bancontact = new PaymentMethod( PaymentMethods::BANCONTACT ); - // Get new gateway. - if ( null !== $integration ) { - $gateway = $integration->get_gateway( $config_id ); - } - } + $payment_method_bancontact->images = [ + 'woocommerce' => $image_service->get_path( 'methods/bancontact/method-bancontact-wc-51x32.svg' ), + ]; - return $gateway; - } + $this->payment_methods->add( $payment_method_bancontact ); - /** - * Complement payment. - * - * @param Payment $payment Payment. - * @return void - */ - public static function complement_payment( Payment $payment ) { - // Key. - if ( null === $payment->key ) { - $payment->key = uniqid( 'pay_' ); - } + // Bank Transfer. + $payment_method_bank_transfer = new PaymentMethod( PaymentMethods::BANK_TRANSFER ); - $origin_id = $payment->get_origin_id(); + $payment_method_bank_transfer->images = [ + 'woocommerce' => $image_service->get_path( 'methods/bank-transfer/method-bank-transfer-wc-51x32.svg' ), + ]; - if ( null === $origin_id ) { - // Queried object. - $queried_object = \get_queried_object(); - $queried_object_id = \get_queried_object_id(); + $this->payment_methods->add( $payment_method_bank_transfer ); - if ( null !== $queried_object && $queried_object_id > 0 ) { - $origin_id = $queried_object_id; - } + // Belfius Direct Net. + $payment_method_belfius = new PaymentMethod( PaymentMethods::BELFIUS ); - // Referer. - $referer = \wp_get_referer(); + $payment_method_belfius->images = [ + 'woocommerce' => $image_service->get_path( 'methods/belfius/method-belfius-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_belfius ); + + // Billie. + $payment_method_billie = new PaymentMethod( PaymentMethods::BILLIE ); + + $payment_method_billie->images = [ + 'woocommerce' => $image_service->get_path( 'methods/billie/method-billie-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_billie ); + + // Billink. + $payment_method_billink = new PaymentMethod( PaymentMethods::BILLINK ); + + $payment_method_billie->images = [ + 'woocommerce' => $image_service->get_path( 'methods/billink/method-billink-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_billink ); + + // Bitcoin. + $payment_method_bitcoin = new PaymentMethod( PaymentMethods::BITCOIN ); + + $payment_method_bitcoin->images = [ + 'woocommerce' => $image_service->get_path( 'methods/bitcoin/method-bitcoin-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_bitcoin ); + + // BLIK. + $payment_method_blik = new PaymentMethod( PaymentMethods::BLIK ); + + $payment_method_blik->images = [ + 'woocommerce' => $image_service->get_path( 'methods/blik/method-blik-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_blik ); + + // Bunq. + $payment_method_bunq = new PaymentMethod( PaymentMethods::BUNQ ); + + $payment_method_bunq->images = [ + 'woocommerce' => $image_service->get_path( 'methods/bunq/method-bunq-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_bunq ); + + // In3. + $payment_method_in3 = new PaymentMethod( PaymentMethods::IN3 ); + + $payment_method_in3->descriptions = [ + 'customer' => \sprintf( $bnpl_disclaimer_template, $payment_method_in3->name ), + ]; + + $payment_method_in3->images = [ + 'woocommerce' => $image_service->get_path( 'methods/in3/method-in3-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_in3 ); + + // Capayable. + $payment_method_capayable = new PaymentMethod( PaymentMethods::CAPAYABLE ); + + $payment_method_capayable->images = []; + + $this->payment_methods->add( $payment_method_capayable ); + + // Card. + $payment_method_card = new PaymentMethod( PaymentMethods::CARD ); + + $payment_method_card->descriptions = [ + 'default' => \__( 'The most popular payment method in the world. Offers customers a safe and trusted way to pay online. Customers can pay for their order quickly and easily with their card, without having to worry about their security. It is possible to charge a payment surcharge for card costs.', 'pronamic_ideal' ), + ]; + + $payment_method_card->images = [ + 'woocommerce' => $image_service->get_path( 'methods/credit-card/method-credit-card-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_card ); + + // Credit card. + $payment_method_credit_card = new PaymentMethod( PaymentMethods::CREDIT_CARD ); + + $payment_method_credit_card->descriptions = [ + 'default' => \__( 'The most popular payment method in the world. Offers customers a safe and trusted way to pay online. Customers can pay for their order quickly and easily with their credit card, without having to worry about their security. It is possible to charge a payment surcharge for credit card costs.', 'pronamic_ideal' ), + ]; + + $payment_method_credit_card->images = [ + 'woocommerce' => $image_service->get_path( 'methods/credit-card/method-credit-card-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_credit_card ); + + // Direct debit. + $payment_method_direct_debit = new PaymentMethod( PaymentMethods::DIRECT_DEBIT ); + + $payment_method_direct_debit->images = [ + 'woocommerce' => $image_service->get_path( 'methods/direct-debit/method-direct-debit-wc-51x32.svg' ), + ]; + + $this->payment_methods->add( $payment_method_direct_debit ); + + /* translators: %s: payment method */ + $description_template = \__( 'By using this payment method you authorize us via %s to debit payments from your bank account.', 'pronamic_ideal' ); + + // Direct debit (mandate via Bancontact). + $payment_method_direct_debit_bancontact = new PaymentMethod( PaymentMethods::DIRECT_DEBIT_BANCONTACT ); + + $payment_method_direct_debit_bancontact->descriptions = [ + 'customer' => \sprintf( $description_template, $payment_method_direct_debit_bancontact->name ), + ]; - if ( null === $origin_id && false !== $referer ) { - $referer_host = \wp_parse_url( $referer, \PHP_URL_HOST ); + $payment_method_direct_debit_bancontact->images = [ + 'woocommerce' => $image_service->get_path( 'methods/direct-debit-bancontact/method-direct-debit-bancontact-wc-107x32.svg' ), + ]; - if ( null === $referer_host ) { - $referer = \home_url( $referer ); - } + $this->payment_methods->add( $payment_method_direct_debit_bancontact ); - $post_id = \url_to_postid( $referer ); + // Direct debit (mandate via Bancontact). + $payment_method_direct_debit_ideal = new PaymentMethod( PaymentMethods::DIRECT_DEBIT_IDEAL ); - if ( $post_id > 0 ) { - $origin_id = $post_id; - } - } + $payment_method_direct_debit_ideal->descriptions = [ + 'customer' => \sprintf( $description_template, $payment_method_direct_debit_ideal->name ), + ]; - // Set origin ID. - $payment->set_origin_id( $origin_id ); - } + $payment_method_direct_debit_ideal->images = [ + 'woocommerce' => $image_service->get_path( 'methods/direct-debit-ideal/method-direct-debit-ideal-wc-107x32.svg' ), + ]; - // Customer. - $customer = $payment->get_customer(); + $this->payment_methods->add( $payment_method_direct_debit_ideal ); - if ( null === $customer ) { - $customer = new Customer(); + // Direct debit (mandate via SOFORT). + $payment_method_direct_debit_sofort = new PaymentMethod( PaymentMethods::DIRECT_DEBIT_SOFORT ); - $payment->set_customer( $customer ); - } + $payment_method_direct_debit_sofort->descriptions = [ + 'customer' => \sprintf( $description_template, $payment_method_direct_debit_sofort->name ), + ]; - CustomerHelper::complement_customer( $customer ); + $payment_method_direct_debit_sofort->images = [ + 'woocommerce' => $image_service->get_path( 'methods/direct-debit-sofort/method-direct-debit-sofort-wc-107x32.svg' ), + ]; - // Billing address. - $billing_address = $payment->get_billing_address(); + $this->payment_methods->add( $payment_method_direct_debit_sofort ); - if ( null !== $billing_address ) { - AddressHelper::complement_address( $billing_address ); - } + // EPS. + $payment_method_eps = new PaymentMethod( PaymentMethods::EPS ); - // Shipping address. - $shipping_address = $payment->get_shipping_address(); + $payment_method_eps->images = [ + 'woocommerce' => $image_service->get_path( 'methods/eps/method-eps-wc-51x32.svg' ), + ]; - if ( null !== $shipping_address ) { - AddressHelper::complement_address( $shipping_address ); - } + $this->payment_methods->add( $payment_method_eps ); - // Version. - if ( null === $payment->get_version() ) { - $payment->set_version( pronamic_pay_plugin()->get_version() ); - } + // Focum. + $payment_method_focum = new PaymentMethod( PaymentMethods::FOCUM ); - // Post data. - self::process_payment_post_data( $payment ); + $payment_method_eps->images = [ + 'woocommerce' => $image_service->get_path( 'methods/focum/method-focum-wc-51x32.svg' ), + ]; - // Gender. - if ( null !== $customer->get_gender() ) { - $payment->delete_meta( 'gender' ); - } + $this->payment_methods->add( $payment_method_focum ); - // Date of birth. - if ( null !== $customer->get_birth_date() ) { - $payment->delete_meta( 'birth_date' ); - } + // IDEAL. + $payment_method_ideal = new PaymentMethod( PaymentMethods::IDEAL ); - /** - * If an issuer has been specified and the payment - * method is unknown, we set the payment method to - * iDEAL. This may not be correct in all cases, - * but for now Pronamic Pay works this way. - * - * @link https://github.com/wp-pay-extensions/gravityforms/blob/2.4.0/src/Processor.php#L251-L256 - * @link https://github.com/wp-pay-extensions/contact-form-7/blob/1.0.0/src/Pronamic.php#L181-L187 - * @link https://github.com/wp-pay-extensions/formidable-forms/blob/2.1.0/src/Extension.php#L318-L329 - * @link https://github.com/wp-pay-extensions/ninjaforms/blob/1.2.0/src/PaymentGateway.php#L80-L83 - * @link https://github.com/wp-pay/core/blob/2.4.0/src/Forms/FormProcessor.php#L131-L134 - */ - $issuer = $payment->get_meta( 'issuer' ); + $payment_method_ideal->descriptions = [ + 'customer' => \__( 'With iDEAL you can easily pay online in the secure environment of your own bank.', 'pronamic_ideal' ), + ]; - $payment_method = $payment->get_payment_method(); + $payment_method_ideal->images = [ + 'woocommerce' => $image_service->get_path( 'methods/ideal/method-ideal-wc-51x32.svg' ), + ]; - if ( null !== $issuer && null === $payment_method ) { - $payment->set_payment_method( PaymentMethods::IDEAL ); - } + $this->payment_methods->add( $payment_method_ideal ); - // Consumer bank details. - $consumer_bank_details_name = $payment->get_meta( 'consumer_bank_details_name' ); - $consumer_bank_details_iban = $payment->get_meta( 'consumer_bank_details_iban' ); + // IDEAL QR. + $payment_method_ideal_qr = new PaymentMethod( PaymentMethods::IDEALQR ); - if ( null !== $consumer_bank_details_name || null !== $consumer_bank_details_iban ) { - $consumer_bank_details = $payment->get_consumer_bank_details(); + $payment_method_ideal_qr->images = [ + 'woocommerce' => $image_service->get_path( 'methods/ideal-qr/method-ideal-qr-wc-51x32.svg' ), + ]; - if ( null === $consumer_bank_details ) { - $consumer_bank_details = new BankAccountDetails(); - } + $this->payment_methods->add( $payment_method_ideal_qr ); - if ( null === $consumer_bank_details->get_name() ) { - $consumer_bank_details->set_name( $consumer_bank_details_name ); - } + // Giropay. + $payment_method_giropay = new PaymentMethod( PaymentMethods::GIROPAY ); - if ( null === $consumer_bank_details->get_iban() ) { - $consumer_bank_details->set_iban( $consumer_bank_details_iban ); - } + $payment_method_giropay->images = [ + 'woocommerce' => $image_service->get_path( 'methods/giropay/method-giropay-wc-51x32.svg' ), + ]; - $payment->set_consumer_bank_details( $consumer_bank_details ); - } + $this->payment_methods->add( $payment_method_giropay ); - // Payment lines payment. - $lines = $payment->get_lines(); + // Google Pay. + $payment_method_google_pay = new PaymentMethod( PaymentMethods::GOOGLE_PAY ); - if ( null !== $lines ) { - foreach ( $lines as $line ) { - $line->set_payment( $payment ); - } - } - } + $payment_method_google_pay->images = [ + 'woocommerce' => $image_service->get_path( 'methods/google-pay/method-google-pay-wc-51x32.svg' ), + ]; - /** - * Process payment input data. - * - * @param Payment $payment Payment. - * @return void - */ - private static function process_payment_post_data( Payment $payment ) { - $gateway = $payment->get_gateway(); + $this->payment_methods->add( $payment_method_google_pay ); - if ( null === $gateway ) { - return; - } + // KBC/CBC Payment Button. + $payment_method_kbc = new PaymentMethod( PaymentMethods::KBC ); - $payment_method = $payment->get_payment_method(); + $payment_method_kbc->images = [ + 'woocommerce' => $image_service->get_path( 'methods/kbc/method-kbc-wc-51x32.svg' ), + ]; - if ( null === $payment_method ) { - return; - } + $this->payment_methods->add( $payment_method_kbc ); - $payment_method = $gateway->get_payment_method( $payment_method ); + // Klarna. + $payment_method_klarna = new PaymentMethod( PaymentMethods::KLARNA ); - if ( null === $payment_method ) { - return; - } + $payment_method_klarna->images = [ + 'woocommerce' => $image_service->get_path( 'methods/klarna/method-klarna-wc-51x32.svg' ), + ]; - foreach ( $payment_method->get_fields() as $field ) { - $id = $field->get_id(); + $this->payment_methods->add( $payment_method_klarna ); - // phpcs:ignore WordPress.Security.NonceVerification.Missing - if ( \array_key_exists( $id, $_POST ) ) { - // phpcs:ignore WordPress.Security.NonceVerification.Missing - $value = \sanitize_text_field( \wp_unslash( $_POST[ $id ] ) ); + // Klarna Pay Later. + $payment_method_klarna_pay_later = new PaymentMethod( PaymentMethods::KLARNA_PAY_LATER ); - if ( '' !== $field->meta_key ) { - $payment->set_meta( $field->meta_key, $value ); - } - } - } - } + $payment_method_klarna_pay_later->images = [ + 'woocommerce' => $image_service->get_path( 'methods/klarna-pay-later/method-klarna-pay-later-wc-51x32.svg' ), + ]; - /** - * Get default gateway configuration ID. - * - * @return int|null - */ - private static function get_default_config_id() { - $value = (int) \get_option( 'pronamic_pay_config_id' ); + $payment_method_klarna_pay_later->descriptions = [ + 'customer' => \sprintf( $bnpl_disclaimer_template, $payment_method_klarna_pay_later->name ), + ]; - if ( 0 === $value ) { - return null; - } + $this->payment_methods->add( $payment_method_klarna_pay_later ); - if ( 'publish' !== \get_post_status( $value ) ) { - return null; - } + // Klarna Pay Now. + $payment_method_klarna_pay_now = new PaymentMethod( PaymentMethods::KLARNA_PAY_NOW ); - return $value; - } + $payment_method_klarna_pay_now->images = [ + 'woocommerce' => $image_service->get_path( 'methods/klarna-pay-now/method-klarna-pay-now-wc-51x32.svg' ), + ]; - /** - * Start payment. - * - * @param Payment $payment The payment to start at the specified gateway. - * @return Payment - * @throws \Exception Throws exception if gateway payment start fails. - */ - public static function start_payment( Payment $payment ) { - // Set default or filtered config ID. - $config_id = $payment->get_config_id(); + $this->payment_methods->add( $payment_method_klarna_pay_now ); - if ( null === $config_id ) { - $config_id = self::get_default_config_id(); - } + // Klarna Pay Over Time. + $payment_method_klarna_pay_over_time = new PaymentMethod( PaymentMethods::KLARNA_PAY_OVER_TIME ); - /** - * Filters the payment gateway configuration ID. - * - * @param null|int $config_id Gateway configuration ID. - * @param Payment $payment Payment. - */ - $config_id = \apply_filters( 'pronamic_payment_gateway_configuration_id', $config_id, $payment ); + $payment_method_klarna_pay_over_time->images = [ + 'woocommerce' => $image_service->get_path( 'methods/klarna-pay-over-time/method-klarna-pay-over-time-wc-51x32.svg' ), + ]; - if ( null !== $config_id ) { - $payment->set_config_id( $config_id ); - } + $this->payment_methods->add( $payment_method_klarna_pay_over_time ); - /** - * Merge tags. - * - * @link https://github.com/pronamic/wp-pronamic-pay/issues/358 - * @link https://github.com/pronamic/wp-pronamic-pay-woocommerce/issues/43 - */ - $payment->set_description( $payment->format_string( (string) $payment->get_description() ) ); + // Maestro. + $payment_method_maestro = new PaymentMethod( PaymentMethods::MAESTRO ); - // Save payment. - $payment->save(); + $payment_method_maestro->images = [ + 'woocommerce' => $image_service->get_path( 'methods/maestro/method-maestro-wc-51x32.svg' ), + ]; - // Periods. - $periods = $payment->get_periods(); + $this->payment_methods->add( $payment_method_maestro ); - if ( null !== $periods ) { - foreach ( $periods as $period ) { - $subscription = $period->get_phase()->get_subscription(); + // Mastercard. + $payment_method_mastercard = new PaymentMethod( PaymentMethods::MASTERCARD ); - $subscription->set_next_payment_date( \max( $subscription->get_next_payment_date(), $period->get_end_date() ) ); - } - } + $payment_method_mastercard->images = [ + 'woocommerce' => $image_service->get_path( 'methods/mastercard/method-mastercard-wc-51x32.svg' ), + ]; - // Subscriptions. - $subscriptions = $payment->get_subscriptions(); + $this->payment_methods->add( $payment_method_mastercard ); - foreach ( $subscriptions as $subscription ) { - $subscription->save(); - } + // MB WAY. + $payment_method_mb_way = new PaymentMethod( PaymentMethods::MB_WAY ); - // Gateway. - $gateway = $payment->get_gateway(); + $payment_method_mb_way->images = [ + 'woocommerce' => $image_service->get_path( 'methods/mb-way/method-mb-way-wc-51x32.svg' ), + ]; - if ( null === $gateway ) { - $payment->add_note( - \sprintf( - /* translators: %d: Gateway configuration ID */ - \__( 'Payment failed because gateway configuration with ID `%d` does not exist.', 'pronamic_ideal' ), - $config_id - ) - ); + $this->payment_methods->add( $payment_method_mb_way ); - $payment->set_status( PaymentStatus::FAILURE ); + // MyBank. + $payment_method_mybank = new PaymentMethod( PaymentMethods::MYBANK ); - $payment->save(); + $this->payment_methods->add( $payment_method_mybank ); - return $payment; - } + // Payconiq. + $payment_method_payconiq = new PaymentMethod( PaymentMethods::PAYCONIQ ); - // Mode. - $payment->set_mode( $gateway->get_mode() ); + $payment_method_payconiq->images = [ + 'woocommerce' => $image_service->get_path( 'methods/payconiq/method-payconiq-wc-51x32.svg' ), + ]; - // Subscriptions. - $subscriptions = $payment->get_subscriptions(); + $this->payment_methods->add( $payment_method_payconiq ); - // Start payment at the gateway. - try { - self::pronamic_service( $payment ); + // PayPal. + $payment_method_paypal = new PaymentMethod( PaymentMethods::PAYPAL ); - $gateway->start( $payment ); - } catch ( \Exception $exception ) { - $message = $exception->getMessage(); + $payment_method_paypal->images = [ + 'woocommerce' => $image_service->get_path( 'methods/paypal/method-paypal-wc-51x32.svg' ), + ]; - // Maybe include error code in message. - $code = $exception->getCode(); + $this->payment_methods->add( $payment_method_paypal ); - if ( $code > 0 ) { - $message = \sprintf( '%s: %s', $code, $message ); - } + // Przelewy24. + $payment_method_przelewy24 = new PaymentMethod( PaymentMethods::PRZELEWY24 ); - $payment->add_note( $message ); + $payment_method_przelewy24->images = [ + 'woocommerce' => $image_service->get_path( 'methods/przelewy24/method-przelewy24-wc-51x32.svg' ), + ]; - $payment->set_status( PaymentStatus::FAILURE ); + $this->payment_methods->add( $payment_method_przelewy24 ); - throw $exception; - } finally { - $payment->save(); - } + // Riverty. + $payment_method_riverty = new PaymentMethod( PaymentMethods::RIVERTY ); - // Schedule payment status check. - if ( $gateway->supports( 'payment_status_request' ) ) { - StatusChecker::schedule_event( $payment ); - } + $payment_method_riverty->descriptions = [ + 'default' => \__( 'Riverty (formerly AfterPay) is a payment service that allows customers to pay after receiving the product.', 'pronamic_ideal' ), + 'customer' => \sprintf( $bnpl_disclaimer_template, $payment_method_riverty->name ), + ]; - return $payment; - } + $payment_method_riverty->images = [ + 'woocommerce' => $image_service->get_path( 'methods/riverty/method-riverty-wc-51x32.svg' ), + ]; - /** - * The Pronamic Pay service forms an abstraction layer for the various supported - * WordPress plugins and Payment Service Providers (PSP. Optionally, a risk analysis - * can be performed before payment. - * - * @param Payment $payment Payment. - * @return void - */ - private static function pronamic_service( Payment $payment ) { - if ( null === self::$pronamic_service_url ) { - return; - } + $this->payment_methods->add( $payment_method_riverty ); - try { - $body = [ - 'license' => \get_option( 'pronamic_pay_license_key' ), - 'payment' => \wp_json_encode( $payment->get_json() ), - ]; + // Santander. + $payment_method_santander = new PaymentMethod( PaymentMethods::SANTANDER ); - $map = [ - 'query' => 'GET', - 'body' => 'POST', - 'server' => 'SERVER', - ]; + $payment_method_santander->images = [ + 'woocommerce' => $image_service->get_path( 'methods/santander/method-santander-wc-51x32.svg' ), + ]; - foreach ( $map as $parameter => $key ) { - $name = '_' . $key; + $this->payment_methods->add( $payment_method_santander ); - $body[ $parameter ] = $GLOBALS[ $name ]; - } + // SOFORT Banking. + $payment_method_sofort = new PaymentMethod( PaymentMethods::SOFORT ); - $response = Http::post( - self::$pronamic_service_url, - [ - 'body' => $body, - ] - ); + $payment_method_sofort->images = [ + 'woocommerce' => $image_service->get_path( 'methods/sofort/method-sofort-wc-51x32.svg' ), + ]; - $data = $response->json(); + $this->payment_methods->add( $payment_method_sofort ); - if ( ! \is_object( $data ) ) { - return; - } + // SprayPay. + $payment_method_spraypay = new PaymentMethod( PaymentMethods::SPRAYPAY ); - if ( \property_exists( $data, 'id' ) ) { - $payment->set_meta( 'pronamic_pay_service_id', $data->id ); - } + $payment_method_spraypay->images = [ + 'woocommerce' => $image_service->get_path( 'methods/spraypay/method-spraypay-wc-51x32.svg' ), + ]; - if ( \property_exists( $data, 'risk_score' ) ) { - $payment->set_meta( 'pronamic_pay_risk_score', $data->risk_score ); - } - } catch ( \Exception $e ) { - return; - } - } + $this->payment_methods->add( $payment_method_spraypay ); - /** - * Create refund. - * - * @param Refund $refund Refund. - * @return void - * @throws \Exception Throws exception on error. - */ - public static function create_refund( Refund $refund ) { - $payment = $refund->get_payment(); + // Swish. + $payment_method_swish = new PaymentMethod( PaymentMethods::SWISH ); - $gateway = $payment->get_gateway(); + $payment_method_swish->images = [ + 'woocommerce' => $image_service->get_path( 'methods/swish/method-swish-wc-51x32.svg' ), + ]; - if ( null === $gateway ) { - throw new \Exception( - \esc_html__( 'Unable to process refund as gateway could not be found.', 'pronamic_ideal' ) - ); - } + $this->payment_methods->add( $payment_method_swish ); - try { - $gateway->create_refund( $refund ); + // TWINT. + $payment_method_twint = new PaymentMethod( PaymentMethods::TWINT ); - $payment->refunds[] = $refund; + $payment_method_twint->images = [ + 'woocommerce' => $image_service->get_path( 'methods/twint/method-twint-wc-51x32.svg' ), + ]; - $refunded_amount = $payment->get_refunded_amount(); + $this->payment_methods->add( $payment_method_twint ); - $refunded_amount = $refunded_amount->add( $refund->get_amount() ); + // V PAY. + $payment_method_v_pay = new PaymentMethod( PaymentMethods::V_PAY ); - $payment->set_refunded_amount( $refunded_amount ); - } catch ( \Exception $exception ) { - $payment->add_note( $exception->getMessage() ); + $payment_method_v_pay->images = [ + 'woocommerce' => $image_service->get_path( 'methods/v-pay/method-v-pay-wc-51x32.svg' ), + ]; - throw $exception; - } finally { - $payment->save(); - } - } + $this->payment_methods->add( $payment_method_v_pay ); - /** - * Payment redirect URL. - * - * @param string $url Redirect URL. - * @param Payment $payment Payment. - * @return string - */ - public function payment_redirect_url( $url, Payment $payment ) { - $source = $payment->get_source(); + // Vipps. + $payment_method_vipps = new PaymentMethod( PaymentMethods::VIPPS ); - /** - * Filters the payment redirect URL by plugin integration source. - * - * @param string $url Redirect URL. - * @param Payment $payment Payment. - */ - $url = \apply_filters( 'pronamic_payment_redirect_url_' . $source, $url, $payment ); + $payment_method_vipps->images = [ + 'woocommerce' => $image_service->get_path( 'methods/vipps/method-vipps-wc-51x32.svg' ), + ]; - return $url; - } + $this->payment_methods->add( $payment_method_vipps ); - /** - * Is debug mode. - * - * @link https://github.com/easydigitaldownloads/easy-digital-downloads/blob/2.9.26/includes/misc-functions.php#L26-L38 - * @return bool True if debug mode is enabled, false otherwise. - */ - public function is_debug_mode() { - $value = \get_option( 'pronamic_pay_debug_mode', false ); + // Visa. + $payment_method_visa = new PaymentMethod( PaymentMethods::VISA ); - if ( defined( 'PRONAMIC_PAY_DEBUG' ) && PRONAMIC_PAY_DEBUG ) { - $value = true; - } + $payment_method_visa->images = [ + 'woocommerce' => $image_service->get_path( 'methods/visa/method-visa-wc-51x32.svg' ), + ]; - return (bool) $value; + $this->payment_methods->add( $payment_method_visa ); } }