mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Inject json config and retrigger fraudnet
This commit is contained in:
parent
56f12ed853
commit
b5f63b9f47
4 changed files with 41 additions and 64 deletions
|
@ -1,34 +1,43 @@
|
|||
window.addEventListener('load', function() {
|
||||
|
||||
const getSessionIdFromJson = () => {
|
||||
const form = document.querySelector('form.checkout');
|
||||
if(!form) {
|
||||
return;
|
||||
function _loadBeaconJS(options) {
|
||||
var script = document.createElement('script');
|
||||
script.src = options.fnUrl;
|
||||
document.body.appendChild(script);
|
||||
}
|
||||
|
||||
function _injectConfig() {
|
||||
var script = document.querySelector("[fncls='fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99']");
|
||||
if (script) {
|
||||
if (script.parentNode) {
|
||||
script.parentNode.removeChild(script);
|
||||
}
|
||||
}
|
||||
|
||||
const fncls = document.querySelector("[fncls='fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99']");
|
||||
if(!fncls) {
|
||||
return;
|
||||
}
|
||||
const fncls_params = JSON.parse(fncls.textContent);
|
||||
script = document.createElement('script');
|
||||
script.id = 'fconfig';
|
||||
script.type = 'application/json';
|
||||
script.setAttribute('fncls', 'fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99');
|
||||
|
||||
if(document.querySelector("[name='fraudnet-session-id']") !== null) {
|
||||
document.querySelector("[name='fraudnet-session-id']").remove();
|
||||
}
|
||||
var configuration = {
|
||||
'f': FraudNetConfig.f,
|
||||
's': FraudNetConfig.s
|
||||
};
|
||||
|
||||
const fraudnetSessionId = document.createElement('input');
|
||||
fraudnetSessionId.setAttribute('type', 'hidden');
|
||||
fraudnetSessionId.setAttribute('name', 'fraudnet-session-id');
|
||||
fraudnetSessionId.setAttribute('value', fncls_params.f);
|
||||
script.text = JSON.stringify(configuration);
|
||||
document.body.appendChild(script);
|
||||
|
||||
form.appendChild(fraudnetSessionId);
|
||||
console.log(fncls_params)
|
||||
_loadBeaconJS({fnUrl: "https://c.paypal.com/da/r/fb.js"})
|
||||
}
|
||||
|
||||
document.addEventListener('hosted_fields_loaded', (event) => {
|
||||
getSessionIdFromJson();
|
||||
if (PAYPAL.asyncData && typeof PAYPAL.asyncData.initAndCollect === 'function') {
|
||||
PAYPAL.asyncData.initAndCollect()
|
||||
}
|
||||
|
||||
_injectConfig();
|
||||
});
|
||||
|
||||
getSessionIdFromJson();
|
||||
_injectConfig();
|
||||
})
|
||||
|
||||
|
|
|
@ -123,11 +123,6 @@ class PayUponInvoice {
|
|||
}
|
||||
);
|
||||
|
||||
add_action(
|
||||
'wp_footer',
|
||||
array( $this, 'add_parameter_block' )
|
||||
);
|
||||
|
||||
add_action(
|
||||
'wp_enqueue_scripts',
|
||||
array( $this, 'register_assets' )
|
||||
|
@ -273,17 +268,6 @@ class PayUponInvoice {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set configuration JSON for FraudNet integration.
|
||||
*/
|
||||
public function add_parameter_block(): void {
|
||||
?>
|
||||
<script type="application/json" fncls="fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99"><?php echo wc_esc_json( $this->fraudnet_configuration(), true ); ?></script>
|
||||
<?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript?>
|
||||
<script type="text/javascript" src="https://c.paypal.com/da/r/fb.js"></script>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers PUI assets.
|
||||
*/
|
||||
|
@ -294,29 +278,16 @@ class PayUponInvoice {
|
|||
array(),
|
||||
$this->asset_version
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(),
|
||||
wp_localize_script(
|
||||
'ppcp-pay-upon-invoice',
|
||||
'FraudNetConfig',
|
||||
array(
|
||||
'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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -177,9 +177,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
|
|||
$payment_source = $this->payment_source_factory->from_wc_order( $wc_order, $birth_date );
|
||||
|
||||
try {
|
||||
$fraudnet_session_id = filter_input( INPUT_POST, 'fraudnet-session-id', FILTER_SANITIZE_STRING ) ?? '';
|
||||
|
||||
$order = $this->order_endpoint->create( array( $purchase_unit ), $payment_source, $fraudnet_session_id );
|
||||
$order = $this->order_endpoint->create( array( $purchase_unit ), $payment_source );
|
||||
$this->add_paypal_meta( $wc_order, $order, $this->environment );
|
||||
|
||||
WC()->cart->empty_cart();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue