The Refund feature allows merchants to refund generic transactions. Supports full refunds, partial refunds, and refunding specific line items.
The Refund process involves:
- Validation: Ensures the refund amount does not exceed the remaining authorized amount. Line item quantities and amounts are also validated.
- Creation: A refund is created via the PostFinanceCheckout SDK.
- Result: A strict Domain Entity
Refundis returned, containing state and ID.
- RefundService: The main entry point. Validates and executes refunds.
- RefundContext: A DTO containing all necessary data to create a refund (Amount, Line Items, Type, etc.).
- RefundGatewayInterface: Abstraction for the underlying API interaction.
When performing a partial refund on specific line items, it is crucial to understand how the Reduction Amount is calculated.
The field amount in the lineItems array of RefundContext corresponds to the Unit Price Reduction, NOT the total reduction for that item.
Formula:
Total Reduction = (Quantity Returned * Unit Price) + (Remaining Quantity * Unit Price Reduction)
Example: You sold 2 items of "Swiss Watch" at 150.00 each. You want to refund 20.00 total as a discount/adjustment, without returning any items.
- Quantity Returned: 0
- Remaining Quantity: 2
- Desired Total Refund: 20.00
Calculation for Unit Price Reduction:
20.00 = (0 * 150.00) + (2 * X) -> 20.00 = 2X -> X = 10.00
So you must pass quantity: 0 and amount: 10.00 in the line item context.
$context = new RefundContext(
transactionId: 123,
amount: 20.00, // Total Refund Amount
// ...
lineItems: [
[
'uniqueId' => 'sku-123',
'quantity' => 0, // Returning 0 physical items
'amount' => 10.00 // Reducing unit price by 10.00 (x 2 items = 20.00)
]
]
);use PostFinanceCheckout\PluginCore\Refund\RefundService;
use PostFinanceCheckout\PluginCore\Refund\RefundContext;
use PostFinanceCheckout\PluginCore\Refund\Type;
// ... instantiate services ...
$context = new RefundContext(
transactionId: 123,
amount: 50.00,
merchantReference: 'refund-ref-1',
type: Type::MERCHANT_INITIATED_ONLINE,
lineItems: [] // Optional: For partial specific items
);
try {
$refund = $refundService->createRefund($spaceId, $context);
echo "Refund created: " . $refund->id;
} catch (InvalidRefundException $e) {
echo "Validation failed: " . $e->getMessage();
}You can retrieve all refunds associated with a transaction using getRefunds:
$refunds = $refundService->getRefunds($spaceId, $transactionId);
foreach ($refunds as $refund) {
echo "Refund ID: " . $refund->id;
echo "State: " . $refund->state->value;
}See the example directory for a fully working CLI script that demonstrates:
- Full Refund
- Validation Error (Amount too high)
- Partial Refund