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;
/**
* @var \stdClass
*/
private $source;
/**
* The payment source.
*
* @var \stdClass
*/
private $source;
/**
/**
* PaymentToken constructor.
*
* @param string $id The Id.
* @param string $type The type.
* @param string $id The Id.
* @param string $type The type.
* @param \stdClass $source The source.
* @throws RuntimeException When the type is not valid.
*/
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' )
);
}
$this->id = $id;
$this->type = $type;
$this->source = $source;
}
$this->id = $id;
$this->type = $type;
$this->source = $source;
}
/**
* Returns the ID.
@ -77,15 +80,14 @@ class PaymentToken {
return $this->type;
}
/**
* Returns the source.
*
* @return \stdClass
*/
public function source(): \stdClass
{
return $this->source;
}
/**
* Returns the source.
*
* @return \stdClass
*/
public function source(): \stdClass {
return $this->source;
}
/**
* Returns the object as array.
@ -94,8 +96,8 @@ class PaymentToken {
*/
public function to_array(): array {
return array(
'id' => $this->id(),
'type' => $this->type(),
'id' => $this->id(),
'type' => $this->type(),
'source' => $this->source(),
);
}

View file

@ -35,7 +35,7 @@ class PaymentTokenFactory {
return new PaymentToken(
$data->id,
( 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' );
$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( 'subscription.repository.payment-token' );
return new SmartButton(
$container->get( 'button.url' ),
$container->get( 'session.handler' ),
@ -83,7 +83,7 @@ return array(
$subscription_helper,
$messages_apply,
$environment,
$payment_token_repository
$payment_token_repository
);
},
'button.url' => static function ( $container ): string {

View file

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

View file

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

View file

@ -58,24 +58,6 @@ class SubscriptionModule implements ModuleInterface {
10,
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' );
$refund_processor = $container->get( 'wcgateway.processor.refunds' );
$state = $container->get( 'onboarding.state' );
$payment_token_repository = $container->get('subscription.repository.payment-token');
$purchase_unit_factory = $container->get('api.factory.purchase-unit');
$payer_factory = $container->get('api.factory.payer');
$order_endpoint = $container->get('api.endpoint.order');
$payment_token_repository = $container->get( 'subscription.repository.payment-token' );
$purchase_unit_factory = $container->get( 'api.factory.purchase-unit' );
$payer_factory = $container->get( 'api.factory.payer' );
$order_endpoint = $container->get( 'api.endpoint.order' );
return new CreditCardGateway(
$settings_renderer,
$order_processor,
@ -80,11 +80,11 @@ return array(
$session_handler,
$refund_processor,
$state,
$payment_token_repository,
$purchase_unit_factory,
$payer_factory,
$order_endpoint
);
$payment_token_repository,
$purchase_unit_factory,
$payer_factory,
$order_endpoint
);
},
'wcgateway.disabler' => static function ( $container ): DisableGateways {
$session_handler = $container->get( 'session.handler' );

View file

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

View file

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