2020-09-14 14:17:39 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The process_payment functionality for the both gateways.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\PayPalCommerce\WcGateway\Gateway
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
|
|
|
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
|
|
|
|
|
2021-03-25 16:11:45 +01:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
2020-09-14 14:17:39 +03:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
2020-11-20 15:40:01 +01:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
2020-09-14 14:17:39 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait ProcessPaymentTrait
|
|
|
|
*/
|
|
|
|
trait ProcessPaymentTrait {
|
|
|
|
/**
|
|
|
|
* Process a payment for an WooCommerce order.
|
|
|
|
*
|
|
|
|
* @param int $order_id The WooCommerce order id.
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function process_payment( $order_id ) {
|
|
|
|
global $woocommerce;
|
|
|
|
$wc_order = wc_get_order( $order_id );
|
|
|
|
if ( ! is_a( $wc_order, \WC_Order::class ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-25 16:11:45 +01:00
|
|
|
/**
|
|
|
|
* 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() );
|
|
|
|
|
|
|
|
$selected_token = null;
|
|
|
|
foreach ($tokens as $token) {
|
|
|
|
if($token->id() === $saved_credit_card) {
|
|
|
|
$selected_token = $token;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:17:39 +03:00
|
|
|
/**
|
|
|
|
* If the WC_Order is payed through the approved webhook.
|
|
|
|
*/
|
|
|
|
//phpcs:disable WordPress.Security.NonceVerification.Recommended
|
|
|
|
if ( isset( $_REQUEST['ppcp-resume-order'] ) && $wc_order->has_status( 'processing' ) ) {
|
|
|
|
$this->session_handler->destroy_session_data();
|
|
|
|
return array(
|
|
|
|
'result' => 'success',
|
|
|
|
'redirect' => $this->get_return_url( $wc_order ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ( $this->order_processor->process( $wc_order, $woocommerce ) ) {
|
|
|
|
$this->session_handler->destroy_session_data();
|
|
|
|
return array(
|
|
|
|
'result' => 'success',
|
|
|
|
'redirect' => $this->get_return_url( $wc_order ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch ( PayPalApiException $error ) {
|
|
|
|
if ( $error->has_detail( 'INSTRUMENT_DECLINED' ) ) {
|
|
|
|
$this->session_handler->increment_insufficient_funding_tries();
|
|
|
|
$host = $this->config->has( 'sandbox_on' ) && $this->config->get( 'sandbox_on' ) ?
|
|
|
|
'https://www.sandbox.paypal.com/' : 'https://www.paypal.com/';
|
|
|
|
$url = $host . 'checkoutnow?token=' . $this->session_handler->order()->id();
|
|
|
|
if ( $this->session_handler->insufficient_funding_tries() >= 3 ) {
|
|
|
|
$this->session_handler->destroy_session_data();
|
|
|
|
wc_add_notice(
|
2020-10-08 20:03:07 -03:00
|
|
|
__( 'Please use a different payment method.', 'woocommerce-paypal-payments' ),
|
2020-09-14 14:17:39 +03:00
|
|
|
'error'
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'result' => 'success',
|
|
|
|
'redirect' => $url,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->session_handler->destroy_session_data();
|
2020-11-20 15:40:01 +01:00
|
|
|
} catch ( RuntimeException $error ) {
|
|
|
|
$this->session_handler->destroy_session_data();
|
2020-11-23 16:25:43 +01:00
|
|
|
wc_add_notice( $error->getMessage(), 'error' );
|
2020-11-20 15:40:01 +01:00
|
|
|
return null;
|
2020-09-14 14:17:39 +03:00
|
|
|
}
|
2020-11-20 15:40:01 +01:00
|
|
|
|
2020-09-14 14:17:39 +03:00
|
|
|
wc_add_notice(
|
|
|
|
$this->order_processor->last_error(),
|
|
|
|
'error'
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-10 12:10:12 +01:00
|
|
|
|
|
|
|
/**
|
2021-03-12 10:52:07 +01:00
|
|
|
* Checks if vault enabled setting for PayPal or credit card is enabled.
|
2021-03-10 12:55:35 +01:00
|
|
|
*
|
|
|
|
* @return bool Whether vault settings are enabled or not.
|
|
|
|
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasn't been found.
|
2021-03-10 12:10:12 +01:00
|
|
|
*/
|
|
|
|
protected function vault_settings_enabled(): bool {
|
2021-03-12 10:52:07 +01:00
|
|
|
if ( $this->config->has( 'vault_enabled' ) && $this->config->get( 'vault_enabled' ) ) {
|
|
|
|
return true;
|
2021-03-10 12:10:12 +01:00
|
|
|
}
|
2021-03-12 10:52:07 +01:00
|
|
|
if ( $this->config->has( 'dcc_vault_enabled' ) && $this->config->get( 'dcc_vault_enabled' ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-03-10 12:10:12 +01:00
|
|
|
}
|
2020-09-14 14:17:39 +03:00
|
|
|
}
|