diff --git a/Civi/Stripe/Api.php b/Civi/Stripe/Api.php index 3c8388ab19427ee840c9b53c4622f2426106c6dd..a2e3b8e076da02c88e91107376f963f6a9f5d5bc 100644 --- a/Civi/Stripe/Api.php +++ b/Civi/Stripe/Api.php @@ -11,6 +11,7 @@ */ namespace Civi\Stripe; +use Civi\Payment\Exception\PaymentProcessorException; use CRM_Stripe_ExtensionUtil as E; class Api { @@ -47,13 +48,24 @@ class Api { * @throws \Stripe\Exception\ApiErrorException */ public function getDetailsFromBalanceTransaction(string $chargeID, $stripeObject = NULL): array { - if ($stripeObject && ($stripeObject->object !== 'charge') && (!empty($chargeID))) { + if (!empty($chargeID)) { $charge = $this->getPaymentProcessor()->stripeClient->charges->retrieve($chargeID); $balanceTransactionID = $this->getValueFromStripeObject('balance_transaction', 'String', $charge); } - else { + elseif ($stripeObject && ($stripeObject->object === 'charge')) { $balanceTransactionID = $this->getValueFromStripeObject('balance_transaction', 'String', $stripeObject); } + else { + // We don't have any way of getting the balance_transaction ID. + throw new \Civi\Payment\Exception\PaymentProcessorException('Cannot call getDetailsFromBalanceTransaction with empty chargeID when stripeObject is not of type "charge"'); + } + + // We may need to get balance transaction details multiple times when processing. + // The first time we retrieve from stripe but then we use the cached version. + if (isset(\Civi::$statics[__CLASS__][__FUNCTION__]['balanceTransactions'][$balanceTransactionID])) { + return \Civi::$statics[__CLASS__][__FUNCTION__]['balanceTransactions'][$balanceTransactionID]; + } + try { $balanceTransaction = $this->getPaymentProcessor()->stripeClient->balanceTransactions->retrieve($balanceTransactionID); } @@ -63,17 +75,18 @@ class Api { if (!empty($balanceTransactionID)) { $fee = $this->getPaymentProcessor() ->getFeeFromBalanceTransaction($balanceTransaction, $this->getValueFromStripeObject('currency', 'String', $stripeObject)); - return [ - 'fee_amount' => $fee, + \Civi::$statics[__CLASS__][__FUNCTION__]['balanceTransactions'][$balanceTransactionID] = [ + 'fee_amount' => \Civi::settings()->get('stripe_record_payoutcurrency') ? $balanceTransaction->fee / 100 : $fee, 'available_on' => \CRM_Stripe_Api::formatDate($balanceTransaction->available_on), 'exchange_rate' => $balanceTransaction->exchange_rate, 'charge_amount' => $this->getValueFromStripeObject('amount', 'Float', $stripeObject), 'charge_currency' => $this->getValueFromStripeObject('currency', 'String', $stripeObject), + 'charge_fee' => $fee, 'payout_amount' => $balanceTransaction->amount / 100, 'payout_currency' => \CRM_Stripe_Api::formatCurrency($balanceTransaction->currency), - 'payout_net' => $balanceTransaction->net / 100, 'payout_fee' => $balanceTransaction->fee / 100, ]; + return \Civi::$statics[__CLASS__][__FUNCTION__]['balanceTransactions'][$balanceTransactionID]; } else { return [ diff --git a/managed/stripecustomdata.mgd.php b/managed/stripecustomdata.mgd.php index f7cb603e8ec789960a1c6c9f96be78fe3a28f75a..43a97707967557fdb30b56793d7ae60c53afe8d3 100644 --- a/managed/stripecustomdata.mgd.php +++ b/managed/stripecustomdata.mgd.php @@ -144,7 +144,7 @@ else { ], ], [ - 'name' => 'CustomGroup_Payment_details_CustomField_payout_net', + 'name' => 'CustomGroup_Payment_details_CustomField_charge_amount', 'entity' => 'CustomField', 'cleanup' => 'unused', 'update' => 'unmodified', @@ -152,14 +152,14 @@ else { 'version' => 4, 'values' => [ 'custom_group_id.name' => 'Payment_details', - 'name' => 'payout_net', - 'label' => E::ts('Payout Net'), + 'name' => 'charge_amount', + 'label' => E::ts('Charge Amount'), 'data_type' => 'Float', 'html_type' => 'Text', 'text_length' => 255, 'is_searchable' => TRUE, 'is_view' => TRUE, - 'column_name' => 'payout_net', + 'column_name' => 'charge_amount', ], 'match' => [ 'name', @@ -168,7 +168,7 @@ else { ], ], [ - 'name' => 'CustomGroup_Payment_details_CustomField_charge_amount', + 'name' => 'CustomGroup_Payment_details_CustomField_charge_currency', 'entity' => 'CustomField', 'cleanup' => 'unused', 'update' => 'unmodified', @@ -176,14 +176,13 @@ else { 'version' => 4, 'values' => [ 'custom_group_id.name' => 'Payment_details', - 'name' => 'charge_amount', - 'label' => E::ts('Charge Amount'), - 'data_type' => 'Float', + 'name' => 'charge_currency', + 'label' => E::ts('Charge Currency'), 'html_type' => 'Text', 'text_length' => 255, 'is_searchable' => TRUE, 'is_view' => TRUE, - 'column_name' => 'charge_amount', + 'column_name' => 'charge_currency', ], 'match' => [ 'name', @@ -192,7 +191,7 @@ else { ], ], [ - 'name' => 'CustomGroup_Payment_details_CustomField_charge_currency', + 'name' => 'CustomGroup_Payment_details_CustomField_charge_fee', 'entity' => 'CustomField', 'cleanup' => 'unused', 'update' => 'unmodified', @@ -200,13 +199,14 @@ else { 'version' => 4, 'values' => [ 'custom_group_id.name' => 'Payment_details', - 'name' => 'charge_currency', - 'label' => E::ts('Charge Currency'), + 'name' => 'charge_fee', + 'label' => E::ts('Charge Fee'), + 'data_type' => 'Float', 'html_type' => 'Text', 'text_length' => 255, 'is_searchable' => TRUE, 'is_view' => TRUE, - 'column_name' => 'charge_currency', + 'column_name' => 'charge_fee', ], 'match' => [ 'name',