mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Add delete payment functionality
This commit is contained in:
parent
964936ee17
commit
28b12af089
7 changed files with 243 additions and 56 deletions
|
@ -3,7 +3,39 @@ document.addEventListener(
|
|||
() => {
|
||||
jQuery('.ppcp-delete-payment-button').click(async (event) => {
|
||||
event.preventDefault();
|
||||
jQuery(this).prop('disabled', true);
|
||||
const token = event.target.id;
|
||||
|
||||
console.log(event.target.id)
|
||||
const response = await fetch(
|
||||
PayPalCommerceGatewayVaulting.delete.endpoint,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(
|
||||
{
|
||||
nonce: PayPalCommerceGatewayVaulting.delete.nonce,
|
||||
token,
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
const reportError = error => {
|
||||
alert(error);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
try {
|
||||
const result = await response.json();
|
||||
reportError(result.data);
|
||||
} catch (exc) {
|
||||
console.error(exc);
|
||||
reportError(response.status);
|
||||
}
|
||||
}
|
||||
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,6 +10,7 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\Vaulting;
|
||||
|
||||
use WooCommerce\PayPalCommerce\Vaulting\Assets\MyAccountPaymentsAssets;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\Endpoint\DeletePaymentTokenEndpoint;
|
||||
|
||||
return array(
|
||||
'vaulting.module-url' => static function ( $container ): string {
|
||||
|
@ -31,4 +32,10 @@ return array(
|
|||
$endpoint = $container->get( 'api.endpoint.payment-token' );
|
||||
return new PaymentTokenRepository( $factory, $endpoint );
|
||||
},
|
||||
'vaulting.endpoint.delete' => function( $container ) : DeletePaymentTokenEndpoint {
|
||||
return new DeletePaymentTokenEndpoint(
|
||||
$container->get( 'vaulting.repository.payment-token' ),
|
||||
$container->get( 'button.request-data' )
|
||||
);
|
||||
},
|
||||
);
|
||||
|
|
|
@ -9,6 +9,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Vaulting\Assets;
|
||||
|
||||
use WooCommerce\PayPalCommerce\Vaulting\Endpoint\DeletePaymentTokenEndpoint;
|
||||
|
||||
/**
|
||||
* Class MyAccountPaymentsAssets
|
||||
*/
|
||||
class MyAccountPaymentsAssets {
|
||||
|
||||
/**
|
||||
|
@ -43,4 +48,20 @@ class MyAccountPaymentsAssets {
|
|||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Localize script.
|
||||
*/
|
||||
public function localize() {
|
||||
wp_localize_script(
|
||||
'ppcp-vaulting-myaccount-payments',
|
||||
'PayPalCommerceGatewayVaulting',
|
||||
array(
|
||||
'delete' => array(
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( DeletePaymentTokenEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( DeletePaymentTokenEndpoint::nonce() ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
/**
|
||||
* The endpoint for deleting payment tokens.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Vaulting\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Vaulting\Endpoint;
|
||||
|
||||
use Exception;
|
||||
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||
|
||||
/**
|
||||
* Class DeletePayment
|
||||
*/
|
||||
class DeletePaymentTokenEndpoint {
|
||||
|
||||
const ENDPOINT = 'ppc-vaulting-delete';
|
||||
|
||||
/**
|
||||
* The repository.
|
||||
*
|
||||
* @var PaymentTokenRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* The request data.
|
||||
*
|
||||
* @var RequestData
|
||||
*/
|
||||
protected $request_data;
|
||||
|
||||
/**
|
||||
* DeletePaymentTokenEndpoint constructor.
|
||||
*
|
||||
* @param PaymentTokenRepository $repository The repository.
|
||||
* @param RequestData $request_data The request data.
|
||||
*/
|
||||
public function __construct( PaymentTokenRepository $repository, RequestData $request_data ) {
|
||||
$this->repository = $repository;
|
||||
$this->request_data = $request_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nonce for the endpoint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the incoming request.
|
||||
*/
|
||||
public function handle_request() {
|
||||
try {
|
||||
$data = $this->request_data->read_request( $this->nonce() );
|
||||
|
||||
$tokens = $this->repository->all_for_user_id( get_current_user_id() );
|
||||
if ( $tokens ) {
|
||||
foreach ( $tokens as $token ) {
|
||||
if ( isset( $data['token'] ) && $token->id() === $data['token'] ) {
|
||||
if ( $this->repository->delete_token( get_current_user_id(), $token ) ) {
|
||||
wp_send_json_success();
|
||||
return true;
|
||||
}
|
||||
|
||||
wp_send_json_error( 'Could not delete payment token.' );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch ( Exception $error ) {
|
||||
wp_send_json_error( $error->getMessage(), 403 );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Vaulting;
|
||||
|
||||
class PaymentTokensRendered {
|
||||
|
||||
public function render( array $tokens ) {
|
||||
ob_start();
|
||||
?>
|
||||
<table class="shop_table shop_table_responsive">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Payment sources</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ( $tokens as $token ) {
|
||||
$source = $token->source() ?? null;
|
||||
if ( $source && isset( $source->card ) ) {
|
||||
?>
|
||||
<tr>
|
||||
<td><?= $source->card->brand . ' ...' . $source->card->last_digits;?></td>
|
||||
<td>
|
||||
<a class="ppcp-delete-payment-button" id="<?= $token->id();?>" href="">Delete</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
|
||||
if ( $source && isset( $source->paypal ) ) {
|
||||
?>
|
||||
<tr>
|
||||
<td><?= $source->paypal->payer->email_address;?></td>
|
||||
<td>
|
||||
<a class="ppcp-delete-payment-button" id="<?= $token->id();?>" href="">Delete</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
83
modules/ppcp-vaulting/src/class-paymenttokensrenderer.php
Normal file
83
modules/ppcp-vaulting/src/class-paymenttokensrenderer.php
Normal file
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
/**
|
||||
* The payment tokens renderer.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Vaulting
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Vaulting;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
|
||||
|
||||
/**
|
||||
* Class PaymentTokensRendered
|
||||
*/
|
||||
class PaymentTokensRendered {
|
||||
|
||||
/**
|
||||
* Render payment tokens.
|
||||
*
|
||||
* @param PaymentToken[] $tokens The tokens.
|
||||
* @return false|string
|
||||
*/
|
||||
public function render( array $tokens ) {
|
||||
ob_start();
|
||||
?>
|
||||
<table class="shop_table shop_table_responsive">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php echo esc_html__( 'Payment sources', 'woocommerce-paypal-payments' ); ?></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ( $tokens as $token ) {
|
||||
$source = $token->source() ?? null;
|
||||
if ( $source && isset( $source->card ) ) {
|
||||
?>
|
||||
<tr>
|
||||
<td><?php echo esc_attr( $source->card->brand ) . ' ...' . esc_attr( $source->card->last_digits ); ?></td>
|
||||
<td>
|
||||
<a class="ppcp-delete-payment-button" id="<?php echo esc_attr( $token->id() ); ?>" href=""><?php echo esc_html__( 'Delete', 'woocommerce-paypal-payments' ); ?></a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
if ( $source && isset( $source->paypal ) ) {
|
||||
?>
|
||||
<tr>
|
||||
<td><?php echo esc_attr( $source->paypal->payer->email_address ); ?></td>
|
||||
<td>
|
||||
<a class="ppcp-delete-payment-button" id="<?php echo esc_attr( $token->id() ); ?>" href=""><?php echo esc_html__( 'Delete', 'woocommerce-paypal-payments' ); ?></a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render no payments message.
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public function render_no_tokens() {
|
||||
ob_start();
|
||||
?>
|
||||
<div class="woocommerce-Message woocommerce-Message--info woocommerce-info">
|
||||
<?php echo esc_html__( 'No payments available yet.', 'woocommerce-paypal-payments' ); ?>
|
||||
</div>
|
||||
<?php
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@ use Dhii\Container\ServiceProvider;
|
|||
use Dhii\Modular\Module\ModuleInterface;
|
||||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\Assets\MyAccountPaymentsAssets;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\Endpoint\DeletePaymentTokenEndpoint;
|
||||
|
||||
/**
|
||||
* Class StatusReportModule
|
||||
|
@ -61,11 +61,13 @@ class VaultingModule implements ModuleInterface {
|
|||
'woocommerce_account_ppcp-paypal-payment-tokens_endpoint',
|
||||
function () use ( $container ) {
|
||||
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
|
||||
$renderer = $container->get( 'vaulting.payment-tokens-renderer' );
|
||||
|
||||
$tokens = $payment_token_repository->all_for_user_id( get_current_user_id() );
|
||||
if ( $tokens ) {
|
||||
$renderer = $container->get( 'vaulting.payment-tokens-renderer' );
|
||||
echo wp_kses_post( $renderer->render( $tokens ) );
|
||||
} else {
|
||||
echo wp_kses_post( $renderer->render_no_tokens() );
|
||||
}
|
||||
}
|
||||
);
|
||||
|
@ -76,9 +78,20 @@ class VaultingModule implements ModuleInterface {
|
|||
function () use ( $asset_loader ) {
|
||||
if ( is_account_page() && $this->is_payments_page() ) {
|
||||
$asset_loader->enqueue();
|
||||
$asset_loader->localize();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
add_action(
|
||||
'wc_ajax_' . DeletePaymentTokenEndpoint::ENDPOINT,
|
||||
static function () use ( $container ) {
|
||||
$endpoint = $container->get( 'vaulting.endpoint.delete' );
|
||||
assert( $endpoint instanceof DeletePaymentTokenEndpoint );
|
||||
|
||||
$endpoint->handle_request();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue