Skip to content

Commit

Permalink
MOL-1244: fix refund manager for swag commercial plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Oct 19, 2023
1 parent 159513e commit 893a30d
Show file tree
Hide file tree
Showing 20 changed files with 935 additions and 559 deletions.
21 changes: 6 additions & 15 deletions src/Controller/Api/Order/ShippingControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Kiener\MolliePayments\Facade\MollieShipment;
use Kiener\MolliePayments\Traits\Api\ApiTrait;
use Mollie\Api\Resources\OrderLine;
use Mollie\Api\Resources\Shipment;
use Psr\Log\LoggerInterface;
Expand All @@ -18,6 +19,8 @@

class ShippingControllerBase extends AbstractController
{
use ApiTrait;

/**
* @var MollieShipment
*/
Expand Down Expand Up @@ -225,13 +228,8 @@ public function shipOrderLegacy(RequestDataBag $data, Context $context): JsonRes
* @param Context $context
* @return JsonResponse
*/
public function getShipOrderResponse(
string $orderId,
string $trackingCarrier,
string $trackingCode,
string $trackingUrl,
Context $context
): JsonResponse {
public function getShipOrderResponse(string $orderId, string $trackingCarrier, string $trackingCode, string $trackingUrl, Context $context): JsonResponse
{
try {
if (empty($orderId)) {
throw new \InvalidArgumentException('Missing Argument for Order ID!');
Expand All @@ -247,14 +245,7 @@ public function getShipOrderResponse(

return $this->shipmentToJson($shipment);
} catch (\Exception $e) {
$data = [
'orderId' => $orderId,
'trackingCarrier' => $trackingCarrier,
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl,
];

return $this->exceptionToJson($e, $data);
return $this->buildErrorResponse($e->getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,39 @@ export default class OrderAttributes {
this._paymentId = '';
this._swSubscriptionId = '';
this._creditCardAttributes = null;
this._paymentRef = null;

if (orderEntity === null) {
return;
}

const customFields = orderEntity.customFields;
this.customFields = orderEntity.customFields;

if (customFields === null || customFields === undefined) {
if (this.customFields === null || this.customFields === undefined) {
return;
}

if (customFields.mollie_payments === undefined || customFields.mollie_payments === null) {
if (this.customFields.mollie_payments === undefined || this.customFields.mollie_payments === null) {
return;
}

const mollieData = customFields.mollie_payments;
const mollieData = this.customFields.mollie_payments;

this._orderId = this._convertString(mollieData['order_id']);
this._paymentId = this._convertString(mollieData['payment_id']);
this._swSubscriptionId = this._convertString(mollieData['swSubscriptionId']);
this._paymentRef = this._convertString(mollieData['third_party_payment_id']);
this._creditCardAttributes = new CreditcardAttributes(mollieData);
}

/**
*
* @returns {boolean}
*/
isMollieOrder() {
return (this.customFields !== null && 'mollie_payments' in this.customFields);
}

/**
*
* @returns {null|CreditcardAttributes|*}
Expand All @@ -59,6 +69,30 @@ export default class OrderAttributes {
return this._paymentId;
}

/**
*
* @returns {string|*|null}
*/
getMollieID() {
if (this.getOrderId() !== '') {
return this.getOrderId();
}

if (this.getPaymentId() !== '') {
return this.getPaymentId();
}

return null;
}

/**
*
* @returns {boolean}
*/
isSubscription() {
return (this.getSwSubscriptionId() !== '');
}

/**
*
* @returns {string|*}
Expand All @@ -67,6 +101,14 @@ export default class OrderAttributes {
return this._swSubscriptionId;
}

/**
*
* @returns {string}
*/
getPaymentRef() {
return this._paymentRef;
}

/**
*
* @param value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export default class RefundManager {

/**
*
* @param configService
* @param acl
*/
constructor(configService, acl) {
this._configService = configService;
this._acl = acl;
}

/**
* Gets if the refund manager is available
* @returns {boolean}
*/
async isRefundManagerAvailable(salesChannelId) {

const aclAllowed = this._acl.can('mollie_refund_manager:read');

if (!aclAllowed) {
return false;
}

let refundManagerPossible = false;

await this._configService.getRefundManagerConfig(salesChannelId).then((response) => {
refundManagerPossible = response.enabled;
});

return refundManagerPossible;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ Component.register('mollie-refund-manager', {
}
},


// ---------------------------------------------------------------------------------------------------------
// <editor-fold desc="ORDER FORM">
// ---------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import OrderAttributes from '../../../../core/models/OrderAttributes';

export default class MollieShipping {


/**
*
* @param shippingService
*/
constructor(shippingService) {
this._shippingService = shippingService;
}

/**
*
* @param order
* @returns {boolean}
*/
async isShippingPossible(order) {

const orderAttributes = new OrderAttributes(order);

// this can happen on subscription renewals...they have no order id
// and therefore the order cannot be shipped
if (orderAttributes.getOrderId() === '') {
return false;
}

const items = await this.getShippableItems(order);

for (let i = 0; i < items.length; i++) {
const lineItem = items[i];

if (lineItem.quantity > 0) {
return true;
}
}

return false;
}

/**
*
* @param order
* @returns {Promise<{quantity: *, label: *}[]>}
*/
async getShippableItems(order) {
// load the already shipped items
// so that we can calculate what is left to be shipped
await this._shippingService
.status({
orderId: order.id,
})
.then((response) => {
this.shippedLineItems = response;
});

const finalItems = [];

for (let i = 0; i < order.lineItems.length; i++) {
const lineItem = order.lineItems[i];

finalItems.push({
label: lineItem.label,
quantity: this._shippableQuantity(lineItem),
});
}

return finalItems;
}

/**
*
* @param item
* @returns {*|number}
* @private
*/
_shippableQuantity(item) {

if (this.shippedLineItems === null || this.shippedLineItems === undefined) {
return 0;
}

const itemShippingStatus = this.shippedLineItems[item.id];

if (itemShippingStatus === null || itemShippingStatus === undefined) {
return 0;
}

return itemShippingStatus.quantityShippable;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default class MollieShippingEvents {

/**
*
* @returns {string}
*/
static get EventShippedOrder() {
return 'mollie-shipped-order';
}

}
Loading

0 comments on commit 893a30d

Please sign in to comment.