Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Stripe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Extensions
Stripe
Commits
f8ae05a2
Commit
f8ae05a2
authored
2 years ago
by
mattwire
Committed by
mattwire
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix duplicate refund payments showing in CiviCRM
parent
df278c6a
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!188
Refund fixes
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CRM/Core/Payment/Stripe.php
+34
-1
34 additions, 1 deletion
CRM/Core/Payment/Stripe.php
CRM/Core/Payment/StripeIPN.php
+69
-64
69 additions, 64 deletions
CRM/Core/Payment/StripeIPN.php
CRM/Stripe/Api.php
+1
-0
1 addition, 0 deletions
CRM/Stripe/Api.php
with
104 additions
and
65 deletions
CRM/Core/Payment/Stripe.php
+
34
−
1
View file @
f8ae05a2
...
...
@@ -928,6 +928,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$refundParams
[
'amount'
]
=
$this
->
getAmount
(
$params
);
try
{
$refund
=
$this
->
stripeClient
->
refunds
->
create
(
$refundParams
);
$fee
=
$this
->
getFeeFromBalanceTransaction
(
$refund
[
'balance_transaction'
],
$refund
[
'currency'
]);
}
catch
(
Exception
$e
)
{
$this
->
handleError
(
$e
->
getCode
(),
$e
->
getMessage
());
...
...
@@ -958,8 +959,9 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$refundParams
=
[
'refund_trxn_id'
=>
$refund
->
id
,
'refund_status_id'
=>
$refundStatus
,
'refund_status
_name
'
=>
$refundStatusName
,
'refund_status'
=>
$refundStatusName
,
'processor_result'
=>
$refund
->
jsonSerialize
(),
'fee_amount'
=>
$fee
??
NULL
,
];
return
$refundParams
;
}
...
...
@@ -1376,4 +1378,35 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
}
throw
new
PaymentProcessorException
(
$errorMessage
,
$errorCode
);
}
/**
* Get the Fee charged by Stripe from the "balance transaction".
* If the transaction is declined, there won't be a balance_transaction_id.
* We also have to do currency conversion here in case Stripe has converted it internally.
*
* @param string $balanceTransactionID
*
* @return float
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public
function
getFeeFromBalanceTransaction
(
string
$balanceTransactionID
,
$currency
):
float
{
$fee
=
0.0
;
if
(
$balanceTransactionID
)
{
try
{
$balanceTransaction
=
$this
->
stripeClient
->
balanceTransactions
->
retrieve
(
$balanceTransactionID
);
if
(
$currency
!==
$balanceTransaction
->
currency
&&
!
empty
(
$balanceTransaction
->
exchange_rate
))
{
$fee
=
CRM_Stripe_Api
::
currencyConversion
(
$balanceTransaction
->
fee
,
$balanceTransaction
->
exchange_rate
,
$currency
);
}
else
{
// We must round to currency precision otherwise payments may fail because Contribute BAO saves but then
// can't retrieve because it tries to use the full unrounded number when it only got saved with 2dp.
$fee
=
round
(
$balanceTransaction
->
fee
/
100
,
CRM_Utils_Money
::
getCurrencyPrecision
(
$currency
));
}
}
catch
(
Exception
$e
)
{
throw
new
\Civi\Payment\Exception\PaymentProcessorException
(
"Error retrieving balanceTransaction
{
$balanceTransactionID
}
. "
.
$e
->
getMessage
());
}
}
return
$fee
;
}
}
This diff is collapsed.
Click to expand it.
CRM/Core/Payment/StripeIPN.php
+
69
−
64
View file @
f8ae05a2
...
...
@@ -355,7 +355,16 @@ class CRM_Core_Payment_StripeIPN {
$return
=
(
object
)
[
'message'
=>
''
,
'ok'
=>
FALSE
,
'exception'
=>
NULL
];
try
{
$this
->
setInputParameters
();
$return
->
ok
=
$this
->
processEventType
();
switch
(
$this
->
eventType
)
{
case
'charge.refunded'
:
$return
->
ok
=
TRUE
;
$return
->
message
=
$this
->
doChargeRefunded
();
break
;
default
:
$return
->
ok
=
$this
->
processEventType
();
}
}
catch
(
Exception
$e
)
{
if
(
$this
->
exceptionOnFailure
)
{
...
...
@@ -537,45 +546,7 @@ class CRM_Core_Payment_StripeIPN {
$this
->
updateContributionFailed
(
$params
);
return
TRUE
;
case
'charge.refunded'
:
// 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
)))
{
return
TRUE
;
};
// This charge was actually captured, so record the refund in CiviCRM
if
(
!
$this
->
setInfo
())
{
return
TRUE
;
}
// This gives us the actual amount refunded
$amountRefunded
=
CRM_Stripe_Api
::
getObjectParam
(
'amount_refunded'
,
$this
->
getData
()
->
object
);
// This gives us the refund date + reason code
$refunds
=
$this
->
_paymentProcessor
->
stripeClient
->
refunds
->
all
([
'charge'
=>
$this
->
charge_id
,
'limit'
=>
1
]);
// This gets the fee refunded
$this
->
setBalanceTransactionDetails
(
$refunds
->
data
[
0
]
->
balance_transaction
);
$params
=
[
'contribution_id'
=>
$this
->
contribution
[
'id'
],
'total_amount'
=>
0
-
abs
(
$amountRefunded
),
'trxn_date'
=>
date
(
'YmdHis'
,
$refunds
->
data
[
0
]
->
created
),
'trxn_result_code'
=>
$refunds
->
data
[
0
]
->
reason
,
'fee_amount'
=>
0
-
abs
(
$this
->
fee
),
'trxn_id'
=>
$this
->
charge_id
,
'order_reference'
=>
$this
->
invoice_id
??
NULL
,
];
if
(
isset
(
$this
->
contribution
[
'payments'
]))
{
$refundStatusID
=
(
int
)
CRM_Core_PseudoConstant
::
getKey
(
'CRM_Contribute_BAO_Contribution'
,
'contribution_status_id'
,
'Refunded'
);
foreach
(
$this
->
contribution
[
'payments'
]
as
$payment
)
{
if
(((
int
)
$payment
[
'status_id'
]
===
$refundStatusID
)
&&
((
float
)
$payment
[
'total_amount'
]
===
$params
[
'total_amount'
]))
{
// Already refunded
return
TRUE
;
}
}
// This triggers the financial transactions/items to be updated correctly.
$params
[
'cancelled_payment_id'
]
=
reset
(
$this
->
contribution
[
'payments'
])[
'id'
];
}
$this
->
updateContributionRefund
(
$params
);
return
TRUE
;
// case 'charge.refunded': Handled via doChargeRefunded();
case
'charge.succeeded'
:
// For a recurring contribution we can process charge.succeeded once we receive the event with an invoice ID.
...
...
@@ -630,6 +601,63 @@ class CRM_Core_Payment_StripeIPN {
return
TRUE
;
}
/**
* Process the received event in CiviCRM
*
* @return bool
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
private
function
doChargeRefunded
()
{
// 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
)))
{
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
$refunds
=
$this
->
_paymentProcessor
->
stripeClient
->
refunds
->
all
([
'charge'
=>
$this
->
charge_id
,
'limit'
=>
1
]);
$refund
=
$refunds
->
data
[
0
];
if
(
isset
(
$this
->
contribution
[
'payments'
]))
{
foreach
(
$this
->
contribution
[
'payments'
]
as
$payment
)
{
if
(
$payment
[
'trxn_id'
]
===
$refund
->
id
)
{
return
'Refund '
.
$refund
->
id
.
' already recorded in CiviCRM'
;
}
if
(
$payment
[
'trxn_id'
]
===
$this
->
charge_id
)
{
// This triggers the financial transactions/items to be updated correctly.
$cancelledPaymentID
=
$payment
[
'id'
];
}
}
}
// 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
=
[
'contribution_id'
=>
$this
->
contribution
[
'id'
],
'total_amount'
=>
0
-
abs
(
$amountRefunded
),
'trxn_date'
=>
date
(
'YmdHis'
,
$refund
->
created
),
'trxn_result_code'
=>
$refund
->
reason
,
'fee_amount'
=>
0
-
abs
(
$this
->
fee
),
'trxn_id'
=>
$refund
->
id
,
'order_reference'
=>
$this
->
invoice_id
??
NULL
,
];
if
(
!
empty
(
$cancelledPaymentID
))
{
$refundParams
[
'cancelled_payment_id'
]
=
$cancelledPaymentID
;
}
$this
->
updateContributionRefund
(
$refundParams
);
return
'OK - refund recorded'
;
}
/**
* Create the next contribution for a recurring contribution
* This happens when Stripe generates a new invoice and notifies us (normally by invoice.finalized but
...
...
@@ -684,7 +712,7 @@ class CRM_Core_Payment_StripeIPN {
else
{
$balanceTransactionID
=
CRM_Stripe_Api
::
getObjectParam
(
'balance_transaction'
,
$this
->
getData
()
->
object
);
}
$this
->
set
BalanceTransaction
Details
(
$balanceTransactionID
);
$this
->
fee
=
$this
->
getPaymentProcessor
()
->
getFeeFrom
BalanceTransaction
(
$balanceTransactionID
,
$this
->
retrieve
(
'currency'
,
'String'
,
FALSE
)
);
// Get the CiviCRM recurring contribution that matches the Stripe subscription (if we have one).
$this
->
getSubscriptionDetails
();
...
...
@@ -820,29 +848,6 @@ class CRM_Core_Payment_StripeIPN {
return
TRUE
;
}
private
function
setBalanceTransactionDetails
(
$balanceTransactionID
)
{
// Gather info about the amount and fee.
// Get the Stripe charge object if one exists. Null charge still needs processing.
// If the transaction is declined, there won't be a balance_transaction_id.
$this
->
fee
=
0.0
;
if
(
$balanceTransactionID
)
{
try
{
$currency
=
$this
->
retrieve
(
'currency'
,
'String'
,
FALSE
);
$balanceTransaction
=
$this
->
_paymentProcessor
->
stripeClient
->
balanceTransactions
->
retrieve
(
$balanceTransactionID
);
if
(
$currency
!==
$balanceTransaction
->
currency
&&
!
empty
(
$balanceTransaction
->
exchange_rate
))
{
$this
->
fee
=
CRM_Stripe_Api
::
currencyConversion
(
$balanceTransaction
->
fee
,
$balanceTransaction
->
exchange_rate
,
$currency
);
}
else
{
// We must round to currency precision otherwise payments may fail because Contribute BAO saves but then
// can't retrieve because it tries to use the full unrounded number when it only got saved with 2dp.
$this
->
fee
=
round
(
$balanceTransaction
->
fee
/
100
,
CRM_Utils_Money
::
getCurrencyPrecision
(
$currency
));
}
}
catch
(
Exception
$e
)
{
$this
->
exception
(
'Error retrieving balance transaction. '
.
$e
->
getMessage
());
}
}
}
/**
* This allows us to end a subscription once:
* a) We've reached the end date / number of installments
...
...
This diff is collapsed.
Click to expand it.
CRM/Stripe/Api.php
+
1
−
0
View file @
f8ae05a2
...
...
@@ -226,4 +226,5 @@ class CRM_Stripe_Api {
// to "auto"
return
substr
(
$civiCRMLocale
,
0
,
2
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment