Skip to content

Commit

Permalink
Add change subscription payment for PayPal payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Emili Castells Guasch authored and Emili Castells Guasch committed Jan 3, 2024
1 parent f758469 commit e23f50a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
19 changes: 15 additions & 4 deletions modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
Expand Down Expand Up @@ -522,10 +522,21 @@ function ( PaymentToken $token ): bool {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$saved_paypal_payment = wc_clean( wp_unslash( $_POST['saved_paypal_payment'] ?? '' ) );
if ( $saved_paypal_payment ) {
$wc_order->update_meta_data( 'payment_token_id', $saved_paypal_payment );
$wc_order->save();
$payment_token = WC_Payment_Tokens::get( $saved_paypal_payment );
if ( $payment_token ) {
$wc_order->add_payment_token( $payment_token );
$wc_order->save();

return $this->handle_payment_success( $wc_order );
return $this->handle_payment_success( $wc_order );
}

wc_add_notice( __( 'Could not change payment.', 'woocommerce-paypal-payments' ), 'error' );

return array(
'result' => 'failure',
'redirect' => wc_get_checkout_url(),
'errorMessage' => __( 'Could not change payment.', 'woocommerce-paypal-payments' ),
);
}
}

Expand Down
8 changes: 5 additions & 3 deletions modules/ppcp-wc-subscriptions/src/RenewalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,11 @@ private function token_id( WC_Order $wc_order, WC_Customer $customer ): string {

$tokens = $wc_order->get_payment_tokens();
if ( $tokens ) {
$token = WC_Payment_Tokens::get( $tokens[0] );
if ( $token ) {
$token_id = $token->get_token();
foreach ( $tokens as $token_id ) {
$token = WC_Payment_Tokens::get( $token_id );
if ( $token && $token->get_gateway_id() === $wc_order->get_payment_method() ) {
return $token->get_token();
}
}
}

Expand Down
27 changes: 9 additions & 18 deletions modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,21 @@ protected function display_saved_paypal_payments(
&& PayPalGateway::ID === $id
&& $subscription_helper->is_subscription_change_payment()
) {
$tokens = $payment_token_repository->all_for_user_id( get_current_user_id() );
if ( ! $tokens || ! $payment_token_repository->tokens_contains_paypal( $tokens ) ) {
return esc_html__(
'No PayPal payments saved, in order to use a saved payment you first need to create it through a purchase.',
'woocommerce-paypal-payments'
);
}

$tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id(), PayPalGateway::ID );
$output = sprintf(
'<p class="form-row form-row-wide"><label>%1$s</label><select id="saved-paypal-payment" name="saved_paypal_payment">',
esc_html__( 'Select a saved PayPal payment', 'woocommerce-paypal-payments' )
esc_html__( 'Select PayPal payment', 'woocommerce-paypal-payments' )
);
foreach ( $tokens as $token ) {
if ( isset( $token->source()->paypal ) ) {
$output .= sprintf(
'<option value="%1$s">%2$s</option>',
$token->id(),
$token->source()->paypal->payer->email_address
);
}
$output .= sprintf(
'<option value="%1$s">%2$s</option>',
$token->get_id(),
$token->get_meta( 'email' ) ?? ''
);
}
$output .= '</select></p>';
$output .= '</select></p>';

return $output;
return $output;
}

return $description;
Expand Down

0 comments on commit e23f50a

Please sign in to comment.