Add payer action to wc order (WIP)

This commit is contained in:
dinamiko 2022-07-05 15:49:59 +02:00
parent c27d49d86d
commit 93778b1539
6 changed files with 170 additions and 6 deletions

View file

@ -2233,7 +2233,11 @@ return array(
);
},
'wcgateway.oxxo-gateway' => static function( ContainerInterface $container ): OXXOGateway {
return new OXXOGateway();
return new OXXOGateway(
$container->get( 'api.endpoint.order' ),
$container->get( 'api.factory.purchase-unit' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'wcgateway.logging.is-enabled' => function ( ContainerInterface $container ) : bool {
$settings = $container->get( 'wcgateway.settings' );

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO;
use WC_Order;
use WooCommerce\PayPalCommerce\WcGateway\Helper\CheckoutHelper;
/**
@ -49,6 +50,17 @@ class OXXO {
return $methods;
}
);
add_filter(
'woocommerce_thankyou_order_received_text',
function( string $message, WC_Order $order ) {
$payer_action = $order->get_meta( 'ppcp_oxxo_payer_action' ) ?? '';
return $message . ' ' . $payer_action;
},
10,
2
);
}
/**

View file

@ -9,7 +9,12 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO;
use Psr\Log\LoggerInterface;
use WC_Payment_Gateway;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
/**
* Class PayUponInvoiceGateway.
@ -18,9 +23,38 @@ class OXXOGateway extends WC_Payment_Gateway {
const ID = 'ppcp-oxxo-gateway';
/**
* OXXOGateway constructor.
* The order endpoint.
*
* @var OrderEndpoint
*/
public function __construct() {
protected $order_endpoint;
/**
* The purchase unit factory.
*
* @var PurchaseUnitFactory
*/
protected $purchase_unit_factory;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* OXXOGateway constructor.
*
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
OrderEndpoint $order_endpoint,
PurchaseUnitFactory $purchase_unit_factory,
LoggerInterface $logger
) {
$this->id = self::ID;
$this->method_title = __( 'OXXO', 'woocommerce-paypal-payments' );
@ -40,6 +74,10 @@ class OXXOGateway extends WC_Payment_Gateway {
'process_admin_options',
)
);
$this->order_endpoint = $order_endpoint;
$this->purchase_unit_factory = $purchase_unit_factory;
$this->logger = $logger;
}
/**
@ -71,4 +109,63 @@ class OXXOGateway extends WC_Payment_Gateway {
),
);
}
/**
* Processes the order.
*
* @param int $order_id The WC order ID.
* @return array
*/
public function process_payment( $order_id ) {
$wc_order = wc_get_order( $order_id );
$wc_order->update_status( 'on-hold', __( 'Awaiting OXXO payment.', 'woocommerce-paypal-payments' ) );
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
try {
$order = $this->order_endpoint->create( array( $purchase_unit ) );
$payment_method = $this->order_endpoint->confirm_payment_source( $order->id() );
foreach ( $payment_method->links as $link ) {
if ( $link->rel === 'payer-action' ) {
$wc_order->add_meta_data( 'ppcp_oxxo_payer_action', $link->href );
$wc_order->save_meta_data();
}
}
} catch ( RuntimeException $exception ) {
$error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) {
$details = '';
foreach ( $exception->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
$error = $details;
}
$this->logger->error( $error );
wc_add_notice( $error, 'error' );
$wc_order->update_status(
'failed',
$error
);
return array(
'result' => 'failure',
'redirect' => wc_get_checkout_url(),
);
}
WC()->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
}
}

View file

@ -12,9 +12,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice;
use Psr\Log\LoggerInterface;
use RuntimeException;
use WC_Order;
use WC_Order_Item_Product;
use WC_Payment_Gateway;
use WC_Product;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;

View file

@ -103,7 +103,7 @@ class CheckoutHelper {
}
/**
* Ensures product is ready for PUI.
* Ensures product is neither downloadable nor virtual.
*
* @param WC_Product $product WC product.
* @return bool