public function Manual::refundPayment in Commerce Core 8.2
Refunds the given payment.
Parameters
\Drupal\commerce_payment\Entity\PaymentInterface $payment: The payment to refund.
\Drupal\commerce_price\Price $amount: The amount to refund. If NULL, defaults to the entire payment amount.
Throws
\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the transaction fails for any reason.
Overrides SupportsRefundsInterface::refundPayment
File
- modules/
payment/ src/ Plugin/ Commerce/ PaymentGateway/ Manual.php, line 220
Class
- Manual
- Provides the Manual payment gateway.
Namespace
Drupal\commerce_payment\Plugin\Commerce\PaymentGatewayCode
public function refundPayment(PaymentInterface $payment, Price $amount = NULL) {
$this
->assertPaymentState($payment, [
'completed',
'partially_refunded',
]);
// If not specified, refund the entire amount.
$amount = $amount ?: $payment
->getAmount();
$this
->assertRefundAmount($payment, $amount);
$old_refunded_amount = $payment
->getRefundedAmount();
$new_refunded_amount = $old_refunded_amount
->add($amount);
if ($new_refunded_amount
->lessThan($payment
->getAmount())) {
$payment->state = 'partially_refunded';
}
else {
$payment->state = 'refunded';
}
$payment
->setRefundedAmount($new_refunded_amount);
$payment
->save();
}