WIP authorize order from admin panel

This commit is contained in:
Mészáros Róbert 2020-04-13 12:39:34 +03:00
parent 846d351f4a
commit 35cceed231
2 changed files with 49 additions and 1 deletions

View file

@ -105,6 +105,27 @@ class OrderEndpoint
return $order;
}
public function authorize(string $orderId) {
$bearer = $this->bearer->bearer();
$url = trailingslashit($this->host) . 'v2/checkout/orders/' . $orderId . '/authorize';
$args = [
'headers' => [
'Authorization' => 'Bearer ' . $bearer,
'Content-Type' => 'application/json',
'Prefer' => 'return=representation',
],
];
$response = wp_remote_post($url, $args);
if (is_wp_error($response)) {
throw new RuntimeException(__('Could not authorize order.', 'woocommerce-paypal-commerce-gateway'));
}
if (wp_remote_retrieve_response_code($response) !== 201) {
throw new RuntimeException(__('Could not capture order.', 'woocommerce-paypal-commerce-gateway'));
}
return $orderId;
}
public function order(string $id) : Order
{
$bearer = $this->bearer->bearer();

View file

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\Exception\ModuleExceptionInterface;
use Dhii\Modular\Module\ModuleInterface;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
@ -42,5 +42,32 @@ class WcGatewayModule implements ModuleInterface
return $disabler->handler((array) $methods);
}
);
add_filter(
'woocommerce_order_actions',
function ($orderActions) : array {
$orderActions['ppcp_authorize_order'] = __(
'Authorize PayPal Order',
'woocommerce-paypal-gateway'
);
return $orderActions;
}
);
add_action(
'woocommerce_order_action_ppcp_authorize_order',
function (\WC_Order $wcOrder) use ($container) {
$payPalOrderId = get_post_meta($wcOrder->get_id(), '_paypal_order_id', true);
/**
* @var OrderEndpoint $orderEndpoint
*/
$orderEndpoint = $container->get('api.endpoint.order');
$orderEndpoint->authorize($payPalOrderId);
$wcOrder->update_status('processing');
}
);
}
}