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

MOL-1244: fix refund manager and shipping for swag commercial plugin #641

Merged
Merged
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
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
Loading