Skip to content

Use payment_source + experience_context #1491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: trunk
Choose a base branch
from
1 change: 1 addition & 0 deletions .ddev/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ web_environment:
- ADMIN_PASS=admin
- [email protected]
- WC_VERSION=7.7.2
- PPCP_FLAG_OLD_APPLICATION_CONTEXT=0

# Key features of ddev's config.yaml:

Expand Down
10 changes: 9 additions & 1 deletion modules/ppcp-api-client/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingPlans;
use WooCommerce\PayPalCommerce\ApiClient\Factory\BillingCycleFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ExperienceContextFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentPreferencesFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PlanFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ProductFactory;
Expand Down Expand Up @@ -268,6 +269,11 @@
'api.factory.application-context' => static function ( ContainerInterface $container ) : ApplicationContextFactory {
return new ApplicationContextFactory();
},
'api.factory.experience-context' => static function ( ContainerInterface $container ) : ExperienceContextFactory {
return new ExperienceContextFactory(
$container->get( 'wcgateway.settings' )
);
},
'api.factory.payment-token' => static function ( ContainerInterface $container ) : PaymentTokenFactory {
return new PaymentTokenFactory();
},
Expand Down Expand Up @@ -353,7 +359,9 @@
return new AddressFactory();
},
'api.factory.payment-source' => static function ( ContainerInterface $container ): PaymentSourceFactory {
return new PaymentSourceFactory();
return new PaymentSourceFactory(
$container->get( 'api.factory.experience-context' )
);
},
'api.factory.order' => static function ( ContainerInterface $container ): OrderFactory {
$purchase_unit_factory = $container->get( 'api.factory.purchase-unit' );
Expand Down
50 changes: 16 additions & 34 deletions modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PatchCollection;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentMethod;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems PaymentToken not needed here.

use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
Expand All @@ -27,7 +27,6 @@
use WooCommerce\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\ErrorResponse;
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet;
Expand Down Expand Up @@ -177,10 +176,9 @@ public function with_bn_code( string $bn_code ): OrderEndpoint {
* Creates an order.
*
* @param PurchaseUnit[] $items The purchase unit items for the order.
* @param string $shipping_preference One of ApplicationContext::SHIPPING_PREFERENCE_ values.
* @param string $shipping_preference One of ExperienceContext::SHIPPING_PREFERENCE_ values.
* @param Payer|null $payer The payer off the order.
* @param PaymentToken|null $payment_token The payment token.
* @param PaymentMethod|null $payment_method The payment method.
* @param PaymentSource|null $payment_source The payment source.
* @param string $paypal_request_id The paypal request id.
* @param string $user_action The user action.
*
Expand All @@ -191,15 +189,14 @@ public function create(
array $items,
string $shipping_preference,
Payer $payer = null,
PaymentToken $payment_token = null,
PaymentMethod $payment_method = null,
?PaymentSource $payment_source = null,
string $paypal_request_id = '',
string $user_action = ApplicationContext::USER_ACTION_CONTINUE
): Order {
$bearer = $this->bearer->bearer();
$data = array(
'intent' => ( $this->subscription_helper->cart_contains_subscription() || $this->subscription_helper->current_product_is_subscription() ) ? 'AUTHORIZE' : $this->intent,
'purchase_units' => array_map(
'intent' => ( $this->subscription_helper->cart_contains_subscription() || $this->subscription_helper->current_product_is_subscription() ) ? 'AUTHORIZE' : $this->intent,
'purchase_units' => array_map(
static function ( PurchaseUnit $item ) use ( $shipping_preference ): array {
$data = $item->to_array();

Expand All @@ -212,18 +209,15 @@ static function ( PurchaseUnit $item ) use ( $shipping_preference ): array {
},
$items
),
'application_context' => $this->application_context_repository
->current_context( $shipping_preference, $user_action )->to_array(),
);
if ( $payment_source ) {
$data['payment_source'] = $payment_source->to_array();
} else {
$data['application_context'] = $this->application_context_repository->current_context( $shipping_preference, $user_action )->to_array();
}
if ( $payer && ! empty( $payer->email_address() ) ) {
$data['payer'] = $payer->to_array();
}
if ( $payment_token ) {
$data['payment_source']['token'] = $payment_token->to_array();
}
if ( $payment_method ) {
$data['payment_method'] = $payment_method->to_array();
}

/**
* The filter can be used to modify the order creation request body data.
Expand Down Expand Up @@ -254,35 +248,23 @@ static function ( PurchaseUnit $item ) use ( $shipping_preference ): array {

$response = $this->request( $url, $args );
if ( is_wp_error( $response ) ) {
$error = new RuntimeException(
__( 'Could not create order.', 'woocommerce-paypal-payments' )
);
$this->logger->log(
'warning',
$error->getMessage(),
array(
'args' => $args,
'response' => $response,
)
$error = new RuntimeException( 'Could not create order.' );
$this->logger->warning(
$error->getMessage()
);
throw $error;
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 201 !== $status_code ) {
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
$error = new PayPalApiException(
$json,
$status_code
);
$this->logger->log(
'warning',
$this->logger->warning(
sprintf(
'Failed to create order. PayPal API response: %1$s',
$error->getMessage()
),
array(
'args' => $args,
'response' => $response,
)
);
throw $error;
Expand Down
22 changes: 0 additions & 22 deletions modules/ppcp-api-client/src/Entity/ApplicationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ class ApplicationContext {
*/
private $cancel_url;

/**
* The payment method.
*
* @var null
*/
private $payment_method;

/**
* ApplicationContext constructor.
*
Expand Down Expand Up @@ -136,9 +129,6 @@ public function __construct(
$this->landing_page = $landing_page;
$this->shipping_preference = $shipping_preference;
$this->user_action = $user_action;

// Currently we have not implemented the payment method.
$this->payment_method = null;
}

/**
Expand Down Expand Up @@ -204,15 +194,6 @@ public function cancel_url(): string {
return $this->cancel_url;
}

/**
* Returns the payment method.
*
* @return PaymentMethod|null
*/
public function payment_method() {
return $this->payment_method;
}

/**
* Returns the object as array.
*
Expand All @@ -223,9 +204,6 @@ public function to_array(): array {
if ( $this->user_action() ) {
$data['user_action'] = $this->user_action();
}
if ( $this->payment_method() ) {
$data['payment_method'] = $this->payment_method();
}
if ( $this->shipping_preference() ) {
$data['shipping_preference'] = $this->shipping_preference();
}
Expand Down
Loading