Skip to content
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

Feature/filter to modify actions at mollie piwoo 553 #955

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
"Mollie\\WooCommerceTests\\": "tests/php",
"Mollie\\WooCommerceTests\\Unit\\": "tests/php/Unit",
"Mollie\\WooCommerceTests\\Functional\\": "tests/php/Functional"
}
},
"files": [
"inc/api/order-functions.php"
]
},
"scripts": {
"check-coding-standards": "vendor/bin/phpcs --parallel=8 -s",
Expand Down
65 changes: 65 additions & 0 deletions documentation/Plugin-API-Functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
### Programmatically capture, refund, void, cancel, and ship Mollie orders.

With the Mollie API, you can programmatically capture, refund, void, cancel, and ship orders.
These actions are logged by the plugin.
Here are some examples of how to use these functions:

#### Capture an order
```php
use function Mollie\WooCommerce\Inc\Api\mollie_capture_order;

add_action('init', function () {
$order_id = 123;
$order = wc_get_order($order_id);
mollie_capture_order($order);
});
```

#### Refund an order
```php
use function Mollie\WooCommerce\Inc\Api\mollie_refund_order;

add_action('init', function () {
$order_id = 123;
$order = wc_get_order($order_id);
$refund = mollie_refund_order($order, 10.00, 'Refund reason');
// $refund is an instance of Mollie\Api\Resources\Refund
});
```

#### Void an order
```php
use function Mollie\WooCommerce\Inc\Api\mollie_void_order;

add_action('init', function () {
$order_id = 123;
$order = wc_get_order($order_id);
mollie_void_order($order);
});
```

#### Cancel an order
```php
use function Mollie\WooCommerce\Inc\Api\mollie_cancel_order;

add_action('init', function () {
$order_id = 123;
$order = wc_get_order($order_id);
mollie_cancel_order($order);
});
```

#### Ship an order
```php
use function Mollie\WooCommerce\Inc\Api\mollie_ship_order;

add_action('init', function () {
$order_id = 123;
$order = wc_get_order($order_id);
mollie_ship_order($order);
});
```




88 changes: 88 additions & 0 deletions inc/api/order-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* The API for operations with orders.
*
* @package WooCommerce\mollieCommerce\Api
*
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag
*/

declare(strict_types=1);

namespace Mollie\WooCommerce\Inc\Api;

use Mollie\Api\Resources\Refund;
use Mollie\WooCommerce\PluginApi\MolliePluginApi;
use WC_Order;

/**
* Captures the Mollie order.
* Logs the result of the operation.
*
* @param WC_Order $wc_order The WC order.
*
*/
function mollie_capture_order(WC_Order $wc_order): void
{

$mollieApi = MolliePluginApi::getInstance();
$mollieApi->captureOrder($wc_order);
}

/**
* Refunds the Mollie order.
*
* @param WC_Order $wc_order The WC order.
* @param float $amount The refund amount.
* @param string $reason The reason for the refund.
* @return \WP_Error|Refund The result of the refund operation.
*/
function mollie_refund_order(WC_Order $wc_order, float $amount, string $reason = '')
{

$mollieApi = MolliePluginApi::getInstance();
return $mollieApi->refundOrder($wc_order, $amount, $reason);
}

/**
* Voids the authorization.
* Logs the result of the operation.
*
* @param WC_Order $wc_order The WC order.
*
*/
function mollie_void_order(WC_Order $wc_order): void
{

$mollieApi = MolliePluginApi::getInstance();
$mollieApi->voidOrder($wc_order);
}

/**
* Cancels the order at Mollie and also in WooCommerce if was not already done.
* Logs the result of the operation.
*
* @param WC_Order $wc_order The WC order.
*/
function mollie_cancel_order(WC_Order $wc_order): void
{

$order_id = $wc_order->get_id();
$mollieApi = MolliePluginApi::getInstance();
$mollieApi->cancelOrder((string)$order_id);
}

/**
* Ship all order lines and capture an order at Mollie.
* Logs the result of the operation.
*
* @param WC_Order $wc_order The WC order.
*
*/
function mollie_ship_order(WC_Order $wc_order): void
{
$order_id = $wc_order->get_id();
$mollieApi = MolliePluginApi::getInstance();
$mollieApi->shipOrderAndCapture((string)$order_id);
}
8 changes: 7 additions & 1 deletion mollie-payments-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Mollie\WooCommerce\Activation\ActivationModule;
use Mollie\WooCommerce\Activation\ConstraintsChecker;
use Mollie\WooCommerce\Assets\AssetsModule;
use Mollie\WooCommerce\PluginApi\PluginApiModule;
use Mollie\WooCommerce\Shared\SharedModule;
use Mollie\WooCommerce\Gateway\GatewayModule;
use Mollie\WooCommerce\Gateway\Voucher\VoucherModule;
Expand Down Expand Up @@ -165,9 +166,14 @@ function initialize()
new PaymentModule(),
new MerchantCaptureModule(),
new UninstallModule(),
new PluginApiModule(),
];
$modules = apply_filters('mollie_wc_plugin_modules', $modules);
$bootstrap->boot(...$modules);
foreach ($modules as $module) {
$bootstrap->addModule($module);
}
$bootstrap->boot();

} catch (Throwable $throwable) {
handleException($throwable);
}
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<file>./src</file>
<exclude-pattern>./tests/</exclude-pattern>

<config name="testVersion" value="7.2"/>
<config name="testVersion" value="7.4"/>
<config name="ignore_warnings_on_exit" value="1"/>
<rule ref="Inpsyde">
<exclude name="NeutronStandard.StrictTypes.RequireStrictTypes.StrictTypes"/>
Expand Down
1 change: 1 addition & 0 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ public function buildPaymentMethod(
Surcharge $surchargeService,
array $apiMethod
) {

$transformedId = ucfirst($id);
$paymentMethodClassName = 'Mollie\\WooCommerce\\PaymentMethods\\' . $transformedId;
$paymentMethod = new $paymentMethodClassName(
Expand Down
70 changes: 4 additions & 66 deletions src/Gateway/MolliePaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

class MolliePaymentGateway extends WC_Payment_Gateway implements MolliePaymentGatewayI
{
const ORDER_ID_META_KEY = '_mollie_order_id';
/**
* @var bool
*/
Expand Down Expand Up @@ -708,70 +709,7 @@ protected function activePaymentObject($orderId, $useCache): Payment
*/
public function process_refund($order_id, $amount = null, $reason = '')
{
// Get the WooCommerce order
$order = wc_get_order($order_id);

// WooCommerce order not found
if (!$order) {
$error_message = "Could not find WooCommerce order $order_id.";

$this->logger->debug(
__METHOD__ . ' - ' . $error_message
);

return new WP_Error('1', $error_message);
}

// Check if there is a Mollie Payment Order object connected to this WooCommerce order
$payment_object_id = $this->paymentObject()->getActiveMollieOrderId(
$order_id
);

// If there is no Mollie Payment Order object, try getting a Mollie Payment Payment object
if (!$payment_object_id) {
$payment_object_id = $this->paymentObject()
->getActiveMolliePaymentId($order_id);
}

// Mollie Payment object not found
if (!$payment_object_id) {
$error_message = "Can\'t process refund. Could not find Mollie Payment object id for order $order_id.";

$this->logger->debug(
__METHOD__ . ' - ' . $error_message
);

return new WP_Error('1', $error_message);
}

try {
$payment_object = $this->paymentFactory
->getPaymentObject(
$payment_object_id
);
} catch (ApiException $exception) {
$exceptionMessage = $exception->getMessage();
$this->logger->debug($exceptionMessage);
return new WP_Error('error', $exceptionMessage);
}

if (!$payment_object) {
$error_message = "Can\'t process refund. Could not find Mollie Payment object data for order $order_id.";

$this->logger->debug(
__METHOD__ . ' - ' . $error_message
);

return new WP_Error('1', $error_message);
}

return $payment_object->refund(
$order,
$order_id,
$payment_object,
$amount,
$reason
);
return $this->paymentObject()->processRefund($order_id, $amount, $reason);
}

/**
Expand Down Expand Up @@ -1029,8 +967,8 @@ public function getSelectedIssuer(): ?string
*/
public function get_transaction_url($order): string
{
$isPaymentApi = substr($order->get_meta('_mollie_order_id', true), 0, 3) === 'tr_' ;
$resource = ($order->get_meta('_mollie_order_id', true) && !$isPaymentApi) ? 'orders' : 'payments';
$isPaymentApi = substr($order->get_meta(MolliePaymentGateway::ORDER_ID_META_KEY, true), 0, 3) === 'tr_' ;
$resource = ($order->get_meta(MolliePaymentGateway::ORDER_ID_META_KEY, true) && !$isPaymentApi) ? 'orders' : 'payments';

$this->view_transaction_url = 'https://my.mollie.com/dashboard/'
. $resource . '/%s?utm_source=woocommerce&utm_medium=plugin&utm_campaign=partner';
Expand Down
Loading
Loading