Get fraudNet session id from WC session

This commit is contained in:
dinamiko 2022-03-09 16:23:03 +01:00
parent f8d25a9357
commit 753b8bae3e
7 changed files with 155 additions and 46 deletions

View file

@ -1,8 +1,4 @@
document.addEventListener('DOMContentLoaded', () => {
const script = document.createElement('script');
script.setAttribute('src', 'https://c.paypal.com/da/r/fb.js');
document.body.append(script);
jQuery(document.body).on('updated_checkout payment_method_selected', () => {
jQuery('#ppcp-pui-legal-text').hide();
if(jQuery('input[name="payment_method"]:checked').val() === 'ppcp-pay-upon-invoice-gateway') {

View file

@ -29,7 +29,10 @@ use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNet;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNetSessionId;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\OrderEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoice;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider;
use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus;
@ -2125,6 +2128,7 @@ return array(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'api.factory.order' ),
$container->get('wcgateway.pay-upon-invoice-fraudnet'),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
@ -2135,4 +2139,20 @@ return array(
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'wcgateway.pay-upon-invoice-fraudnet-session-id' => static function (ContainerInterface $container): FraudNetSessionId {
return new FraudNetSessionId();
},
'wcgateway.pay-upon-invoice-fraudnet' => static function (ContainerInterface $container): FraudNet {
$session_id = $container->get('wcgateway.pay-upon-invoice-fraudnet-session-id');
return new FraudNet(
(string)$session_id(),
'bar'
);
},
'wcgateway.pay-upon-invoice' => static function (ContainerInterface $container): PayUponInvoice {
return new PayUponInvoice(
$container->get('wcgateway.url'),
$container->get('wcgateway.pay-upon-invoice-fraudnet')
);
}
);

View file

@ -0,0 +1,38 @@
<?php
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice;
class FraudNet
{
/**
* @var string
*/
protected $session_id;
/**
* @var string
*/
protected $source_website_id;
public function __construct(string $session_id, string $source_website_id)
{
$this->session_id = $session_id;
$this->source_website_id = $source_website_id;
}
/**
* @return string
*/
public function sessionId(): string
{
return $this->session_id;
}
/**
* @return string
*/
public function sourceWebsiteId(): string
{
return $this->source_website_id;
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice;
class FraudNetSessionId
{
public function __invoke()
{
if(WC()->session->get( 'ppcp_fraudnet_session_id' )) {
return WC()->session->get( 'ppcp_fraudnet_session_id' );
}
$session_id = bin2hex(random_bytes(16));
WC()->session->set( 'ppcp_fraudnet_session_id', $session_id);
return bin2hex($session_id);
}
}

View file

@ -36,12 +36,23 @@ class OrderEndpoint {
* @var LoggerInterface
*/
protected $logger;
/**
* @var FraudNet
*/
protected $fraudNet;
public function __construct( string $host, Bearer $bearer, OrderFactory $order_factory, LoggerInterface $logger ) {
public function __construct(
string $host,
Bearer $bearer,
OrderFactory $order_factory,
FraudNet $fraudNet,
LoggerInterface $logger
) {
$this->host = $host;
$this->bearer = $bearer;
$this->order_factory = $order_factory;
$this->logger = $logger;
$this->fraudNet = $fraudNet;
}
/**
@ -98,7 +109,7 @@ class OrderEndpoint {
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'Prefer' => 'return=representation',
'PayPal-Client-Metadata-Id' => 'd4e0d7b9-4f75-43f9-9437-d8a57c901585',
'PayPal-Client-Metadata-Id' => $this->fraudNet->sessionId(),
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
),
'body' => wp_json_encode( $data ),

View file

@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice;
class PayUponInvoice {
/**
* @var string
*/
protected $module_url;
/**
* @var FraudNet
*/
protected $fraud_net;
public function __construct( string $module_url, FraudNet $fraud_net ) {
$this->module_url = $module_url;
$this->fraud_net = $fraud_net;
}
public function init() {
add_action(
'wp_footer',
array( $this, 'add_parameter_block' )
);
add_action(
'woocommerce_review_order_after_submit',
array( $this, 'add_legal_text' )
);
add_action(
'wp_enqueue_scripts',
array( $this, 'register_assets' )
);
}
public function add_parameter_block() { ?>
<script type="application/json" fncls="fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99">{"f":"<?php echo $this->fraud_net->sessionId(); ?>","s":"<?php echo $this->fraud_net->sourceWebsiteId(); ?>"}</script>
<script type="text/javascript" src="https://c.paypal.com/da/r/fb.js"></script>
<?php
}
public function add_legal_text() {
$gateway_settings = get_option( 'woocommerce_ppcp-pay-upon-invoice-gateway_settings' );
?>
<p id="ppcp-pui-legal-text"
style="display:none;"><?php echo wp_kses_post( $gateway_settings['legal_text'] ?? '' ); ?></p>
<?php
}
public function register_assets() {
wp_enqueue_script(
'ppcp-pay-upon-invoice',
trailingslashit( $this->module_url ) . 'assets/js/pay-upon-invoice.js',
array(),
1
);
}
}

View file

@ -185,46 +185,9 @@ class WCGatewayModule implements ModuleInterface {
add_action(
'init',
function() use ( $c ) {
if ( 'DE' === $c->get( 'api.shop.country' ) ) { // TODO && is_checkout() does not work, we are on admin-ajax.php
add_action(
'wp_footer',
function () {
?>
<script type="application/json" fncls="fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99">
{
"f":"d4e0d7b9-4f75-43f9-9437-d8a57c901585",
"s":"flowid_provided_to_you"
}
</script>
<?php
}
);
add_action(
'wp_enqueue_scripts',
function () use ( $c ) {
$gateway_module_url = $c->get( 'wcgateway.url' );
wp_enqueue_script(
'ppcp-pay-upon-invoice',
trailingslashit( $gateway_module_url ) . 'assets/js/pay-upon-invoice.js',
array(),
1
);
}
);
add_action(
'woocommerce_review_order_after_submit',
function () {
$gateway_settings = get_option( 'woocommerce_ppcp-pay-upon-invoice-gateway_settings' );
?>
<p id="ppcp-pui-legal-text" style="display:none;"><?php echo wp_kses_post( $gateway_settings['legal_text'] ?? '' ); ?></p>
<?php
}
);
function () use ($c) {
if ( 'DE' === $c->get( 'api.shop.country' ) ) {
($c->get('wcgateway.pay-upon-invoice'))->init();
}
}
);