Skip to content
Snippets Groups Projects
Commit 7792120c authored by mattwire's avatar mattwire Committed by mattwire
Browse files

Stripe does not refund fees

parent f8ae05a2
No related branches found
No related tags found
1 merge request!188Refund fixes
...@@ -928,7 +928,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment { ...@@ -928,7 +928,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$refundParams['amount'] = $this->getAmount($params); $refundParams['amount'] = $this->getAmount($params);
try { try {
$refund = $this->stripeClient->refunds->create($refundParams); $refund = $this->stripeClient->refunds->create($refundParams);
$fee = $this->getFeeFromBalanceTransaction($refund['balance_transaction'], $refund['currency']); // Stripe does not refund fees - see https://support.stripe.com/questions/understanding-fees-for-refunded-payments
// $fee = $this->getFeeFromBalanceTransaction($refund['balance_transaction'], $refund['currency']);
} }
catch (Exception $e) { catch (Exception $e) {
$this->handleError($e->getCode(), $e->getMessage()); $this->handleError($e->getCode(), $e->getMessage());
...@@ -960,8 +961,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment { ...@@ -960,8 +961,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
'refund_trxn_id' => $refund->id, 'refund_trxn_id' => $refund->id,
'refund_status_id' => $refundStatus, 'refund_status_id' => $refundStatus,
'refund_status' => $refundStatusName, 'refund_status' => $refundStatusName,
'processor_result' => $refund->jsonSerialize(), 'fee_amount' => 0,
'fee_amount' => $fee ?? NULL,
]; ];
return $refundParams; return $refundParams;
} }
......
...@@ -602,7 +602,7 @@ class CRM_Core_Payment_StripeIPN { ...@@ -602,7 +602,7 @@ class CRM_Core_Payment_StripeIPN {
} }
/** /**
* Process the received event in CiviCRM * Process the charge.refunded event from Stripe
* *
* @return bool * @return bool
* @throws \CRM_Core_Exception * @throws \CRM_Core_Exception
...@@ -614,15 +614,21 @@ class CRM_Core_Payment_StripeIPN { ...@@ -614,15 +614,21 @@ class CRM_Core_Payment_StripeIPN {
// Cancelling an uncaptured paymentIntent triggers charge.refunded but we don't want to process that // Cancelling an uncaptured paymentIntent triggers charge.refunded but we don't want to process that
if (empty(CRM_Stripe_Api::getObjectParam('captured', $this->getData()->object))) { if (empty(CRM_Stripe_Api::getObjectParam('captured', $this->getData()->object))) {
return TRUE; return TRUE;
};
// This charge was actually captured, so record the refund in CiviCRM
if (!$this->setInfo()) {
return TRUE;
} }
// This gives us the refund date + reason code // This gives us the refund date + reason code
$refunds = $this->_paymentProcessor->stripeClient->refunds->all(['charge' => $this->charge_id, 'limit' => 1]); $refunds = $this->_paymentProcessor->stripeClient->refunds->all(['charge' => $this->charge_id, 'limit' => 1]);
$refund = $refunds->data[0]; $refund = $refunds->data[0];
// Stripe does not refund fees - see https://support.stripe.com/questions/understanding-fees-for-refunded-payments
// This gets the fee refunded
// $this->fee = $this->getPaymentProcessor()->getFeeFromBalanceTransaction($refund->balance_transaction, $this->retrieve('currency', 'String', FALSE));
// This gives us the actual amount refunded
$amountRefunded = CRM_Stripe_Api::getObjectParam('amount_refunded', $this->getData()->object);
// Get the CiviCRM contribution that matches the Stripe metadata we have from the event
$this->getContribution();
if (isset($this->contribution['payments'])) { if (isset($this->contribution['payments'])) {
foreach ($this->contribution['payments'] as $payment) { foreach ($this->contribution['payments'] as $payment) {
if ($payment['trxn_id'] === $refund->id) { if ($payment['trxn_id'] === $refund->id) {
...@@ -635,17 +641,12 @@ class CRM_Core_Payment_StripeIPN { ...@@ -635,17 +641,12 @@ class CRM_Core_Payment_StripeIPN {
} }
} }
// This gets the fee refunded
$this->fee = $this->getPaymentProcessor()->getFeeFromBalanceTransaction($refund->balance_transaction, $this->retrieve('currency', 'String', FALSE));
// This gives us the actual amount refunded
$amountRefunded = CRM_Stripe_Api::getObjectParam('amount_refunded', $this->getData()->object);
$refundParams = [ $refundParams = [
'contribution_id' => $this->contribution['id'], 'contribution_id' => $this->contribution['id'],
'total_amount' => 0 - abs($amountRefunded), 'total_amount' => 0 - abs($amountRefunded),
'trxn_date' => date('YmdHis', $refund->created), 'trxn_date' => date('YmdHis', $refund->created),
'trxn_result_code' => $refund->reason, 'trxn_result_code' => $refund->reason,
'fee_amount' => 0 - abs($this->fee), 'fee_amount' => 0,
'trxn_id' => $refund->id, 'trxn_id' => $refund->id,
'order_reference' => $this->invoice_id ?? NULL, 'order_reference' => $this->invoice_id ?? NULL,
]; ];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment