Skip to content
Snippets Groups Projects
Commit f6e848a1 authored by mattwire's avatar mattwire
Browse files

Check for fee in tests. Switch to getDetailsForBalanceTransaction() for all webhooks that use it

parent 4181358b
Branches
Tags
1 merge request!217Implement Stripe Checkout (with support for SEPA and ACH)
......@@ -252,7 +252,7 @@ class CRM_Core_Payment_StripeCheckout extends CRM_Core_Payment_Stripe {
* Takes the lineitems passed into doPayment and converts them into an array suitable for passing to Stripe Checkout
*
* @param array $civicrmLineItems
* @param string $currency
* @param \Civi\Payment\PropertyBag $propertyBag
*
* @return array
* @throws \Brick\Money\Exception\UnknownCurrencyException
......
......@@ -154,33 +154,6 @@ class Events {
return $contribution ?? [];
}
/**
* @param string $chargeID
*
* @return float
* @throws \CRM_Core_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
private function getFeeFromCharge(string $chargeID): float {
if (($this->getData()->object['object'] !== 'charge') && (!empty($chargeID))) {
$charge = $this->getPaymentProcessor()->stripeClient->charges->retrieve($chargeID);
$balanceTransactionID = \CRM_Stripe_Api::getObjectParam('balance_transaction', $charge);
}
else {
$balanceTransactionID = $this->getValueFromStripeObject('balance_transaction', 'String');
}
try {
$balanceTransaction = $this->getPaymentProcessor()->stripeClient->balanceTransactions->retrieve($balanceTransactionID);
}
catch (\Exception $e) {
throw new \Civi\Payment\Exception\PaymentProcessorException("Error retrieving balanceTransaction {$balanceTransactionID}. " . $e->getMessage());
}
$fee = $this->getPaymentProcessor()
->getFeeFromBalanceTransaction($balanceTransaction, $this->getValueFromStripeObject('currency', 'String'));
return $fee ?? 0.0;
}
/**
* @param string $chargeID
*
......@@ -305,6 +278,7 @@ class Events {
// We have a recurring contribution but no contribution so we'll repeattransaction
// Stripe has generated a new invoice (next payment in a subscription) so we
// create a new contribution in CiviCRM
$balanceTransactionDetails = $this->getDetailsFromBalanceTransaction($chargeID);
$repeatContributionParams = [
'contribution_recur_id' => $contributionRecur['id'],
'contribution_status_id' => \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
......@@ -312,8 +286,11 @@ class Events {
'order_reference' => $invoiceID,
'trxn_id' => $chargeID,
'total_amount' => $this->getValueFromStripeObject('amount', 'String'),
'fee_amount' => $this->getFeeFromCharge($chargeID),
// 'fee_amount' Added below via $balanceTransactionDetails
];
foreach ($balanceTransactionDetails as $key => $value) {
$repeatContributionParams[$key] = $value;
}
return $this->repeatContribution($repeatContributionParams);
// Don't touch the contributionRecur as it's updated automatically by Contribution.repeattransaction
}
......@@ -747,15 +724,20 @@ class Events {
// If contribution is in Pending or Failed state record payment and transition to Completed
if (in_array($contribution['contribution_status_id'], $statusesAllowedToComplete)) {
$balanceTransactionDetails = $this->getDetailsFromBalanceTransaction($chargeID);
$contributionParams = [
'contribution_id' => $contribution['id'],
'trxn_date' => $this->getValueFromStripeObject('receive_date', 'String'),
'order_reference' => $invoiceID,
'trxn_id' => $chargeID,
'total_amount' => $this->getValueFromStripeObject('amount', 'String'),
'fee_amount' => $this->getFeeFromCharge($chargeID),
// 'fee_amount' Added below via $balanceTransactionDetails
'contribution_status_id' => $contribution['contribution_status_id'],
];
foreach ($balanceTransactionDetails as $key => $value) {
$contributionParams[$key] = $value;
}
$this->updateContributionCompleted($contributionParams);
// Don't touch the contributionRecur as it's updated automatically by Contribution.completetransaction
}
......
......@@ -159,6 +159,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'pi_mock,ch_mock',
'fee_amount' => 11.90
]);
}
......@@ -295,6 +296,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'pi_mock,ch_mock',
'fee_amount' => 11.90
]);
}
......@@ -332,6 +334,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'ch_mock',
'fee_amount' => 11.90
]);
}
......@@ -496,6 +499,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'ch_mock',
'fee_amount' => 11.90
]);
$this->checkContribRecur(['contribution_status_id' => 'In Progress']);
}
......@@ -533,6 +537,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'ch_mock',
'fee_amount' => 11.90
]);
$this->checkContribRecur(['contribution_status_id' => 'In Progress']);
}
......@@ -731,6 +736,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'in_mock_2,ch_mock_2',
'fee_amount' => 11.90
], (int) $contrib2['id']);
}
......@@ -783,6 +789,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'in_mock_2,ch_mock_2',
'fee_amount' => 11.90
], $contrib2);
// Now trigger invoice.finalized. We expect that it does nothing?
......@@ -810,6 +817,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'in_mock_2,ch_mock_2',
'fee_amount' => 11.90
], $contrib2);
}
......@@ -1009,6 +1017,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'in_mock_2,ch_mock_3',
'fee_amount' => 11.90
], $contributions[1]);
}
......@@ -1067,6 +1076,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->checkContrib([
'contribution_status_id' => 'Completed',
'trxn_id' => 'ch_mock',
'fee_amount' => 11.90
]);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment