Skip to content

Commit

Permalink
Change refund function signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Mar 21, 2023
1 parent 0e5fa4c commit c2d2732
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Pronamic\WordPress\Pay\Fields\SelectFieldOptionGroup;
use Pronamic\WordPress\Pay\Payments\Payment;
use Pronamic\WordPress\Pay\Payments\PaymentStatus;
use Pronamic\WordPress\Pay\Refunds\Refund;
use WP_Error;

/**
Expand Down Expand Up @@ -878,12 +879,15 @@ public function update_status( Payment $payment ) {
/**
* Create refund.
*
* @param string $transaction_id Transaction ID.
* @param Money $amount Amount to refund.
* @param string $description Refund reason.
* @param Refund $refund Refund.
* @return null|string
*/
public function create_refund( $transaction_id, Money $amount, $description = null ) {
public function create_refund( Refund $refund ) {
$payment = $refund->get_payment();
$amount = $refund->get_amount();

$transaction_id = $payment->get_transaction_id();

$original_transaction = $this->request( 'GET', 'Transaction/Status/' . $transaction_id );

if ( ! \is_object( $original_transaction ) ) {
Expand All @@ -909,13 +913,7 @@ public function create_refund( $transaction_id, Money $amount, $description = nu
}

// Invoice.
$payment = \get_pronamic_payment_by_transaction_id( $transaction_id );

$invoice = null;

if ( null !== $payment ) {
$invoice = Util::get_invoice_number( (string) $this->config->get_invoice_number(), $payment );
}
$invoice = Util::get_invoice_number( (string) $this->config->get_invoice_number(), $payment );

// Refund request.
$data = (object) [
Expand Down Expand Up @@ -944,50 +942,47 @@ public function create_refund( $transaction_id, Money $amount, $description = nu
],
];

$refund = $this->request( 'POST', 'Transaction', $data );
$result = $this->request( 'POST', 'Transaction', $data );

// Check refund object.
if ( ! \is_object( $refund ) ) {
return null;
if ( ! \is_object( $result ) ) {
throw new \Exception( 'Unexpceted response from Buckaroo.' );
}

// Check refund status.
if ( \property_exists( $refund, 'Status' ) && \property_exists( $refund->Status, 'Code' ) ) {
$status = Statuses::transform( (string) $refund->Status->Code->Code );
if ( \property_exists( $result, 'Status' ) && \property_exists( $result->Status, 'Code' ) ) {
$status = Statuses::transform( (string) $result->Status->Code->Code );

if ( PaymentStatus::SUCCESS !== $status ) {
throw new \Exception(
\sprintf(
/* translators: 1: payment provider name, 2: status message, 3: status sub message*/
__( 'Unable to create refund at %1$s gateway: %2$s%3$s', 'pronamic_ideal' ),
__( 'Buckaroo', 'pronamic_ideal' ),
$refund->Status->Code->Description,
\property_exists( $refund->Status, 'SubCode' ) ? '' . $refund->Status->SubCode->Description : ''
$result->Status->Code->Description,
\property_exists( $result->Status, 'SubCode' ) ? '' . $result->Status->SubCode->Description : ''
)
);
}
}

// Update payment refunded amount.
if ( null !== $payment ) {
$result = $this->request( 'GET', 'Transaction/RefundInfo/' . $transaction_id );

if (
\property_exists( $result, 'RefundedAmount' )
&&
\property_exists( $result, 'RefundCurrency' )
&&
! empty( $result->RefundedAmount )
) {
$refunded_amount = new Money( $result->RefundedAmount, $result->RefundCurrency );

$payment->set_refunded_amount( $refunded_amount );
}
if ( \property_exists( $result, 'Key' ) ) {
$refund->psp_id = $result->Key;
}

// Return.
$refund_id = \property_exists( $refund, 'Key' ) ? $refund->Key : null;
// Update payment refunded amount.
$result = $this->request( 'GET', 'Transaction/RefundInfo/' . $transaction_id );

return $refund_id;
if (
\property_exists( $result, 'RefundedAmount' )
&&
\property_exists( $result, 'RefundCurrency' )
&&
! empty( $result->RefundedAmount )
) {
$refunded_amount = new Money( $result->RefundedAmount, $result->RefundCurrency );

$payment->set_refunded_amount( $refunded_amount );
}
}
}

0 comments on commit c2d2732

Please sign in to comment.