| 1 |
<?php declare( strict_types=1 ); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\PaymentMethods\Base; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MultiSafepay\Exception\ApiException; |
| 7 |
use MultiSafepay\Api\TransactionManager; |
| 8 |
use MultiSafepay\Api\Transactions\RefundRequest; |
| 9 |
use MultiSafepay\Api\Transactions\TransactionResponse; |
| 10 |
use MultiSafepay\ValueObject\CartItem; |
| 11 |
use MultiSafepay\WooCommerce\Services\SdkService; |
| 12 |
use MultiSafepay\WooCommerce\Utils\MoneyUtil; |
| 13 |
use Psr\Http\Client\ClientExceptionInterface; |
| 14 |
use WC_Order; |
| 15 |
use WP_Error; |
| 16 |
|
| 17 |
trait BaseRefunds { |
| 18 |
|
| 19 |
/** |
| 20 |
* Process the refund. |
| 21 |
* |
| 22 |
* @param integer $order_id Order ID. |
| 23 |
* @param float $amount Amount to be refunded. |
| 24 |
* @param string $reason Reason description. |
| 25 |
* |
| 26 |
* @return mixed boolean|WP_Error |
| 27 |
* |
| 28 |
* @throws ClientExceptionInterface |
| 29 |
*/ |
| 30 |
public function process_refund( $order_id, $amount = null, $reason = '' ) { |
| 31 |
|
| 32 |
// Refund amount can not be 0 |
| 33 |
if ( 0.00 === (float) $amount ) { |
| 34 |
return new WP_Error( '400', __( 'Amount of refund should be higher than 0', 'multisafepay' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
/** @var SdkService $sdk */ |
| 38 |
$sdk = new SdkService(); |
| 39 |
|
| 40 |
/** @var TransactionManager $transaction_manager */ |
| 41 |
$transaction_manager = $sdk->get_transaction_manager(); |
| 42 |
|
| 43 |
/** @var WC_Order $order */ |
| 44 |
$order = wc_get_order( $order_id ); |
| 45 |
|
| 46 |
// Get meta multisafepay_transaction_id |
| 47 |
$multisafepay_transaction_id = $order->get_meta( 'multisafepay_transaction_id', true ); |
| 48 |
|
| 49 |
/** @var TransactionResponse $multisafepay_transaction */ |
| 50 |
$multisafepay_transaction = $transaction_manager->get( |
| 51 |
! empty( $multisafepay_transaction_id ) ? $multisafepay_transaction_id : $order->get_order_number() |
| 52 |
); |
| 53 |
|
| 54 |
if ( $multisafepay_transaction->requiresShoppingCart() ) { |
| 55 |
/** @var RefundRequest $refund_request */ |
| 56 |
$refund_request = $transaction_manager->createRefundRequest( $multisafepay_transaction ); |
| 57 |
|
| 58 |
$refunds = $order->get_refunds(); |
| 59 |
$refund_merchant_item_id = reset( $refunds )->id; |
| 60 |
|
| 61 |
$cart_item = new CartItem(); |
| 62 |
$cart_item->addName( __( 'Refund', 'multisafepay' ) ) |
| 63 |
->addQuantity( 1 ) |
| 64 |
->addUnitPrice( MoneyUtil::create_money( (float) $amount, $order->get_currency() )->negative() ) |
| 65 |
->addMerchantItemId( 'refund_id_' . $refund_merchant_item_id ) |
| 66 |
->addTaxRate( 0 ); |
| 67 |
|
| 68 |
$refund_request->getCheckoutData()->addItem( $cart_item ); |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! $multisafepay_transaction->requiresShoppingCart() ) { |
| 72 |
$refund_request = new RefundRequest(); |
| 73 |
$refund_request->addDescriptionText( $reason ); |
| 74 |
$refund_request->addMoney( MoneyUtil::create_money( (float) $amount, $order->get_currency() ) ); |
| 75 |
} |
| 76 |
|
| 77 |
try { |
| 78 |
$error = null; |
| 79 |
$transaction_manager->refund( $multisafepay_transaction, $refund_request ); |
| 80 |
} catch ( Exception |ClientExceptionInterface |ApiException $exception ) { |
| 81 |
$error = __( 'Error:', 'multisafepay' ) . htmlspecialchars( $exception->getMessage() ); |
| 82 |
$this->logger->log_error( 'Error during refund: ' . $error . ' Refund request : ' . wp_json_encode( $refund_request->getData() ) ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! $error ) { |
| 86 |
/* translators: %1$: The currency code. %2$ The transaction amount */ |
| 87 |
$note = sprintf( __( 'Refund of %1$s%2$s has been processed successfully.', 'multisafepay' ), get_woocommerce_currency_symbol( $order->get_currency() ), $amount ); |
| 88 |
$this->logger->log_info( $note ); |
| 89 |
$order->add_order_note( $note ); |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
if ( get_option( 'multisafepay_debugmode', false ) ) { |
| 94 |
/* translators: %1$: The order ID. %2$ The PSP transaction ID */ |
| 95 |
$message = sprintf( __( 'Refund for Order ID: %1$s with transactionId: %2$s gives message: %3$s.', 'multisafepay' ), $order_id, $multisafepay_transaction->getTransactionId(), $error ); |
| 96 |
$this->logger->log_warning( $message ); |
| 97 |
} |
| 98 |
|
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|