2022-03-09 16:23:03 +01:00
< ? php
2022-04-14 17:17:56 +02:00
/**
* PUI integration .
*
* @ package WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice
*/
2022-03-09 16:23:03 +01:00
declare ( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice ;
2022-03-20 14:15:56 +01:00
use Psr\Log\LoggerInterface ;
2022-03-22 09:19:07 +01:00
use WC_Order ;
use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException ;
2022-04-14 10:13:41 +02:00
use WooCommerce\PayPalCommerce\Onboarding\Environment ;
2022-04-11 12:50:05 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings ;
2022-04-14 17:17:56 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException ;
2022-04-19 11:31:47 +02:00
use WP_Error ;
2022-03-20 14:15:56 +01:00
2022-04-14 17:17:56 +02:00
/**
* Class PayUponInvoice .
*/
2022-03-09 16:23:03 +01:00
class PayUponInvoice {
/**
2022-04-14 17:17:56 +02:00
* The module URL .
*
2022-03-09 16:23:03 +01:00
* @ var string
*/
protected $module_url ;
/**
2022-04-14 17:17:56 +02:00
* The FraudNet entity .
*
2022-03-09 16:23:03 +01:00
* @ var FraudNet
*/
protected $fraud_net ;
2022-03-20 14:15:56 +01:00
/**
2022-04-14 17:17:56 +02:00
* The order endpoint .
*
2022-04-22 11:49:30 +02:00
* @ var PayUponInvoiceOrderEndpoint
2022-03-20 14:15:56 +01:00
*/
protected $order_endpoint ;
/**
2022-04-14 17:17:56 +02:00
* The logger .
*
2022-03-20 14:15:56 +01:00
* @ var LoggerInterface
*/
protected $logger ;
2022-04-11 12:50:05 +02:00
/**
2022-04-14 17:17:56 +02:00
* The settings .
*
2022-04-11 12:50:05 +02:00
* @ var Settings
*/
protected $settings ;
2022-04-14 10:13:41 +02:00
/**
2022-04-14 17:17:56 +02:00
* The environment .
*
2022-04-14 10:13:41 +02:00
* @ var Environment
*/
protected $environment ;
2022-04-21 16:51:17 +02:00
/**
* The asset version .
*
* @ var string
*/
protected $asset_version ;
2022-04-14 17:17:56 +02:00
/**
* PayUponInvoice constructor .
*
* @ param string $module_url The module URL .
* @ param FraudNet $fraud_net The FraudNet entity .
2022-04-22 11:49:30 +02:00
* @ param PayUponInvoiceOrderEndpoint $order_endpoint The order endpoint .
2022-04-14 17:17:56 +02:00
* @ param LoggerInterface $logger The logger .
* @ param Settings $settings The settings .
* @ param Environment $environment The environment .
2022-04-21 16:51:17 +02:00
* @ param string $asset_version The asset version .
2022-04-14 17:17:56 +02:00
*/
2022-03-20 14:15:56 +01:00
public function __construct (
string $module_url ,
FraudNet $fraud_net ,
2022-04-22 11:49:30 +02:00
PayUponInvoiceOrderEndpoint $order_endpoint ,
2022-04-11 12:50:05 +02:00
LoggerInterface $logger ,
2022-04-14 10:13:41 +02:00
Settings $settings ,
2022-04-21 16:51:17 +02:00
Environment $environment ,
string $asset_version
2022-03-20 14:15:56 +01:00
) {
2022-03-25 16:37:58 +01:00
$this -> module_url = $module_url ;
$this -> fraud_net = $fraud_net ;
2022-03-20 14:15:56 +01:00
$this -> order_endpoint = $order_endpoint ;
2022-03-25 16:37:58 +01:00
$this -> logger = $logger ;
2022-04-14 17:17:56 +02:00
$this -> settings = $settings ;
$this -> environment = $environment ;
2022-04-21 16:51:17 +02:00
$this -> asset_version = $asset_version ;
2022-03-09 16:23:03 +01:00
}
2022-04-14 17:17:56 +02:00
/**
* Initializes PUI integration .
*
* @ throws NotFoundException When setting is not found .
*/
2022-04-19 11:31:47 +02:00
public function init () : void {
2022-03-25 16:37:58 +01:00
add_filter (
'ppcp_partner_referrals_data' ,
2022-04-19 11:31:47 +02:00
function ( array $data ) : array {
2022-04-14 17:17:56 +02:00
if ( $this -> settings -> has ( 'ppcp-onboarding-pui' ) && $this -> settings -> get ( 'ppcp-onboarding-pui' ) !== '1' ) {
2022-04-11 12:50:05 +02:00
return $data ;
}
2022-04-14 17:17:56 +02:00
if ( in_array ( 'PPCP' , $data [ 'products' ], true ) ) {
2022-03-25 16:37:58 +01:00
$data [ 'products' ][] = 'PAYMENT_METHODS' ;
$data [ 'capabilities' ][] = 'PAY_UPON_INVOICE' ;
}
2022-04-11 12:50:05 +02:00
2022-03-25 16:37:58 +01:00
return $data ;
}
);
2022-03-11 15:21:48 +01:00
2022-03-09 16:23:03 +01:00
add_action (
'wp_footer' ,
array ( $this , 'add_parameter_block' )
);
add_action (
'wp_enqueue_scripts' ,
array ( $this , 'register_assets' )
);
2022-03-10 14:41:07 +01:00
2022-03-25 16:37:58 +01:00
add_action (
'ppcp_payment_capture_completed_webhook_handler' ,
function ( WC_Order $wc_order , string $order_id ) {
try {
$payment_instructions = $this -> order_endpoint -> order_payment_instructions ( $order_id );
$wc_order -> update_meta_data ( 'ppcp_ratepay_payment_instructions_payment_reference' , $payment_instructions );
$this -> logger -> info ( " Ratepay payment instructions added to order # { $wc_order -> get_id () } . " );
} catch ( RuntimeException $exception ) {
$this -> logger -> error ( $exception -> getMessage () );
}
},
10 ,
2
);
2022-03-24 16:27:03 +01:00
2022-04-08 10:50:59 +02:00
add_action (
'woocommerce_email_before_order_table' ,
2022-04-19 11:31:47 +02:00
function ( WC_Order $order , bool $sent_to_admin ) {
2022-04-08 10:50:59 +02:00
if ( ! $sent_to_admin && PayUponInvoiceGateway :: ID === $order -> get_payment_method () && $order -> has_status ( 'processing' ) ) {
$this -> logger -> info ( " Adding Ratepay payment instructions to email for order # { $order -> get_id () } . " );
$instructions = $order -> get_meta ( 'ppcp_ratepay_payment_instructions_payment_reference' );
$gateway_settings = get_option ( 'woocommerce_ppcp-pay-upon-invoice-gateway_settings' );
$merchant_name = $gateway_settings [ 'brand_name' ] ? ? '' ;
2022-04-19 11:31:47 +02:00
$order_date = $order -> get_date_created ();
if ( null === $order_date ) {
$this -> logger -> error ( 'Could not get WC order date for Ratepay payment instructions.' );
return ;
}
2022-04-08 10:50:59 +02:00
$order_purchase_date = $order_date -> date ( 'd-m-Y' );
$order_time = $order_date -> date ( 'H:i:s' );
$order_date = $order_date -> date ( 'd-m-Y H:i:s' );
2022-04-19 11:31:47 +02:00
$thirty_days_date = strtotime ( $order_date . ' +30 days' );
if ( false === $thirty_days_date ) {
$this -> logger -> error ( 'Could not create +30 days date from WC order date.' );
return ;
}
$order_date_30d = gmdate ( 'd-m-Y' , $thirty_days_date );
2022-04-08 10:50:59 +02:00
$payment_reference = $instructions [ 0 ] ? ? '' ;
$bic = $instructions [ 1 ] -> bic ? ? '' ;
$bank_name = $instructions [ 1 ] -> bank_name ? ? '' ;
$iban = $instructions [ 1 ] -> iban ? ? '' ;
$account_holder_name = $instructions [ 1 ] -> account_holder_name ? ? '' ;
2022-04-19 11:54:49 +02:00
echo wp_kses_post ( " <p>Für Ihre Bestellung # { $order -> get_id () } ( { $order_purchase_date } $order_time ) bei { $merchant_name } haben Sie die Zahlung mittels “Rechnungskauf mit Ratepay“ gewählt. " );
2022-04-08 10:50:59 +02:00
echo '<br>Bitte benutzen Sie die folgenden Informationen für Ihre Überweisung:</br>' ;
2022-04-19 11:54:49 +02:00
echo wp_kses_post ( " <p>Bitte überweisen Sie den Betrag in Höhe von { $order -> get_currency () } { $order -> get_total () } bis zum { $order_date_30d } auf das unten angegebene Konto. Wichtig: Bitte geben Sie unbedingt als Verwendungszweck { $payment_reference } an, sonst kann die Zahlung nicht zugeordnet werden.</p> " );
2022-04-08 10:50:59 +02:00
echo '<ul>' ;
2022-04-19 11:54:49 +02:00
echo wp_kses_post ( " <li>Empfänger: { $account_holder_name } </li> " );
echo wp_kses_post ( " <li>IBAN: { $iban } </li> " );
echo wp_kses_post ( " <li>BIC: { $bic } </li> " );
echo wp_kses_post ( " <li>Name der Bank: { $bank_name } </li> " );
echo wp_kses_post ( " <li>Verwendungszweck: { $payment_reference } </li> " );
2022-04-08 10:50:59 +02:00
echo '</ul>' ;
2022-04-19 11:54:49 +02:00
echo wp_kses_post ( " <p> { $merchant_name } hat die Forderung gegen Sie an die PayPal (Europe) S.à r.l. et Cie, S.C.A. abgetreten, die wiederum die Forderung an Ratepay GmbH abgetreten hat. Zahlungen mit schuldbefreiender Wirkung können nur an die Ratepay GmbH geleistet werden.</p> " );
2022-04-08 10:50:59 +02:00
echo '<p>Mit freundlichen Grüßen' ;
echo '<br>' ;
2022-04-19 11:54:49 +02:00
echo wp_kses_post ( " { $merchant_name } </p> " );
2022-04-08 10:50:59 +02:00
}
},
10 ,
3
);
2022-03-31 12:47:05 +02:00
2022-03-25 16:37:58 +01:00
add_filter (
'woocommerce_gateway_description' ,
2022-04-19 11:31:47 +02:00
function ( string $description , string $id ) : string {
2022-03-25 16:37:58 +01:00
if ( PayUponInvoiceGateway :: ID === $id ) {
2022-03-28 10:42:11 +02:00
ob_start ();
echo '<div style="padding: 20px 0;">' ;
woocommerce_form_field (
'billing_birth_date' ,
array (
2022-03-30 09:54:03 +02:00
'type' => 'date' ,
'label' => __ ( 'Birth date' , 'woocommerce-paypal-payments' ),
'class' => array ( 'form-row-wide' ),
2022-03-28 10:42:11 +02:00
'required' => true ,
2022-03-30 09:54:03 +02:00
'clear' => true ,
2022-03-29 11:15:36 +02:00
)
);
2022-03-28 10:42:11 +02:00
echo '</div><div>' ;
2022-04-14 17:17:56 +02:00
$site_country_code = explode ( '-' , get_bloginfo ( 'language' ) )[ 0 ] ? ? '' ;
2022-04-19 12:37:25 +02:00
// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
$button_text = apply_filters ( 'woocommerce_order_button_text' , __ ( 'Place order' , 'woocommerce' ) );
2022-04-19 11:54:49 +02:00
if ( 'de' === $site_country_code ) {
2022-04-19 12:37:25 +02:00
echo wp_kses_post (
'Mit Klicken auf ' . $button_text . ' akzeptieren Sie die <a href="https://www.ratepay.com/legal-payment-terms" target="_blank">Ratepay Zahlungsbedingungen</a> und erklären sich mit der Durchführung einer <a href="https://www.ratepay.com/legal-payment-dataprivacy" target="_blank">Risikoprüfung durch Ratepay</a>, unseren Partner, einverstanden. Sie akzeptieren auch PayPals <a href="https://www.paypal.com/de/webapps/mpp/ua/privacy-full?locale.x=de_DE&_ga=1.228729434.718583817.1563460395" target="_blank">Datenschutzerklärung</a>. Falls Ihre Transaktion per Kauf auf Rechnung erfolgreich abgewickelt werden kann, wird der Kaufpreis an Ratepay abgetreten und Sie dürfen nur an Ratepay überweisen, nicht an den Händler.'
);
2022-04-08 12:03:24 +02:00
} else {
2022-04-19 12:37:25 +02:00
echo wp_kses_post (
'By clicking on ' . $button_text . ', you agree to the <a href="https://www.ratepay.com/legal-payment-terms" target="_blank">terms of payment</a> and <a href="https://www.ratepay.com/legal-payment-dataprivacy">performance of a risk check</a> from the payment partner, Ratepay. You also agree to PayPal’ s <a href="https://www.paypal.com/de/webapps/mpp/ua/privacy-full?locale.x=eng_DE&_ga=1.267010504.718583817.1563460395">privacy statement</a>. If your request to purchase upon invoice is accepted, the purchase price claim will be assigned to Ratepay, and you may only pay Ratepay, not the merchant.'
);
2022-04-08 12:03:24 +02:00
}
2022-03-28 10:42:11 +02:00
echo '</div>' ;
2022-04-19 11:31:47 +02:00
$description .= ob_get_clean () ? : '' ;
2022-03-25 16:37:58 +01:00
}
2022-03-28 10:42:11 +02:00
2022-03-25 16:37:58 +01:00
return $description ;
},
10 ,
2
);
add_action (
2022-03-30 09:54:03 +02:00
'woocommerce_after_checkout_validation' ,
2022-04-19 11:31:47 +02:00
function ( array $fields , WP_Error $errors ) {
2022-04-19 11:54:49 +02:00
if ( 'DE' !== $fields [ 'billing_country' ] ) {
2022-03-30 09:54:03 +02:00
$errors -> add ( 'validation' , __ ( 'Billing country not available.' , 'woocommerce-paypal-payments' ) );
2022-03-25 16:37:58 +01:00
}
},
10 ,
2022-03-30 09:54:03 +02:00
2
2022-03-25 16:37:58 +01:00
);
2022-04-08 10:50:59 +02:00
add_filter (
'woocommerce_available_payment_gateways' ,
2022-04-19 11:31:47 +02:00
function ( array $methods ) : array {
2022-04-08 10:50:59 +02:00
$billing_country = filter_input ( INPUT_POST , 'country' , FILTER_SANITIZE_STRING ) ? ? null ;
2022-04-19 11:54:49 +02:00
if ( $billing_country && 'DE' !== $billing_country ) {
2022-04-08 10:50:59 +02:00
unset ( $methods [ PayUponInvoiceGateway :: ID ] );
}
return $methods ;
}
);
2022-03-09 16:23:03 +01:00
}
2022-04-14 17:17:56 +02:00
/**
* Set configuration JSON for FraudNet integration .
*/
2022-04-19 11:31:47 +02:00
public function add_parameter_block () : void {
2022-04-14 10:13:41 +02:00
?>
2022-04-19 14:54:07 +02:00
< script type = " application/json " fncls = " fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99 " >< ? php echo wc_esc_json ( $this -> fraudnet_configuration (), true ); ?> </script>
2022-04-19 11:54:49 +02:00
< ? php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript?>
2022-03-09 16:23:03 +01:00
< script type = " text/javascript " src = " https://c.paypal.com/da/r/fb.js " ></ script >
< ? php
}
2022-04-14 17:17:56 +02:00
/**
* Registers PUI assets .
*/
2022-04-19 11:31:47 +02:00
public function register_assets () : void {
2022-03-09 16:23:03 +01:00
wp_enqueue_script (
'ppcp-pay-upon-invoice' ,
trailingslashit ( $this -> module_url ) . 'assets/js/pay-upon-invoice.js' ,
array (),
2022-04-21 16:51:17 +02:00
$this -> asset_version
2022-03-09 16:23:03 +01:00
);
}
2022-04-19 14:54:07 +02:00
/**
* Returns a configuration JSON string .
*
* @ return string
*/
private function fraudnet_configuration () : string {
$config = array (
'sandbox' => true ,
'f' => $this -> fraud_net -> session_id (),
's' => $this -> fraud_net -> source_website_id (),
);
if ( ! $this -> environment -> current_environment_is ( Environment :: SANDBOX ) ) {
unset ( $config [ 'sandbox' ] );
}
$encoded = wp_json_encode ( $config );
if ( false === $encoded ) {
return '' ;
}
return $encoded ;
}
2022-03-09 16:23:03 +01:00
}