Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring, simplifying the 'process_refund' method #157

Merged
merged 3 commits into from Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 0 additions & 94 deletions includes/gateway/class-omise-payment-creditcard.php
Expand Up @@ -389,100 +389,6 @@ public function process_capture( $order ) {
}
}

/**
* Process refund.
*
* @param int $order_id
* @param float $amount
* @param string $reason
*
* @return boolean True or false based on success, or a WP_Error object.
*
* @see WC_Payment_Gateway::process_refund( $order_id, $amount = null, $reason = '' )
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
if ( ! isset( $order_id ) || ! $order = $this->load_order( $order_id ) ) {
return new WP_Error(
'error',
sprintf(
wp_kses(
__( 'We cannot process your refund.<br/>Note that nothing wrong by you, this might be from the store issue.<br/><br/>Please feel try to create a refund again or report our support team that you have found this problem', 'omise' ),
array(
'br' => array()
)
),
$order_id
)
);
}

$order->add_order_note(
sprintf(
__( 'Omise: Refunding a payment with an amount %1$s %2$s', 'omise' ),
$amount,
$order->get_order_currency()
)
);

try {
$charge = OmiseCharge::retrieve( $this->get_charge_id_from_order() );
$refund = $charge->refunds()->create( array(
'amount' => Omise_Money::to_subunit( $amount, $order->get_order_currency() )
) );

if ( $refund['voided'] ) {
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Voided an amount %1$s %2$s.<br/>Refund id is %3$s', 'omise' ),
array( 'br' => array() )
),
$amount,
$order->get_order_currency(),
$refund['id']
)
);
} else {
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Refunded an amount %1$s %2$s.<br/>Refund id is %3$s', 'omise' ),
array( 'br' => array() )
),
$amount,
$order->get_order_currency(),
$refund['id']
)
);
}

return true;
} catch (Exception $e) {
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Refund failed.<br/>%s', 'omise' ),
array( 'br' => array() )
),
$e->getMessage()
)
);

return new WP_Error(
'error',
sprintf(
wp_kses(
__( 'Omise: Refund failed.<br/>%s', 'omise' ),
array( 'br' => array() )
),
$e->getMessage()
)
);
}

return false;
}

public function callback() {
if ( ! isset( $_GET['order_id'] ) || ! $order = $this->load_order( $_GET['order_id'] ) ) {
wc_add_notice(
Expand Down
78 changes: 0 additions & 78 deletions includes/gateway/class-omise-payment-truemoney.php
Expand Up @@ -89,82 +89,4 @@ public function charge( $order_id, $order ) {
'metadata' => $metadata
) );
}

/**
* Process refund.
*
* @param int $order_id
* @param float $amount
* @param string $reason
*
* @return boolean True|False based on success, or a WP_Error object.
*
* @see WC_Payment_Gateway::process_refund( $order_id, $amount = null, $reason = '' )
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
if ( ! isset( $order_id ) || ! $order = $this->load_order( $order_id ) ) {
return new WP_Error(
'error',
sprintf(
wp_kses(
__( 'We cannot process your refund.<br/>Note that nothing wrong by you, this might be from the store issue.<br/><br/>Please feel try to create a refund again or report our support team that you have found this problem', 'omise' ),
array(
'br' => array()
)
),
$order_id
)
);
}

$currency = $order->get_order_currency();

$order->add_order_note(
sprintf( __( 'Omise: Refunding a payment with an amount %1$s %2$s', 'omise' ), $amount, $currency )
);

try {
$charge = OmiseCharge::retrieve( $order->get_transaction_id() );
$refund = $charge->refunds()->create( array(
'amount' => Omise_Money::to_subunit( $amount, $currency )
) );

$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Refunded an amount %1$s %2$s.<br/>Refund id is %3$s', 'omise' ),
array( 'br' => array() )
),
$amount,
$currency,
$refund['id']
)
);

return true;
} catch (Exception $e) {
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Refund failed.<br/>%s', 'omise' ),
array( 'br' => array() )
),
$e->getMessage()
)
);

return new WP_Error(
'error',
sprintf(
wp_kses(
__( 'Omise: Refund failed.<br/>%s', 'omise' ),
array( 'br' => array() )
),
$e->getMessage()
)
);
}

return false;
}
}
56 changes: 56 additions & 0 deletions includes/gateway/class-omise-payment.php
Expand Up @@ -226,6 +226,62 @@ abstract public function charge( $order_id, $order );
abstract public function result( $order_id, $order, $charge );

/**
* Process refund.
*
* @param int $order_id
* @param float $amount
* @param string $reason
*
* @return boolean True|False based on success, or a WP_Error object.
*
* @see WC_Payment_Gateway::process_refund( $order_id, $amount = null, $reason = '' )
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
if ( ! $order = wc_get_order( $order_id ) ) {
$message = __(
'Refund failed. Cannot retrieve an order with the given ID: %s. Please try again or do a manual refund.',
'omise'
);

return new WP_Error( 'error', sprintf( wp_kses( $message, array( 'br' => array() ) ), $order_id ) );
}

try {
$charge = OmiseCharge::retrieve( $order->get_transaction_id() );
$refund = $charge->refunds()->create( array(
'amount' => Omise_Money::to_subunit( $amount, $order->get_order_currency() )
jonrandy marked this conversation as resolved.
Show resolved Hide resolved
) );

if ( $refund['voided'] ) {
$message = sprintf(
wp_kses(
__( 'Omise: Voided an amount of %1$s %2$s.<br/>Refund id is %3$s', 'omise' ),
array( 'br' => array() )
),
$amount,
$order->get_order_currency(),
$refund['id']
);
} else {
$message = sprintf(
wp_kses(
__( 'Omise: Refunded an amount of %1$s %2$s.<br/>Refund id is %3$s', 'omise' ),
array( 'br' => array() )
),
$amount,
$order->get_order_currency(),
$refund['id']
);
}

$order->add_order_note( $message );
return true;
} catch (Exception $e) {
return new WP_Error( 'error', __( 'Refund failed.' ) . ' ' . $e->getMessage() );
}
}

/**
* Retrieve a charge by a given charge id (that attach to an order).
* Find some diff, then merge it back to WooCommerce system.
*
Expand Down