Skip to content

Commit

Permalink
Release 4.0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
edgaraswallee committed Feb 7, 2022
1 parent fbb7262 commit 911c600
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 4.0.24
- Added refunds by amount

# 4.0.23
- Fixed cart recreate function for custom products

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG_de-DE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#4.0.24
- Rückerstattungen nach Betrag hinzugefügt

# 4.0.23
- Korrigierte Warenkorb-Neuerstellungsfunktion für benutzerdefinierte Produkte

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ tail -f var/log/postfinancecheckout_payment*.log

## Documentation

[Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/shopware-6/4.0.23/docs/en/documentation.html)
[Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/shopware-6/4.0.24/docs/en/documentation.html)

## License

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@
"postfinancecheckout/sdk": "3.0.1"
},
"type": "shopware-platform-plugin",
"version": "4.0.23"
"version": "4.0.24"
}
2 changes: 1 addition & 1 deletion docs/en/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h2>Documentation</h2> </div>
</a>
</li>
<li>
<a href="https://github.com/pfpayments/shopware-6/releases/tag/4.0.23/">
<a href="https://github.com/pfpayments/shopware-6/releases/tag/4.0.24/">
Source
</a>
</li>
Expand Down
28 changes: 28 additions & 0 deletions src/Core/Api/Refund/Controller/RefundController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,32 @@ public function createRefund(Request $request, Context $context): Response

return new Response(null, Response::HTTP_NO_CONTENT);
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Shopware\Core\Framework\Context $context
* @return \Symfony\Component\HttpFoundation\Response
* @throws \PostFinanceCheckout\Sdk\ApiException
* @throws \PostFinanceCheckout\Sdk\Http\ConnectionException
* @throws \PostFinanceCheckout\Sdk\VersioningException
* @Route(
* "/api/_action/postfinancecheckout/refund/create-refund-by-amount/",
* name="api.action.postfinancecheckout.refund.create.refund.by.amount",
* methods={"POST"}
* )
*/
public function createRefundByAmount(Request $request, Context $context): Response
{
$salesChannelId = $request->request->get('salesChannelId');
$transactionId = $request->request->get('transactionId');
$refundableAmount = $request->request->get('refundableAmount');

$settings = $this->settingsService->getSettings($salesChannelId);
$apiClient = $settings->getApiClient();

$transaction = $apiClient->getTransactionService()->read($settings->getSpaceId(), $transactionId);
$this->refundService->createRefundByAmount($transaction, $refundableAmount, $context);

return new Response(null, Response::HTTP_NO_CONTENT);
}
}
35 changes: 35 additions & 0 deletions src/Core/Api/Refund/Service/RefundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ public function create(Transaction $transaction, Context $context, ?string $line
return null;
}

/**
* The pay function will be called after the customer completed the order.
* Allows to process the order and store additional information.
*
* A redirect to the url will be performed
*
* @param \PostFinanceCheckout\Sdk\Model\Transaction $transaction
* @param float $refundableAmount
* @param \Shopware\Core\Framework\Context $context
*
* @return \PostFinanceCheckout\Sdk\Model\Refund|null
* @throws \Exception
*/
public function createRefundByAmount(Transaction $transaction, float $refundableAmount, Context $context): ?Refund
{
try {
$transactionEntity = $this->getTransactionEntityByTransactionId($transaction->getId(), $context);
$settings = $this->settingsService->getSettings($transactionEntity->getSalesChannel()->getId());
$apiClient = $settings->getApiClient();
$refundPayloadClass = new RefundPayload();
$refundPayloadClass->setLogger($this->logger);

$refundPayload = $refundPayloadClass->getByAmount($transaction, $refundableAmount);

if (!is_null($refundPayload)) {
$refund = $apiClient->getRefundService()->refund($settings->getSpaceId(), $refundPayload);
$this->upsert($refund, $context);
return $refund;
}
} catch (\Exception $exception) {
$this->logger->critical($exception->getMessage());
}
return null;
}

/**
* Get transaction entity by PostFinanceCheckout transaction id
*
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Api/WebHooks/Controller/WebHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public function updateRefund(WebHookRequest $callBackData, Context $context): Re
) {
$transactionByOrderTransactionId = $this->transactionService->getByOrderTransactionId($orderTransactionId, $context);
$totalRefundedAmount = $this->getTotalRefundedAmount($transactionByOrderTransactionId->getTransactionId(), $context);
if ($totalRefundedAmount == $orderTransaction->getAmount()->getTotalPrice()) {
if (floatval($orderTransaction->getAmount()->getTotalPrice()) - $totalRefundedAmount <= 0) {
$this->orderTransactionStateHandler->refund($orderTransactionId, $context);
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/Core/Util/Payload/RefundPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ public function get(Transaction $transaction, string $lineItemId, int $quantity)
return null;
}

/**
* @param \PostFinanceCheckout\Sdk\Model\Transaction $transaction
* @param float $amount
* @return \PostFinanceCheckout\Sdk\Model\RefundCreate|null
* @throws \Exception
*/
public function getByAmount(Transaction $transaction, float $amount): ?RefundCreate
{
if (
($transaction->getState() == TransactionState::FULFILL) &&
($amount <= floatval($transaction->getAuthorizationAmount()))
) {
$refund = (new RefundCreate())
->setAmount(self::round($amount))
->setTransaction($transaction->getId())
->setMerchantReference($this->fixLength($transaction->getMerchantReference(), 100))
->setExternalId($this->fixLength(uniqid('refund_', true), 100))
->setType(RefundType::MERCHANT_INITIATED_ONLINE);

if (!$refund->valid()) {
$this->logger->critical('Refund payload invalid:', $refund->listInvalidProperties());
throw new InvalidPayloadException('Refund payload invalid:' . json_encode($refund->listInvalidProperties()));
}

return $refund;
}

return null;
}

/**
* @param array $lineItems
* @param string $uniqueId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ class PostFinanceCheckoutRefundService extends ApiService {
return ApiService.handleResponse(response);
});
}

/**
* Refund a transaction
*
* @param {String} salesChannelId
* @param {int} transactionId
* @param {float} refundableAmount
* @return {*}
*/
createRefundByAmount(salesChannelId, transactionId, refundableAmount) {

const headers = this.getBasicHeaders();
const apiRoute = `${Shopware.Context.api.apiPath}/_action/${this.getApiBasePath()}/refund/create-refund-by-amount/`;

return this.httpClient.post(
apiRoute,
{
salesChannelId: salesChannelId,
transactionId: transactionId,
refundableAmount: refundableAmount
},
{
headers: headers
}
).then((response) => {
return ApiService.handleResponse(response);
});
}
}

export default PostFinanceCheckoutRefundService;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% block postfinancecheckout_order_action_refund_by_amount %}
<sw-modal variant="small"
:title="$tc(`postfinancecheckout-order.modal.title.refund`)"
@modal-close="$emit('modal-close')">

{% block postfinancecheckout_order_action_refund_amount_by_amount %}
<sw-number-field v-model="refundAmount"
:max="refundableAmount"
:min="0"
:value="refundableAmount"
:label="$tc('postfinancecheckout-order.refund.refundAmount.label')"
:suffix="currency">
</sw-number-field>
{% endblock %}

{% block postfinancecheckout_order_action_refund_confirm_button_by_amount %}
<template #modal-footer>
<sw-button variant="primary" @click="refundByAmount()">
{{ $tc('postfinancecheckout-order.refundAction.confirmButton.text') }}
</sw-button>
</template>
{% endblock %}

<sw-loader v-if="isLoading"></sw-loader>
</sw-modal>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* global Shopware */

import template from './index.html.twig';

const {Component, Mixin, Filter, Utils} = Shopware;

Component.register('postfinancecheckout-order-action-refund-by-amount', {
template,

inject: ['PostFinanceCheckoutRefundService'],

mixins: [
Mixin.getByName('notification')
],

props: {
transactionData: {
type: Object,
required: true
},

orderId: {
type: String,
required: true
}
},

data() {
return {
isLoading: true,
currency: this.transactionData.transactions[0].currency,
refundAmount: 0,
refundableAmount: 0,
};
},

computed: {
dateFilter() {
return Filter.getByName('date');
}
},

created() {
this.createdComponent();
},

methods: {
createdComponent() {
this.isLoading = false;
this.currency = this.transactionData.transactions[0].currency;
this.refundAmount = Number(this.transactionData.transactions[0].amountIncludingTax);
this.refundableAmount = Number(this.transactionData.transactions[0].amountIncludingTax);
},

refundByAmount() {
this.isLoading = true;
this.PostFinanceCheckoutRefundService.createRefundByAmount(
this.transactionData.transactions[0].metaData.salesChannelId,
this.transactionData.transactions[0].id,
this.refundAmount
).then(() => {
this.createNotificationSuccess({
title: this.$tc('postfinancecheckout-order.refundAction.successTitle'),
message: this.$tc('postfinancecheckout-order.refundAction.successMessage')
});
this.isLoading = false;
this.$emit('modal-close');
this.$nextTick(() => {
this.$router.replace(`${this.$route.path}?hash=${Utils.createId()}`);
});
}).catch((errorResponse) => {
try {
this.createNotificationError({
title: errorResponse.response.data.errors[0].title,
message: errorResponse.response.data.errors[0].detail,
autoClose: false
});
} catch (e) {
this.createNotificationError({
title: errorResponse.title,
message: errorResponse.message,
autoClose: false
});
} finally {
this.isLoading = false;
this.$emit('modal-close');
this.$nextTick(() => {
this.$router.replace(`${this.$route.path}?hash=${Utils.createId()}`);
});
}
});
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
<template #grid>
{% block postfinancecheckout_order_actions_section %}
<sw-card-section secondary slim>
{% block postfinancecheckout_order_transaction_refunds_action_button %}
<sw-button
variant="primary"
size="small"
:disabled="transaction.state != 'FULFILL' || refundableAmount <= 0"
@click="spawnModal('refundByAmount')">
{{ $tc('postfinancecheckout-order.buttons.label.refund') }}
</sw-button>
{% endblock %}
{% block postfinancecheckout_order_transaction_completion_action_button %}
<sw-button
variant="primary"
Expand Down Expand Up @@ -118,6 +127,15 @@
@modal-close="closeModal">
</postfinancecheckout-order-action-refund>
{% endblock %}
{% block postfinancecheckout_order_actions_modal_refund_by_amount %}
<postfinancecheckout-order-action-refund-by-amount
v-if="modalType === 'refundByAmount'"
:orderId="orderId"
:transactionData="transactionData"
:lineItems="lineItems"
@modal-close="closeModal">
</postfinancecheckout-order-action-refund-by-amount>
{% endblock %}
{% block postfinancecheckout_order_actions_modal_completion%}
<postfinancecheckout-order-action-completion
v-if="modalType === 'completion'"
Expand Down
Loading

0 comments on commit 911c600

Please sign in to comment.