9
9
use WCPay \Constants \Order_Status ;
10
10
use WCPay \Constants \Intent_Status ;
11
11
use WCPay \Constants \Payment_Method ;
12
+ use WCPay \Constants \Refund_Status ;
12
13
use WCPay \Exceptions \Order_Not_Found_Exception ;
13
14
use WCPay \Fraud_Prevention \Models \Rule ;
14
15
use WCPay \Logger ;
@@ -1446,18 +1447,19 @@ public function create_refund_for_order( WC_Order $order, float $amount, string
1446
1447
* @param WC_Order_Refund $wc_refund The WC refund object.
1447
1448
* @param string $refund_id The refund ID.
1448
1449
* @param string|null $refund_balance_transaction_id The balance transaction ID of the refund.
1450
+ * @param bool $is_pending Created refund status can be either pending or succeeded. Default false, i.e. succeeded.
1449
1451
* @throws Order_Not_Found_Exception
1450
1452
* @throws Exception
1451
1453
*/
1452
- public function add_note_and_metadata_for_refund ( WC_Order $ order , WC_Order_Refund $ wc_refund , string $ refund_id , ?string $ refund_balance_transaction_id ): void {
1453
- $ note = $ this ->generate_payment_refunded_note ( $ wc_refund ->get_amount (), $ wc_refund ->get_currency (), $ refund_id , $ wc_refund ->get_reason (), $ order );
1454
+ public function add_note_and_metadata_for_created_refund ( WC_Order $ order , WC_Order_Refund $ wc_refund , string $ refund_id , ?string $ refund_balance_transaction_id, bool $ is_pending = false ): void {
1455
+ $ note = $ this ->generate_payment_created_refund_note ( $ wc_refund ->get_amount (), $ wc_refund ->get_currency (), $ refund_id , $ wc_refund ->get_reason (), $ order, $ is_pending );
1454
1456
1455
1457
if ( ! $ this ->order_note_exists ( $ order , $ note ) ) {
1456
1458
$ order ->add_order_note ( $ note );
1457
1459
}
1458
1460
1459
- // Set refund metadata .
1460
- $ this ->set_wcpay_refund_status_for_order ( $ order , 'successful ' );
1461
+ // Use `successful` to maintain the backward compatibility with the previous WooPayments versions .
1462
+ $ this ->set_wcpay_refund_status_for_order ( $ order , $ is_pending ? Refund_Status:: PENDING : 'successful ' );
1461
1463
$ this ->set_wcpay_refund_id_for_refund ( $ wc_refund , $ refund_id );
1462
1464
if ( isset ( $ refund_balance_transaction_id ) ) {
1463
1465
$ this ->set_wcpay_refund_transaction_id_for_order ( $ wc_refund , $ refund_balance_transaction_id );
@@ -1466,6 +1468,55 @@ public function add_note_and_metadata_for_refund( WC_Order $order, WC_Order_Refu
1466
1468
$ order ->save ();
1467
1469
}
1468
1470
1471
+ /**
1472
+ * Handle a failed refund by adding a note, updating metadata, and optionally deleting the refund.
1473
+ *
1474
+ * @param WC_Order $order The order to add the note to.
1475
+ * @param string $refund_id The ID of the failed refund.
1476
+ * @param int $amount The refund amount in cents.
1477
+ * @param string $currency The currency code.
1478
+ * @param WC_Order_Refund|null $wc_refund The WC refund object to delete if provided.
1479
+ * @param bool $is_cancelled Whether this is a cancellation rather than a failure. Default false.
1480
+ * @return void
1481
+ */
1482
+ public function handle_failed_refund ( WC_Order $ order , string $ refund_id , int $ amount , string $ currency , ?WC_Order_Refund $ wc_refund = null , bool $ is_cancelled = false ): void {
1483
+ // Delete the refund if it exists.
1484
+ if ( $ wc_refund ) {
1485
+ $ wc_refund ->delete ();
1486
+ }
1487
+
1488
+ $ note = sprintf (
1489
+ WC_Payments_Utils::esc_interpolated_html (
1490
+ /* translators: %1: the refund amount, %2: WooPayments, %3: ID of the refund */
1491
+ __ ( 'A refund of %1$s was <strong>%4$s</strong> using %2$s (<code>%3$s</code>). ' , 'woocommerce-payments ' ),
1492
+ [
1493
+ 'strong ' => '<strong> ' ,
1494
+ 'code ' => '<code> ' ,
1495
+ ]
1496
+ ),
1497
+ WC_Payments_Explicit_Price_Formatter::get_explicit_price (
1498
+ wc_price ( WC_Payments_Utils::interpret_stripe_amount ( $ amount , $ currency ), [ 'currency ' => strtoupper ( $ currency ) ] ),
1499
+ $ order
1500
+ ),
1501
+ 'WooPayments ' ,
1502
+ $ refund_id ,
1503
+ $ is_cancelled ? __ ( 'cancelled ' , 'woocommerce-payments ' ) : __ ( 'unsuccessful ' , 'woocommerce-payments ' )
1504
+ );
1505
+
1506
+ if ( $ this ->order_note_exists ( $ order , $ note ) ) {
1507
+ return ;
1508
+ }
1509
+
1510
+ // If order has been fully refunded.
1511
+ if ( Order_Status::REFUNDED === $ order ->get_status () ) {
1512
+ $ order ->update_status ( Order_Status::FAILED );
1513
+ }
1514
+
1515
+ $ order ->add_order_note ( $ note );
1516
+ $ this ->set_wcpay_refund_status_for_order ( $ order , Refund_Status::FAILED );
1517
+ $ order ->save ();
1518
+ }
1519
+
1469
1520
/**
1470
1521
* Get content for the success order note.
1471
1522
*
@@ -1872,43 +1923,54 @@ private function generate_dispute_closed_note( $charge_id, $status, $is_inquiry
1872
1923
/**
1873
1924
* Generates the HTML note for a refunded payment.
1874
1925
*
1875
- * @param float $refunded_amount Amount refunded.
1876
- * @param string $refunded_currency Refund currency.
1877
- * @param string $wcpay_refund_id WCPay Refund ID.
1878
- * @param string $refund_reason Refund reason.
1879
- * @param WC_Order $order Order object.
1926
+ * @param float $refunded_amount Amount refunded.
1927
+ * @param string $refunded_currency Refund currency.
1928
+ * @param string $wcpay_refund_id WCPay Refund ID.
1929
+ * @param string $refund_reason Refund reason.
1930
+ * @param WC_Order $order Order object.
1931
+ * @param bool $is_pending Created refund status can be either pending or succeeded. Default false, i.e. succeeded.
1932
+ *
1880
1933
* @return string HTML note.
1881
1934
*/
1882
- private function generate_payment_refunded_note ( float $ refunded_amount , string $ refunded_currency , string $ wcpay_refund_id , string $ refund_reason , WC_Order $ order ): string {
1935
+ private function generate_payment_created_refund_note ( float $ refunded_amount , string $ refunded_currency , string $ wcpay_refund_id , string $ refund_reason , WC_Order $ order, bool $ is_pending ): string {
1883
1936
$ multi_currency_instance = WC_Payments_Multi_Currency ();
1884
1937
$ formatted_price = WC_Payments_Explicit_Price_Formatter::get_explicit_price ( $ multi_currency_instance ->get_backend_formatted_wc_price ( $ refunded_amount , [ 'currency ' => strtoupper ( $ refunded_currency ) ] ), $ order );
1885
1938
1939
+ $ status_text = $ is_pending ?
1940
+ sprintf (
1941
+ '<a href="https://woocommerce.com/document/woopayments/managing-money/#pending-refunds" target="_blank" rel="noopener noreferrer">%1$s</a> ' ,
1942
+ __ ( 'is pending ' , 'woocommerce-payments ' )
1943
+ )
1944
+ : __ ( 'was successfully processed ' , 'woocommerce-payments ' );
1945
+
1886
1946
if ( empty ( $ refund_reason ) ) {
1887
1947
$ note = sprintf (
1888
1948
WC_Payments_Utils::esc_interpolated_html (
1889
- /* translators: %1: the refund amount, %2: WooPayments, %3: ID of the refund */
1890
- __ ( 'A refund of %1$s was successfully processed using %2$s (<code>%3$s</code>). ' , 'woocommerce-payments ' ),
1949
+ /* translators: %1: the refund amount, %2: WooPayments, %3: ID of the refund, %4: status text */
1950
+ __ ( 'A refund of %1$s %4$s using %2$s (<code>%3$s</code>). ' , 'woocommerce-payments ' ),
1891
1951
[
1892
1952
'code ' => '<code> ' ,
1893
1953
]
1894
1954
),
1895
1955
$ formatted_price ,
1896
1956
'WooPayments ' ,
1897
- $ wcpay_refund_id
1957
+ $ wcpay_refund_id ,
1958
+ $ status_text
1898
1959
);
1899
1960
} else {
1900
1961
$ note = sprintf (
1901
1962
WC_Payments_Utils::esc_interpolated_html (
1902
- /* translators: %1: the successfully charged amount, %2: WooPayments, %3: reason, %4: refund id */
1903
- __ ( 'A refund of %1$s was successfully processed using %2$s. Reason: %3$s. (<code>%4$s</code>) ' , 'woocommerce-payments ' ),
1963
+ /* translators: %1: the refund amount, %2: WooPayments, %3: reason, %4: refund id, %5: status text */
1964
+ __ ( 'A refund of %1$s %5$s using %2$s. Reason: %3$s. (<code>%4$s</code>) ' , 'woocommerce-payments ' ),
1904
1965
[
1905
1966
'code ' => '<code> ' ,
1906
1967
]
1907
1968
),
1908
1969
$ formatted_price ,
1909
1970
'WooPayments ' ,
1910
1971
$ refund_reason ,
1911
- $ wcpay_refund_id
1972
+ $ wcpay_refund_id ,
1973
+ $ status_text
1912
1974
);
1913
1975
}
1914
1976
0 commit comments