Fix phpcs errors

This commit is contained in:
dinamiko 2021-03-25 16:57:42 +01:00
parent f5a2fca5fb
commit a71c8b134a
9 changed files with 207 additions and 206 deletions

View file

@ -36,16 +36,19 @@ class PaymentToken {
*/ */
private $type; private $type;
/** /**
* @var \stdClass * The payment source.
*/ *
private $source; * @var \stdClass
*/
private $source;
/** /**
* PaymentToken constructor. * PaymentToken constructor.
* *
* @param string $id The Id. * @param string $id The Id.
* @param string $type The type. * @param string $type The type.
* @param \stdClass $source The source.
* @throws RuntimeException When the type is not valid. * @throws RuntimeException When the type is not valid.
*/ */
public function __construct( string $id, string $type = self::TYPE_PAYMENT_METHOD_TOKEN, \stdClass $source ) { public function __construct( string $id, string $type = self::TYPE_PAYMENT_METHOD_TOKEN, \stdClass $source ) {
@ -54,10 +57,10 @@ class PaymentToken {
__( 'Not a valid payment source type.', 'woocommerce-paypal-payments' ) __( 'Not a valid payment source type.', 'woocommerce-paypal-payments' )
); );
} }
$this->id = $id; $this->id = $id;
$this->type = $type; $this->type = $type;
$this->source = $source; $this->source = $source;
} }
/** /**
* Returns the ID. * Returns the ID.
@ -77,15 +80,14 @@ class PaymentToken {
return $this->type; return $this->type;
} }
/** /**
* Returns the source. * Returns the source.
* *
* @return \stdClass * @return \stdClass
*/ */
public function source(): \stdClass public function source(): \stdClass {
{ return $this->source;
return $this->source; }
}
/** /**
* Returns the object as array. * Returns the object as array.
@ -94,8 +96,8 @@ class PaymentToken {
*/ */
public function to_array(): array { public function to_array(): array {
return array( return array(
'id' => $this->id(), 'id' => $this->id(),
'type' => $this->type(), 'type' => $this->type(),
'source' => $this->source(), 'source' => $this->source(),
); );
} }

View file

@ -35,7 +35,7 @@ class PaymentTokenFactory {
return new PaymentToken( return new PaymentToken(
$data->id, $data->id,
( isset( $data->type ) ) ? $data->type : PaymentToken::TYPE_PAYMENT_METHOD_TOKEN, ( isset( $data->type ) ) ? $data->type : PaymentToken::TYPE_PAYMENT_METHOD_TOKEN,
$data->source $data->source
); );
} }

View file

@ -69,7 +69,7 @@ return array(
$subscription_helper = $container->get( 'subscription.helper' ); $subscription_helper = $container->get( 'subscription.helper' );
$messages_apply = $container->get( 'button.helper.messages-apply' ); $messages_apply = $container->get( 'button.helper.messages-apply' );
$environment = $container->get( 'onboarding.environment' ); $environment = $container->get( 'onboarding.environment' );
$payment_token_repository = $container->get('subscription.repository.payment-token'); $payment_token_repository = $container->get( 'subscription.repository.payment-token' );
return new SmartButton( return new SmartButton(
$container->get( 'button.url' ), $container->get( 'button.url' ),
$container->get( 'session.handler' ), $container->get( 'session.handler' ),
@ -83,7 +83,7 @@ return array(
$subscription_helper, $subscription_helper,
$messages_apply, $messages_apply,
$environment, $environment,
$payment_token_repository $payment_token_repository
); );
}, },
'button.url' => static function ( $container ): string { 'button.url' => static function ( $container ): string {

View file

@ -116,26 +116,29 @@ class SmartButton implements SmartButtonInterface {
*/ */
private $environment; private $environment;
/** /**
* @var PaymentTokenRepository * The payment token repository.
*/ *
private $payment_token_repository; * @var PaymentTokenRepository
*/
private $payment_token_repository;
/** /**
* SmartButton constructor. * SmartButton constructor.
* *
* @param string $module_url The URL to the module. * @param string $module_url The URL to the module.
* @param SessionHandler $session_handler The Session Handler. * @param SessionHandler $session_handler The Session Handler.
* @param Settings $settings The Settings. * @param Settings $settings The Settings.
* @param PayeeRepository $payee_repository The Payee Repository. * @param PayeeRepository $payee_repository The Payee Repository.
* @param IdentityToken $identity_token The Identity Token. * @param IdentityToken $identity_token The Identity Token.
* @param PayerFactory $payer_factory The Payer factory. * @param PayerFactory $payer_factory The Payer factory.
* @param string $client_id The client ID. * @param string $client_id The client ID.
* @param RequestData $request_data The Request Data helper. * @param RequestData $request_data The Request Data helper.
* @param DccApplies $dcc_applies The DCC applies helper. * @param DccApplies $dcc_applies The DCC applies helper.
* @param SubscriptionHelper $subscription_helper The subscription helper. * @param SubscriptionHelper $subscription_helper The subscription helper.
* @param MessagesApply $messages_apply The Messages apply helper. * @param MessagesApply $messages_apply The Messages apply helper.
* @param Environment $environment The environment object. * @param Environment $environment The environment object.
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
*/ */
public function __construct( public function __construct(
string $module_url, string $module_url,
@ -150,23 +153,23 @@ class SmartButton implements SmartButtonInterface {
SubscriptionHelper $subscription_helper, SubscriptionHelper $subscription_helper,
MessagesApply $messages_apply, MessagesApply $messages_apply,
Environment $environment, Environment $environment,
PaymentTokenRepository $payment_token_repository PaymentTokenRepository $payment_token_repository
) { ) {
$this->module_url = $module_url; $this->module_url = $module_url;
$this->session_handler = $session_handler; $this->session_handler = $session_handler;
$this->settings = $settings; $this->settings = $settings;
$this->payee_repository = $payee_repository; $this->payee_repository = $payee_repository;
$this->identity_token = $identity_token; $this->identity_token = $identity_token;
$this->payer_factory = $payer_factory; $this->payer_factory = $payer_factory;
$this->client_id = $client_id; $this->client_id = $client_id;
$this->request_data = $request_data; $this->request_data = $request_data;
$this->dcc_applies = $dcc_applies; $this->dcc_applies = $dcc_applies;
$this->subscription_helper = $subscription_helper; $this->subscription_helper = $subscription_helper;
$this->messages_apply = $messages_apply; $this->messages_apply = $messages_apply;
$this->environment = $environment; $this->environment = $environment;
$this->payment_token_repository = $payment_token_repository; $this->payment_token_repository = $payment_token_repository;
} }
/** /**
* Registers the necessary action hooks to render the HTML depending on the settings. * Registers the necessary action hooks to render the HTML depending on the settings.
@ -208,37 +211,37 @@ class SmartButton implements SmartButtonInterface {
11 11
); );
$payment_token_repository = $this->payment_token_repository; $payment_token_repository = $this->payment_token_repository;
add_filter( add_filter(
'woocommerce_credit_card_form_fields', 'woocommerce_credit_card_form_fields',
function ( $default_fields, $id ) use( $payment_token_repository ) { function ( $default_fields, $id ) use ( $payment_token_repository ) {
if ( $this->can_save_credit_card() ) { if ( $this->can_save_credit_card() ) {
$default_fields['card-vault'] = sprintf( $default_fields['card-vault'] = sprintf(
'<p class="form-row form-row-wide"><label for="vault"><input class="ppcp-credit-card-vault" type="checkbox" id="ppcp-credit-card-vault" name="vault">%s</label></p>', '<p class="form-row form-row-wide"><label for="vault"><input class="ppcp-credit-card-vault" type="checkbox" id="ppcp-credit-card-vault" name="vault">%s</label></p>',
esc_html__( 'Save your Credit Card', 'woocommerce-paypal-payments' ) esc_html__( 'Save your Credit Card', 'woocommerce-paypal-payments' )
); );
$tokens = $payment_token_repository->all_for_user_id( 1 ); $tokens = $payment_token_repository->all_for_user_id( 1 );
if($tokens && $this->tokens_contains_card($tokens) ) { if ( $tokens && $this->tokens_contains_card( $tokens ) ) {
$output = sprintf( $output = sprintf(
'<select id="saved-credit-card" name="saved_credit_card"><option value="">%s</option>', '<select id="saved-credit-card" name="saved_credit_card"><option value="">%s</option>',
esc_html__( 'Choose a saved payment', 'woocommerce-paypal-payments' ) esc_html__( 'Choose a saved payment', 'woocommerce-paypal-payments' )
); );
foreach ($tokens as $token) { foreach ( $tokens as $token ) {
if(isset($token->source()->card)) { if ( isset( $token->source()->card ) ) {
$output .= sprintf( $output .= sprintf(
'<option value="%1$s">%2$s ...%3$s</option>', '<option value="%1$s">%2$s ...%3$s</option>',
$token->id(), $token->id(),
$token->source()->card->brand, $token->source()->card->brand,
$token->source()->card->last_digits $token->source()->card->last_digits
); );
} }
} }
$output .= '</select>'; $output .= '</select>';
$default_fields['saved-credit-card'] = $output; $default_fields['saved-credit-card'] = $output;
} }
} }
return $default_fields; return $default_fields;
@ -1022,18 +1025,18 @@ class SmartButton implements SmartButtonInterface {
return true; return true;
} }
/** /**
* Check if tokens has card source. * Check if tokens has card source.
* *
* @param PaymentToken[] $tokens * @param PaymentToken[] $tokens The tokens.
* @return bool * @return bool Wether tokens contains card or not.
*/ */
protected function tokens_contains_card($tokens) { protected function tokens_contains_card( $tokens ) {
foreach ($tokens as $token) { foreach ( $tokens as $token ) {
if(isset($token->source()->card)) { if ( isset( $token->source()->card ) ) {
return true; return true;
} }
} }
return false; return false;
} }
} }

View file

@ -72,25 +72,27 @@ class PaymentTokenRepository {
} }
} }
/** /**
* @param int $id * Return all tokens for a user.
* @return array *
*/ * @param int $id The user id.
* @return PaymentToken[]
*/
public function all_for_user_id( int $id ) { public function all_for_user_id( int $id ) {
$tokens = get_user_meta( $id, self::USER_META, true ); $tokens = get_user_meta( $id, self::USER_META, true );
if($tokens) { if ( $tokens ) {
return (array) $tokens; return (array) $tokens;
} }
$tokens_array = []; $tokens_array = array();
try { try {
$tokens = $this->endpoint->for_user( $id ); $tokens = $this->endpoint->for_user( $id );
update_user_meta( $id, self::USER_META, $tokens ); update_user_meta( $id, self::USER_META, $tokens );
return $tokens; return $tokens;
} catch (RuntimeException $exception) { } catch ( RuntimeException $exception ) {
return []; return array();
} }
} }
/** /**
* Delete a token for a user. * Delete a token for a user.

View file

@ -58,24 +58,6 @@ class SubscriptionModule implements ModuleInterface {
10, 10,
2 2
); );
/*
add_action('woocommerce_init', function () use($container) {
$api = $container->get('api.endpoint.payment-token' );
try {
$tokens = $api->for_user(1);
for($i = 0; $i < count($tokens); $i++) {
$api->delete_token($tokens[$i]);
}
$a = 1;
} catch (RuntimeException $exception) {
$a = 1;
}
});
*/
} }
/** /**

View file

@ -66,10 +66,10 @@ return array(
$session_handler = $container->get( 'session.handler' ); $session_handler = $container->get( 'session.handler' );
$refund_processor = $container->get( 'wcgateway.processor.refunds' ); $refund_processor = $container->get( 'wcgateway.processor.refunds' );
$state = $container->get( 'onboarding.state' ); $state = $container->get( 'onboarding.state' );
$payment_token_repository = $container->get('subscription.repository.payment-token'); $payment_token_repository = $container->get( 'subscription.repository.payment-token' );
$purchase_unit_factory = $container->get('api.factory.purchase-unit'); $purchase_unit_factory = $container->get( 'api.factory.purchase-unit' );
$payer_factory = $container->get('api.factory.payer'); $payer_factory = $container->get( 'api.factory.payer' );
$order_endpoint = $container->get('api.endpoint.order'); $order_endpoint = $container->get( 'api.endpoint.order' );
return new CreditCardGateway( return new CreditCardGateway(
$settings_renderer, $settings_renderer,
$order_processor, $order_processor,
@ -80,11 +80,11 @@ return array(
$session_handler, $session_handler,
$refund_processor, $refund_processor,
$state, $state,
$payment_token_repository, $payment_token_repository,
$purchase_unit_factory, $purchase_unit_factory,
$payer_factory, $payer_factory,
$order_endpoint $order_endpoint
); );
}, },
'wcgateway.disabler' => static function ( $container ): DisableGateways { 'wcgateway.disabler' => static function ( $container ): DisableGateways {
$session_handler = $container->get( 'session.handler' ); $session_handler = $container->get( 'session.handler' );

View file

@ -45,27 +45,35 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/ */
private $refund_processor; private $refund_processor;
/** /**
* @var PaymentTokenRepository * The payment token repository.
*/ *
private $payment_token_repository; * @var PaymentTokenRepository
*/
private $payment_token_repository;
/** /**
* @var PurchaseUnitFactory * The purchase unit factory.
*/ *
private $purchase_unit_factory; * @var PurchaseUnitFactory
*/
private $purchase_unit_factory;
/** /**
* @var PayerFactory * The payer factory.
*/ *
private $payer_factory; * @var PayerFactory
*/
private $payer_factory;
/** /**
* @var OrderEndpoint * The order endpoint.
*/ *
private $order_endpoint; * @var OrderEndpoint
*/
private $order_endpoint;
/** /**
* CreditCardGateway constructor. * CreditCardGateway constructor.
* *
* @param SettingsRenderer $settings_renderer The Settings Renderer. * @param SettingsRenderer $settings_renderer The Settings Renderer.
@ -77,6 +85,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
* @param SessionHandler $session_handler The Session Handler. * @param SessionHandler $session_handler The Session Handler.
* @param RefundProcessor $refund_processor The refund processor. * @param RefundProcessor $refund_processor The refund processor.
* @param State $state The state. * @param State $state The state.
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
* @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory.
* @param PayerFactory $payer_factory The payer factory.
* @param OrderEndpoint $order_endpoint The order endpoint.
*/ */
public function __construct( public function __construct(
SettingsRenderer $settings_renderer, SettingsRenderer $settings_renderer,
@ -88,10 +100,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
SessionHandler $session_handler, SessionHandler $session_handler,
RefundProcessor $refund_processor, RefundProcessor $refund_processor,
State $state, State $state,
PaymentTokenRepository $payment_token_repository, PaymentTokenRepository $payment_token_repository,
PurchaseUnitFactory $purchase_unit_factory, PurchaseUnitFactory $purchase_unit_factory,
PayerFactory $payer_factory, PayerFactory $payer_factory,
OrderEndpoint $order_endpoint OrderEndpoint $order_endpoint
) { ) {
$this->id = self::ID; $this->id = self::ID;
@ -151,12 +163,12 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
) )
); );
$this->module_url = $module_url; $this->module_url = $module_url;
$this->payment_token_repository = $payment_token_repository; $this->payment_token_repository = $payment_token_repository;
$this->purchase_unit_factory = $purchase_unit_factory; $this->purchase_unit_factory = $purchase_unit_factory;
$this->payer_factory = $payer_factory; $this->payer_factory = $payer_factory;
$this->order_endpoint = $order_endpoint; $this->order_endpoint = $order_endpoint;
} }
/** /**
* Initialize the form fields. * Initialize the form fields.

View file

@ -31,55 +31,55 @@ trait ProcessPaymentTrait {
return null; return null;
} }
/** /**
* If customer has chosed a saved credit card payment. * If customer has chosed a saved credit card payment.
*/ */
$saved_credit_card = filter_input(INPUT_POST, 'saved_credit_card', FILTER_SANITIZE_STRING); $saved_credit_card = filter_input( INPUT_POST, 'saved_credit_card', FILTER_SANITIZE_STRING );
if($saved_credit_card) { if ( $saved_credit_card ) {
$user_id = (int) $wc_order->get_customer_id(); $user_id = (int) $wc_order->get_customer_id();
$customer = new \WC_Customer( $user_id ); $customer = new \WC_Customer( $user_id );
$tokens = $this->payment_token_repository->all_for_user_id( (int) $customer->get_id() ); $tokens = $this->payment_token_repository->all_for_user_id( (int) $customer->get_id() );
$selected_token = null; $selected_token = null;
foreach ($tokens as $token) { foreach ( $tokens as $token ) {
if($token->id() === $saved_credit_card) { if ( $token->id() === $saved_credit_card ) {
$selected_token = $token; $selected_token = $token;
break; break;
} }
} }
if(!$selected_token) { if ( ! $selected_token ) {
return null; return null;
} }
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order ); $purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
$payer = $this->payer_factory->from_customer( $customer ); $payer = $this->payer_factory->from_customer( $customer );
try { try {
$order = $this->order_endpoint->create( $order = $this->order_endpoint->create(
array( $purchase_unit ), array( $purchase_unit ),
$payer, $payer,
$selected_token $selected_token
); );
if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'CAPTURE' ) { if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'CAPTURE' ) {
$wc_order->update_status( $wc_order->update_status(
'processing', 'processing',
__( 'Payment received.', 'woocommerce-paypal-payments' ) __( 'Payment received.', 'woocommerce-paypal-payments' )
); );
$this->session_handler->destroy_session_data(); $this->session_handler->destroy_session_data();
return array( return array(
'result' => 'success', 'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ), 'redirect' => $this->get_return_url( $wc_order ),
); );
} }
} catch (RuntimeException $error) { } catch ( RuntimeException $error ) {
$this->session_handler->destroy_session_data(); $this->session_handler->destroy_session_data();
wc_add_notice( $error->getMessage(), 'error' ); wc_add_notice( $error->getMessage(), 'error' );
return null; return null;
} }
} }
/** /**
* If the WC_Order is payed through the approved webhook. * If the WC_Order is payed through the approved webhook.