Skip to content

Commit 88b2020

Browse files
authored
Ensure manually captured orders have the Stripe fee and payout meta added to them (#3508)
* Introduce new helper to fetch balance transaction ID from charge object * Fix manually captured orders missing stripe fee and payout meta * Add changelog entry
1 parent 7953774 commit 88b2020

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Fix - Set order payment method title to the customizable title setting rather than the default label.
3232
* Fix - Update Cash App payments to avoid confirming on creation, resolving issues with generic payment failures in live mode.
3333
* Tweak - Add order lock for redirect payments.
34+
* Fix - Missing Stripe Fee and Stripe Payout details on orders that were captured manually.
3435

3536
= 8.7.0 - 2024-09-16 =
3637
* Add - Introduces a new promotional surface to encourage merchants with the legacy checkout experience and APMs enabled to use the new checkout experience.

includes/abstracts/abstract-wc-stripe-payment-gateway.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,4 +2297,23 @@ private function maybe_attach_source_to_customer( $source, $customer = null ) {
22972297

22982298
return true;
22992299
}
2300+
2301+
/**
2302+
* Retrieves the balance transaction ID from the Stripe charge.
2303+
*
2304+
* @param stdClass $charge The charge object.
2305+
*
2306+
* @return string|null The balance transaction ID.
2307+
*/
2308+
public function get_balance_transaction_id_from_charge( $charge ) {
2309+
$balance_transaction_id = null;
2310+
2311+
if ( ! empty( $charge->balance_transaction->id ) ) {
2312+
$balance_transaction_id = $charge->balance_transaction->id;
2313+
} elseif ( ! empty( $charge->balance_transaction ) && is_string( $charge->balance_transaction ) ) {
2314+
$balance_transaction_id = $charge->balance_transaction;
2315+
}
2316+
2317+
return $balance_transaction_id;
2318+
}
23002319
}

includes/class-wc-stripe-order-handler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,11 @@ public function capture_payment( $order_id ) {
334334
$order->save();
335335
}
336336

337-
$this->update_fees( $order, $result->balance_transaction->id );
337+
$balance_transaction_id = $this->get_balance_transaction_id_from_charge( $result );
338+
339+
if ( ! empty( $balance_transaction_id ) ) {
340+
$this->update_fees( $order, $balance_transaction_id );
341+
}
338342
}
339343

340344
// This hook fires when admin manually changes order status to processing or completed.

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,6 @@ If you get stuck, you can ask for help in the Plugin Forum.
159159
* Fix - Set order payment method title to the customizable title setting rather than the default label.
160160
* Fix - Update Cash App payments to avoid confirming on creation, resolving issues with generic payment failures in live mode.
161161
* Tweak - Add order lock for redirect payments.
162+
* Fix - Missing Stripe Fee and Stripe Payout details on orders that were captured manually.
162163

163164
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

tests/phpunit/test-wc-stripe-payment-gateway.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,47 @@ private function get_partial_mock_for_gateway( array $methods = [] ) {
525525
->setMethods( $methods )
526526
->getMock();
527527
}
528+
529+
public function test_get_balance_transaction_id_from_charge() {
530+
$expected_balance_transaction_id = 'txn_test123';
531+
$balance_transaction_object = (object) [
532+
'id' => $expected_balance_transaction_id,
533+
];
534+
535+
$charge_expanded = (object) [
536+
'id' => 'ch_test123',
537+
'balance_transaction' => $balance_transaction_object,
538+
];
539+
$this->assertEquals( $expected_balance_transaction_id, $this->gateway->get_balance_transaction_id_from_charge( $charge_expanded ) );
540+
541+
$charge_non_expanded = (object) [
542+
'id' => 'ch_test123',
543+
'balance_transaction' => $expected_balance_transaction_id,
544+
];
545+
$this->assertEquals( $expected_balance_transaction_id, $this->gateway->get_balance_transaction_id_from_charge( $charge_non_expanded ) );
546+
547+
/**
548+
* ------------------------------------
549+
* Test invalid cases.
550+
* ------------------------------------
551+
*/
552+
$charge_no_balance_transaction_id = (object) [
553+
'id' => 'ch_test123',
554+
];
555+
$this->assertEquals( null, $this->gateway->get_balance_transaction_id_from_charge( $charge_no_balance_transaction_id ) );
556+
557+
$charge_no_balance_transaction = (object) [
558+
'id' => 'ch_test123',
559+
'balance_transaction' => null,
560+
];
561+
$this->assertEquals( null, $this->gateway->get_balance_transaction_id_from_charge( $charge_no_balance_transaction ) );
562+
563+
$charge_no_balance_transaction_object = (object) [
564+
'id' => 'ch_test123',
565+
'balance_transaction' => (object) [],
566+
];
567+
$this->assertEquals( null, $this->gateway->get_balance_transaction_id_from_charge( $charge_no_balance_transaction_object ) );
568+
569+
$this->assertEquals( null, $this->gateway->get_balance_transaction_id_from_charge( null ) );
570+
}
528571
}

0 commit comments

Comments
 (0)