Merge branch 'trunk' into fix/PCP-1713_update_wroding_preview_button

This commit is contained in:
carmenmaymo 2023-06-07 14:33:35 +02:00
commit cce7709330
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
27 changed files with 273 additions and 471 deletions

View file

@ -93,7 +93,7 @@ const PayPalComponent = ({
throw new Error(config.scriptData.labels.error.generic)
}
const order = json.data;
const order = await actions.order.get();
setPaypalOrder(order);

View file

@ -109,7 +109,7 @@ class CheckoutActionHandler {
const input = document.createElement('input');
input.setAttribute('type', 'hidden');
input.setAttribute('name', 'ppcp-resume-order');
input.setAttribute('value', data.data.purchase_units[0].custom_id);
input.setAttribute('value', data.data.custom_id);
document.querySelector(formSelector).appendChild(input);
return data.data.id;
});

View file

@ -180,7 +180,7 @@ class ApproveOrderEndpoint implements EndpointInterface {
);
}
$this->session_handler->replace_order( $order );
wp_send_json_success( $order->to_array() );
wp_send_json_success();
}
if ( $this->order_helper->contains_physical_goods( $order ) && ! $order->status()->is( OrderStatus::APPROVED ) && ! $order->status()->is( OrderStatus::CREATED ) ) {
@ -198,7 +198,7 @@ class ApproveOrderEndpoint implements EndpointInterface {
$this->session_handler->replace_funding_source( $funding_source );
$this->session_handler->replace_order( $order );
wp_send_json_success( $order->to_array() );
wp_send_json_success();
return true;
} catch ( Exception $error ) {
$this->logger->error( 'Order approve failed: ' . $error->getMessage() );

View file

@ -88,7 +88,7 @@ class ApproveSubscriptionEndpoint implements EndpointInterface {
WC()->session->set( 'ppcp_subscription_id', $data['subscription_id'] );
}
wp_send_json_success( $order );
wp_send_json_success();
return true;
}
}

View file

@ -292,7 +292,7 @@ class CreateOrderEndpoint implements EndpointInterface {
! $this->early_order_handler->should_create_early_order()
|| $this->registration_needed
|| isset( $data['createaccount'] ) && '1' === $data['createaccount'] ) {
wp_send_json_success( $order->to_array() );
wp_send_json_success( $this->make_response( $order ) );
}
$this->early_order_handler->register_for_order( $order );
@ -304,7 +304,7 @@ class CreateOrderEndpoint implements EndpointInterface {
$wc_order->save_meta_data();
}
wp_send_json_success( $order->to_array() );
wp_send_json_success( $this->make_response( $order ) );
return true;
} catch ( ValidationException $error ) {
@ -362,7 +362,7 @@ class CreateOrderEndpoint implements EndpointInterface {
* during the "onApprove"-JS callback or the webhook listener.
*/
if ( ! $this->early_order_handler->should_create_early_order() ) {
wp_send_json_success( $order->to_array() );
wp_send_json_success( $this->make_response( $order ) );
}
$this->early_order_handler->register_for_order( $order );
return $data;
@ -569,4 +569,17 @@ class CreateOrderEndpoint implements EndpointInterface {
);
}
}
/**
* Returns the response data for success response.
*
* @param Order $order The PayPal order.
* @return array
*/
private function make_response( Order $order ): array {
return array(
'id' => $order->id(),
'custom_id' => $order->purchase_units()[0]->custom_id(),
);
}
}

View file

@ -123,6 +123,11 @@ class LoginSellerEndpoint implements EndpointInterface {
public function handle_request(): bool {
try {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'Not admin.', 403 );
return false;
}
$data = $this->request_data->read_request( $this->nonce() );
$is_sandbox = isset( $data['env'] ) && 'sandbox' === $data['env'];
$this->settings->set( 'sandbox_on', $is_sandbox );

View file

@ -107,6 +107,11 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
* @throws NotFoundException When order not found or handling failed.
*/
public function handle_request(): bool {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'Not admin.', 403 );
return false;
}
$signup_links = array();
try {

View file

@ -85,6 +85,11 @@ class OrderTrackingEndpoint {
* Handles the request.
*/
public function handle_request(): void {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'Not admin.', 403 );
return;
}
try {
$data = $this->request_data->read_request( $this->nonce() );
$action = $data['action'];

View file

@ -81,8 +81,14 @@ class UninstallModule implements ModuleInterface {
"wc_ajax_{$nonce}",
static function () use ( $request_data, $clear_db, $nonce, $option_names, $scheduled_action_names ) {
try {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'Not admin.', 403 );
return false;
}
// Validate nonce.
$request_data->read_request( $nonce );
$clear_db->delete_options( $option_names );
$clear_db->clear_scheduled_actions( $scheduled_action_names );

View file

@ -1,72 +0,0 @@
<?php
/**
* WooCommerce Payment token for PayPal ACDC (Advanced Credit and Debit Card).
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use WC_Payment_Token;
/**
* Class PaymentTokenACDC
*/
class PaymentTokenACDC extends WC_Payment_Token {
/**
* Token Type String.
*
* @var string
*/
protected $type = 'ACDC';
/**
* Stores Credit Card payment token data.
*
* @var array
*/
protected $extra_data = array(
'last4' => '',
'card_type' => '',
);
/**
* Returns the last four digits.
*
* @param string $context The context.
* @return mixed|null
*/
public function get_last4( $context = 'view' ) {
return $this->get_prop( 'last4', $context );
}
/**
* Set the last four digits.
*
* @param string $last4 Last four digits.
*/
public function set_last4( $last4 ) {
$this->set_prop( 'last4', $last4 );
}
/**
* Returns the card type (mastercard, visa, ...).
*
* @param string $context The context.
* @return string Card type
*/
public function get_card_type( $context = 'view' ) {
return $this->get_prop( 'card_type', $context );
}
/**
* Set the card type (mastercard, visa, ...).
*
* @param string $type Credit card type (mastercard, visa, ...).
*/
public function set_card_type( $type ) {
$this->set_prop( 'card_type', $type );
}
}

View file

@ -19,14 +19,12 @@ class PaymentTokenFactory {
*
* @param string $type The type of WC payment token.
*
* @return void|PaymentTokenACDC|PaymentTokenPayPal
* @return void|PaymentTokenPayPal
*/
public function create( string $type ) {
switch ( $type ) {
case 'paypal':
return new PaymentTokenPayPal();
case 'acdc':
return new PaymentTokenACDC();
}
}
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Vaulting;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Payment_Token_CC;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
@ -64,27 +65,26 @@ class PaymentTokensMigration {
* @param int $id WooCommerce customer id.
*/
public function migrate_payment_tokens_for_user( int $id ):void {
$tokens = $this->payment_token_repository->all_for_user_id( $id );
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $id );
$tokens = $this->payment_token_repository->all_for_user_id( $id );
$total_tokens = count( $tokens );
$this->logger->info( 'Migrating ' . (string) $total_tokens . ' tokens for user ' . (string) $id );
foreach ( $tokens as $token ) {
if ( $this->token_exist( $wc_tokens, $token ) ) {
$this->logger->info( 'Token already exist for user ' . (string) $id );
continue;
}
if ( isset( $token->source()->card ) ) {
$payment_token_acdc = $this->payment_token_factory->create( 'acdc' );
assert( $payment_token_acdc instanceof PaymentTokenACDC );
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $id, CreditCardGateway::ID );
if ( $this->token_exist( $wc_tokens, $token ) ) {
$this->logger->info( 'Token already exist for user ' . (string) $id );
continue;
}
$payment_token_acdc = new WC_Payment_Token_CC();
$payment_token_acdc->set_token( $token->id() );
$payment_token_acdc->set_user_id( $id );
$payment_token_acdc->set_gateway_id( CreditCardGateway::ID );
$payment_token_acdc->set_last4( $token->source()->card->last_digits );
$payment_token_acdc->set_card_type( $token->source()->card->brand );
$payment_token_acdc->set_expiry_year( '0000' );
$payment_token_acdc->set_expiry_month( '00' );
try {
$payment_token_acdc->save();
@ -96,6 +96,12 @@ class PaymentTokensMigration {
continue;
}
} elseif ( $token->source()->paypal ) {
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $id, PayPalGateway::ID );
if ( $this->token_exist( $wc_tokens, $token ) ) {
$this->logger->info( 'Token already exist for user ' . (string) $id );
continue;
}
$payment_token_paypal = $this->payment_token_factory->create( 'paypal' );
assert( $payment_token_paypal instanceof PaymentTokenPayPal );

View file

@ -88,10 +88,6 @@ class VaultingModule implements ModuleInterface {
* @psalm-suppress MissingClosureParamType
*/
function ( $type ) {
if ( $type === 'WC_Payment_Token_ACDC' ) {
return PaymentTokenACDC::class;
}
if ( $type === 'WC_Payment_Token_PayPal' ) {
return PaymentTokenPayPal::class;
}
@ -112,13 +108,6 @@ class VaultingModule implements ModuleInterface {
return $item;
}
if ( strtolower( $payment_token->get_type() ) === 'acdc' ) {
assert( $payment_token instanceof PaymentTokenACDC );
$item['method']['brand'] = $payment_token->get_card_type() . ' ...' . $payment_token->get_last4();
return $item;
}
if ( strtolower( $payment_token->get_type() ) === 'paypal' ) {
assert( $payment_token instanceof PaymentTokenPayPal );
$item['method']['brand'] = $payment_token->get_email();

View file

@ -40,7 +40,6 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\GatewayRepository;
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\FraudNet\FraudNet;
@ -1076,15 +1075,6 @@ 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

@ -228,24 +228,5 @@ class OXXO {
true
);
}
wp_localize_script(
'ppcp-oxxo',
'OXXOConfig',
array(
'oxxo_endpoint' => \WC_AJAX::get_endpoint( 'ppc-oxxo' ),
'oxxo_nonce' => wp_create_nonce( 'ppc-oxxo' ),
'error' => array(
'generic' => __(
'Something went wrong. Please try again or choose another payment source.',
'woocommerce-paypal-payments'
),
'js_validation' => __(
'Required form fields are not filled or invalid.',
'woocommerce-paypal-payments'
),
),
)
);
}
}

View file

@ -1,156 +0,0 @@
<?php
/**
* Handles OXXO payer action.
*
* @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;
/**
* OXXOEndpoint constructor.
*/
class OXXOEndpoint implements EndpointInterface {
/**
* The request data
*
* @var RequestData
*/
protected $request_data;
/**
* The purchase unit factory.
*
* @var PurchaseUnitFactory
*/
protected $purchase_unit_factory;
/**
* The shipping preference factory.
*
* @var ShippingPreferenceFactory
*/
protected $shipping_preference_factory;
/**
* The order endpoint.
*
* @var OrderEndpoint
*/
protected $order_endpoint;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* OXXOEndpoint constructor
*
* @param RequestData $request_data The request data.
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory.
* @param ShippingPreferenceFactory $shipping_preference_factory The shipping preference factory.
* @param LoggerInterface $logger The logger.
*/
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;
}
/**
* The nonce
*
* @return string
*/
public static function nonce(): string {
return 'ppc-oxxo';
}
/**
* Handles the request.
*
* @return bool
*/
public function handle_request(): bool {
$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' );
wp_send_json_error( 'Could not get OXXO payer action.' );
return false;
}
WC()->session->set( 'ppcp_payer_action', $payer_action );
wp_send_json_success(
array( 'payer_action' => $payer_action )
);
return true;
}
}

View file

@ -323,14 +323,6 @@ class WCGatewayModule implements ModuleInterface {
2
);
add_action(
'wc_ajax_ppc-oxxo',
static function () use ( $c ) {
$endpoint = $c->get( 'wcgateway.endpoint.oxxo' );
$endpoint->handle_request();
}
);
add_action(
'woocommerce_order_status_changed',
static function ( int $order_id, string $from, string $to ) use ( $c ) {

View file

@ -58,6 +58,11 @@ class ResubscribeEndpoint {
* Handles the incoming request.
*/
public function handle_request() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'Not admin.', 403 );
return false;
}
try {
// Validate nonce.
$this->request_data->read_request( $this->nonce() );

View file

@ -61,6 +61,11 @@ class SimulateEndpoint {
* Handles the incoming request.
*/
public function handle_request() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'Not admin.', 403 );
return false;
}
try {
// Validate nonce.
$this->request_data->read_request( $this->nonce() );

View file

@ -51,6 +51,11 @@ class SimulationStateEndpoint {
* Handles the incoming request.
*/
public function handle_request() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'Not admin.', 403 );
return false;
}
try {
$state = $this->simulation->get_state();