Skip to content

Commit

Permalink
Merge pull request #274 from woocommerce/PCP-288-vaulting-token-creat…
Browse files Browse the repository at this point in the history
…ion-fails

Allow customers to see and delete their saved payments in My Account.
  • Loading branch information
Dinamiko authored Sep 24, 2021
2 parents 7aac842 + 15f69e8 commit 0515bdf
Show file tree
Hide file tree
Showing 26 changed files with 5,372 additions and 25 deletions.
1 change: 1 addition & 0 deletions modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
( require "$modules_dir/ppcp-subscription/module.php" )(),
( require "$modules_dir/ppcp-wc-gateway/module.php" )(),
( require "$modules_dir/ppcp-webhooks/module.php" )(),
( require "$modules_dir/ppcp-vaulting/module.php" )(),
);

return $modules;
Expand Down
2 changes: 1 addition & 1 deletion modules/ppcp-button/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
$subscription_helper = $container->get( 'subscription.helper' );
$messages_apply = $container->get( 'button.helper.messages-apply' );
$environment = $container->get( 'onboarding.environment' );
$payment_token_repository = $container->get( 'subscription.repository.payment-token' );
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
$settings_status = $container->get( 'wcgateway.settings.status' );
return new SmartButton(
$container->get( 'button.url' ),
Expand Down
2 changes: 1 addition & 1 deletion modules/ppcp-button/src/Assets/class-smartbutton.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Subscription\Repository\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use Woocommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
Expand Down
13 changes: 3 additions & 10 deletions modules/ppcp-subscription/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
namespace WooCommerce\PayPalCommerce\Subscription;

use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Subscription\Repository\PaymentTokenRepository;
use Psr\Container\ContainerInterface;

return array(
'subscription.helper' => static function ( $container ): SubscriptionHelper {
'subscription.helper' => static function ( $container ): SubscriptionHelper {
return new SubscriptionHelper();
},
'subscription.renewal-handler' => static function ( $container ): RenewalHandler {
'subscription.renewal-handler' => static function ( $container ): RenewalHandler {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$repository = $container->get( 'subscription.repository.payment-token' );
$repository = $container->get( 'vaulting.repository.payment-token' );
$endpoint = $container->get( 'api.endpoint.order' );
$purchase_unit_factory = $container->get( 'api.factory.purchase-unit' );
$payer_factory = $container->get( 'api.factory.payer' );
Expand All @@ -31,9 +29,4 @@
$payer_factory
);
},
'subscription.repository.payment-token' => static function ( $container ): PaymentTokenRepository {
$factory = $container->get( 'api.factory.payment-token' );
$endpoint = $container->get( 'api.endpoint.payment-token' );
return new PaymentTokenRepository( $factory, $endpoint );
},
);
2 changes: 1 addition & 1 deletion modules/ppcp-subscription/src/class-renewalhandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Subscription\Repository\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use Psr\Log\LoggerInterface;

Expand Down
8 changes: 4 additions & 4 deletions modules/ppcp-subscription/src/class-subscriptionmodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Subscription\Repository\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use Interop\Container\ServiceProviderInterface;
Expand Down Expand Up @@ -66,7 +66,7 @@ function ( $amount, $order ) use ( $container ) {
add_action(
'woocommerce_subscription_payment_complete',
function ( $subscription ) use ( $container ) {
$payment_token_repository = $container->get( 'subscription.repository.payment-token' );
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );

$this->add_payment_token_id( $subscription, $payment_token_repository, $logger );
Expand All @@ -76,7 +76,7 @@ function ( $subscription ) use ( $container ) {
add_filter(
'woocommerce_gateway_description',
function ( $description, $id ) use ( $container ) {
$payment_token_repository = $container->get( 'subscription.repository.payment-token' );
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
$settings = $container->get( 'wcgateway.settings' );
$subscription_helper = $container->get( 'subscription.helper' );

Expand All @@ -89,7 +89,7 @@ function ( $description, $id ) use ( $container ) {
add_filter(
'woocommerce_credit_card_form_fields',
function ( $default_fields, $id ) use ( $container ) {
$payment_token_repository = $container->get( 'subscription.repository.payment-token' );
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
$settings = $container->get( 'wcgateway.settings' );
$subscription_helper = $container->get( 'subscription.helper' );

Expand Down
2 changes: 2 additions & 0 deletions modules/ppcp-vaulting/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/assets
12 changes: 12 additions & 0 deletions modules/ppcp-vaulting/extensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* The vaulting module extensions.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/

declare(strict_types=1);

namespace WooCommerce\PayPalCommerce\Vaulting;

return array();
16 changes: 16 additions & 0 deletions modules/ppcp-vaulting/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* The vaulting module.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/

declare(strict_types=1);

namespace WooCommerce\PayPalCommerce\Vaulting;

use Dhii\Modular\Module\ModuleInterface;

return static function (): ModuleInterface {
return new VaultingModule();
};
23 changes: 23 additions & 0 deletions modules/ppcp-vaulting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "ppcp-vaulting",
"version": "1.0.0",
"license": "GPL-3.0-or-later",
"main": "resources/js/myaccount-payments.js",
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"babel-loader": "^8.1.0",
"cross-env": "^5.0.1",
"file-loader": "^4.2.0",
"node-sass": "^4.13.0",
"sass-loader": "^8.0.0",
"webpack": "^4.42.1",
"webpack-cli": "^3.1.2",
"babel-plugin-transform-object-rest-spread": "^6.26.0"
},
"scripts": {
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
"watch": "cross-env BABEL_ENV=default NODE_ENV=production webpack --watch",
"dev": "cross-env BABEL_ENV=default webpack --watch"
}
}
41 changes: 41 additions & 0 deletions modules/ppcp-vaulting/resources/js/myaccount-payments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
document.addEventListener(
'DOMContentLoaded',
() => {
jQuery('.ppcp-delete-payment-button').click(async (event) => {
event.preventDefault();
jQuery(this).prop('disabled', true);
const token = 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();
});
});
42 changes: 42 additions & 0 deletions modules/ppcp-vaulting/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* The vaulting module services.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/

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 {
return plugins_url(
'/modules/ppcp-vaulting/',
dirname( __FILE__, 3 ) . '/woocommerce-paypal-payments.php'
);
},
'vaulting.assets.myaccount-payments' => function( $container ) : MyAccountPaymentsAssets {
return new MyAccountPaymentsAssets(
$container->get( 'vaulting.module-url' )
);
},
'vaulting.payment-tokens-renderer' => static function (): PaymentTokensRendered {
return new PaymentTokensRendered();
},
'vaulting.repository.payment-token' => static function ( $container ): PaymentTokenRepository {
$factory = $container->get( 'api.factory.payment-token' );
$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' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
);
67 changes: 67 additions & 0 deletions modules/ppcp-vaulting/src/Assets/class-myaccountpaymentsassets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Register and configure assets for My account PayPal payments page.
*
* @package WooCommerce\PayPalCommerce\Vaulting\Assets
*/

declare(strict_types=1);

namespace WooCommerce\PayPalCommerce\Vaulting\Assets;

use WooCommerce\PayPalCommerce\Vaulting\Endpoint\DeletePaymentTokenEndpoint;

/**
* Class MyAccountPaymentsAssets
*/
class MyAccountPaymentsAssets {

/**
* The URL to the module.
*
* @var string
*/
private $module_url;

/**
* WebhooksStatusPageAssets constructor.
*
* @param string $module_url The URL to the module.
*/
public function __construct(
string $module_url
) {
$this->module_url = untrailingslashit( $module_url );
}

/**
* Enqueues the necessary scripts.
*
* @return void
*/
public function enqueue(): void {
wp_enqueue_script(
'ppcp-vaulting-myaccount-payments',
$this->module_url . '/assets/js/myaccount-payments.js',
array( 'jquery' ),
'1',
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() ),
),
)
);
}
}
Loading

0 comments on commit 0515bdf

Please sign in to comment.