add onApprove endpoint to store order in session

This commit is contained in:
David Remer 2020-04-08 16:23:33 +03:00
parent 7c2bc5a8da
commit 5824a40a08
10 changed files with 128 additions and 13 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,4 @@
import onApprove from "./onApprove";
class CartConfig {
@ -26,12 +26,9 @@ class CartConfig {
return data.data.id;
});
}
const onApprove = (data, actions) => {
return actions.redirect(this.config.redirect);
}
return {
createOrder,
onApprove,
onApprove:onApprove(this),
onError: (error) => {
this.errorHandler.message(error);
}

View file

@ -1,5 +1,6 @@
import ButtonsToggleListener from "./ButtonsToggleListener";
import Product from "./Product";
import onApprove from "./onApprove";
class SingleProductConfig {
constructor(
@ -29,12 +30,10 @@ class SingleProductConfig {
);
observer.init();
}
const onApprove = (data, actions) => {
return actions.redirect(this.config.redirect);
}
return {
createOrder: this.createOrder(),
onApprove,
onApprove: onApprove(this),
onError: (error) => {
this.errorHandler.message(error);
}
@ -123,5 +122,4 @@ class SingleProductConfig {
return this.formElement.classList.contains('grouped_form');
}
}
export default SingleProductConfig;

View file

@ -0,0 +1,22 @@
const onApprove = (context) => {
return (data, actions) => {
return fetch(context.config.ajax.approve_order.endpoint, {
method: 'POST',
body: JSON.stringify({
nonce: context.config.ajax.approve_order.nonce,
order_id:data.orderID
})
}).then((res)=>{
return res.json();
}).then((data)=>{
if (!data.success) {
//Todo: Error handling
return;
}
location.href = context.config.redirect;
});
}
}
export default onApprove;

View file

@ -5,6 +5,7 @@ namespace Inpsyde\PayPalCommerce\Button;
use Inpsyde\PayPalCommerce\Button\Assets\SmartButton;
use Dhii\Data\Container\ContainerInterface;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
@ -40,4 +41,10 @@ return [
$apiClient = $container->get('api.endpoint.order');
return new CreateOrderEndpoint($requestData, $repository, $apiClient);
},
'button.endpoint.approve-order' => function (ContainerInterface $container) : ApproveOrderEndpoint {
$requestData = $container->get('button.request-data');
$apiClient = $container->get('api.endpoint.order');
$sessionHandler = $container->get('session.handler');
return new ApproveOrderEndpoint($requestData, $apiClient, $sessionHandler);
},
];

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Assets;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
@ -82,6 +83,10 @@ class SmartButton
'endpoint' => home_url(\WC_AJAX::get_endpoint(CreateOrderEndpoint::ENDPOINT)),
'nonce' => wp_create_nonce(CreateOrderEndpoint::nonce()),
],
'approve_order' => [
'endpoint' => home_url(\WC_AJAX::get_endpoint(ApproveOrderEndpoint::ENDPOINT)),
'nonce' => wp_create_nonce(ApproveOrderEndpoint::nonce()),
],
],
'button' => [
'wrapper' => '#ppc-button',

View file

@ -6,6 +6,7 @@ namespace Inpsyde\PayPalCommerce\Button;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Inpsyde\PayPalCommerce\Button\Assets\SmartButton;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
@ -56,6 +57,17 @@ class ButtonModule implements ModuleInterface
}
);
add_action(
'wc_ajax_' . ApproveOrderEndpoint::ENDPOINT,
function () use ($container) {
$endpoint = $container->get('button.endpoint.approve-order');
/**
* @var ChangeCartEndpoint $endpoint
*/
$endpoint->handleRequest();
}
);
add_action(
'wc_ajax_' . CreateOrderEndpoint::ENDPOINT,
function () use ($container) {

View file

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
class ApproveOrderEndpoint implements EndpointInterface
{
const ENDPOINT = 'ppc-approve-order';
private $requestData;
private $sessionHandler;
private $apiEndpoint;
public function __construct(
RequestData $requestData,
OrderEndpoint $apiEndpoint,
SessionHandler $sessionHandler
) {
$this->requestData = $requestData;
$this->apiEndpoint = $apiEndpoint;
$this->sessionHandler = $sessionHandler;
}
public static function nonce(): string
{
return self::ENDPOINT . get_current_user_id();
}
public function handleRequest(): bool
{
try {
$data = $this->requestData->readRequest($this->nonce());
if (! isset($data['order_id'])) {
throw new RuntimeException(__("No order id given", "woocommerce-paypal-commerce-gateway"));
}
$order = $this->apiEndpoint->order($data['order_id']);
if (! $order) {
throw new RuntimeException(
sprintf(
// translators: %s is the id of the order.
__('Order %s not found.', 'woocommerce-paypal-commerce-gateway'),
$data['order_id']
)
);
}
if (! $order->status()->is(OrderStatus::APPROVED)) {
throw new RuntimeException(
sprintf(
// translators: %s is the id of the order.
__('Order %s is not approved yet.', 'woocommerce-paypal-commerce-gateway'),
$data['order_id']
)
);
}
$this->sessionHandler->replaceOrder($order);
wp_send_json_success($order);
return true;
} catch (\RuntimeException $error) {
wp_send_json_error($error->getMessage());
return false;
}
}
}

View file

@ -40,7 +40,7 @@ class CreateOrderEndpoint implements EndpointInterface
...$purchaseUnits
);
wp_send_json_success($order->toArray());
return false;
return true;
} catch (\RuntimeException $error) {
wp_send_json_error($error->getMessage());
return false;