Refactor to create PayPal order first

This commit is contained in:
dinamiko 2022-07-13 16:11:33 +02:00
parent d248a278be
commit 09ef106a80
6 changed files with 204 additions and 51 deletions

View file

@ -1,4 +1,33 @@
window.addEventListener('load', function() {
const oxxoButton = document.getElementById('ppcp-oxxo');
oxxoButton?.addEventListener('click', (event) => {
event.preventDefault();
fetch(OXXOConfig.oxxo_endpoint, {
method: 'POST',
body: JSON.stringify({
nonce: OXXOConfig.oxxo_nonce,
})
}).then((res)=>{
return res.json();
}).then((data)=>{
if (!data.success) {
alert('Could not update signup buttons: ' + JSON.stringify(data));
return;
}
window.open(
data.data.payer_action,
'_blank',
'popup'
);
document.querySelector('#place_order').click()
});
});
/*
const oxxoButton = document.getElementById('ppcp-oxxo-payer-action');
if(oxxoButton) {
oxxoButton.addEventListener('click', (event) => {
@ -12,4 +41,6 @@ window.addEventListener('load', function() {
window.open(oxxoButton.href);
}
*/
});

View file

@ -31,6 +31,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXO;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXOEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXOGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNet;
@ -2245,6 +2246,15 @@ return array(
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'wcgateway.endpoint.oxxo' => static function (ContainerInterface $container): OXXOEndpoint {
return new OXXOEndpoint(
$container->get( 'button.request-data' ),
$container->get( 'api.endpoint.order' ),
$container->get( 'api.factory.purchase-unit' ),
$container->get( 'api.factory.shipping-preference' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'wcgateway.logging.is-enabled' => function ( ContainerInterface $container ) : bool {
$settings = $container->get( 'wcgateway.settings' );

View file

@ -93,6 +93,11 @@ class OXXO {
'wp_enqueue_scripts',
array( $this, 'register_assets' )
);
add_action('woocommerce_review_order_after_payment', function () {
echo '<button class="button" id="ppcp-oxxo">Pago en OXXO</button>';
});
}
/**
@ -123,14 +128,23 @@ class OXXO {
public function register_assets(): void {
$gateway_settings = get_option( 'woocommerce_ppcp-oxxo-gateway_settings' );
$gateway_enabled = $gateway_settings['enabled'] ?? '';
if ( $gateway_enabled === 'yes' && is_checkout() && ! empty( is_wc_endpoint_url( 'order-received' ) ) ) {
if ( $gateway_enabled === 'yes' && is_checkout() ) { // && ! empty( is_wc_endpoint_url( 'order-received' ) )
wp_enqueue_script(
'ppcp-pay-upon-invoice',
'ppcp-oxxo',
trailingslashit( $this->module_url ) . 'assets/js/oxxo.js',
array(),
$this->asset_version,
true
);
}
wp_localize_script(
'ppcp-oxxo',
'OXXOConfig',
array(
'oxxo_endpoint' => \WC_AJAX::get_endpoint( 'ppc-oxxo' ),
'oxxo_nonce' => wp_create_nonce( 'ppc-oxxo' ),
)
);
}
}

View file

@ -0,0 +1,135 @@
<?php
/**
* Handles the onboard with Pay upon Invoice setting.
*
* @package WooCommerce\PayPalCommerce\Onboarding\Endpoint
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
class OXXOEndpoint implements EndpointInterface
{
/**
* The request data
*
* @var RequestData
*/
protected $request_data;
/**
* @var PurchaseUnitFactory
*/
protected $purchase_unit_factory;
/**
* @var ShippingPreferenceFactory
*/
protected $shipping_preference_factory;
/**
* @var OrderEndpoint
*/
protected $order_endpoint;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* OXXOEndpoint constructor
*
* @param RequestData $request_data The request data.
*/
public function __construct(
RequestData $request_data,
OrderEndpoint $order_endpoint,
PurchaseUnitFactory $purchase_unit_factory,
ShippingPreferenceFactory $shipping_preference_factory,
LoggerInterface $logger
)
{
$this->request_data = $request_data;
$this->purchase_unit_factory = $purchase_unit_factory;
$this->shipping_preference_factory = $shipping_preference_factory;
$this->order_endpoint = $order_endpoint;
$this->logger = $logger;
}
public static function nonce(): string
{
return 'ppc-oxxo';
}
public function handle_request(): bool
{
$data = $this->request_data->read_request( $this->nonce() );
$purchase_unit = $this->purchase_unit_factory->from_wc_cart();
$payer_action = '';
try {
$shipping_preference = $this->shipping_preference_factory->from_state(
$purchase_unit,
'checkout'
);
$order = $this->order_endpoint->create(array($purchase_unit), $shipping_preference);
$payment_source = array(
'oxxo' => array(
'name' => 'John Doe',
'email' => 'foo@bar.com',
'country_code' => 'MX',
),
);
$payment_method = $this->order_endpoint->confirm_payment_source( $order->id(), $payment_source );
foreach ( $payment_method->links as $link ) {
if ( $link->rel === 'payer-action' ) {
$payer_action = $link->href;
}
}
} catch ( RuntimeException $exception ) {
$error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) {
$details = '';
foreach ( $exception->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
$error = $details;
}
$this->logger->error( $error );
wc_add_notice( $error, 'error' );
}
WC()->session->set( 'ppcp_payer_action', $payer_action );
wp_send_json_success(
array('payer_action' => $payer_action,)
);
return true;
}
}

View file

@ -130,56 +130,11 @@ class OXXOGateway extends WC_Payment_Gateway {
public function process_payment( $order_id ) {
$wc_order = wc_get_order( $order_id );
$wc_order->update_status( 'on-hold', __( 'Awaiting OXXO payment.', 'woocommerce-paypal-payments' ) );
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
try {
$shipping_preference = $this->shipping_preference_factory->from_state(
$purchase_unit,
'checkout'
);
$order = $this->order_endpoint->create( array( $purchase_unit ), $shipping_preference );
$payment_source = array(
'oxxo' => array(
'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(),
'email' => $wc_order->get_billing_email(),
'country_code' => $wc_order->get_billing_country(),
),
);
$payment_method = $this->order_endpoint->confirm_payment_source( $order->id(), $payment_source );
foreach ( $payment_method->links as $link ) {
if ( $link->rel === 'payer-action' ) {
$wc_order->add_meta_data( 'ppcp_oxxo_payer_action', $link->href );
$wc_order->save_meta_data();
}
}
} catch ( RuntimeException $exception ) {
$error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) {
$details = '';
foreach ( $exception->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
$error = $details;
}
$this->logger->error( $error );
wc_add_notice( $error, 'error' );
$wc_order->update_status(
'failed',
$error
);
return array(
'result' => 'failure',
'redirect' => wc_get_checkout_url(),
);
$payer_action = WC()->session->get( 'ppcp_payer_action' );
if($payer_action) {
$wc_order->add_meta_data( 'ppcp_oxxo_payer_action', $payer_action );
$wc_order->save_meta_data();
}
WC()->cart->empty_cart();

View file

@ -262,6 +262,14 @@ class WCGatewayModule implements ModuleInterface {
10,
2
);
add_action(
'wc_ajax_ppc-oxxo',
static function () use ( $c ) {
$endpoint = $c->get( 'wcgateway.endpoint.oxxo' );
$endpoint->handle_request();
}
);
}
/**