| 1 |
<?php |
| 2 |
|
| 3 |
namespace MakeCommerce\Payment\Gateway; |
| 4 |
|
| 5 |
trait Refund { |
| 6 |
|
| 7 |
/** |
| 8 |
* Processes refunds. Automatically called by \WC_Payment_Gateway |
| 9 |
* |
| 10 |
* @since 3.0.0 |
| 11 |
*/ |
| 12 |
public function process_refund( $order_id, $amount = null, $comment = '' ) { |
| 13 |
|
| 14 |
if ( $this->MK ) { |
| 15 |
|
| 16 |
try { |
| 17 |
|
| 18 |
$order = new \WC_Order( $order_id ); |
| 19 |
$transactionId = $order->get_transaction_id(); |
| 20 |
|
| 21 |
if ( empty( $transactionId ) ) { |
| 22 |
$transactionId = get_post_meta( $order_id, '_makecommerce_transaction_id', true ); |
| 23 |
} |
| 24 |
|
| 25 |
if ( $response = $this->MK->createRefund( $transactionId, array( 'amount' => sprintf( "%.2f", $amount ), 'comment' => ( $comment ? : 'refund' ) ) ) ) { |
| 26 |
if ( $status = ( string )$response->transaction->status ) { |
| 27 |
|
| 28 |
switch ( $status ) { |
| 29 |
case "REFUNDED": |
| 30 |
$order->add_order_note( sprintf( __( 'Refund completed for amount %s', 'wc_makecommerce_domain' ), $amount ) ); |
| 31 |
return true; |
| 32 |
break; |
| 33 |
case "PART_REFUNDED": |
| 34 |
$order->add_order_note( sprintf( __( 'Partial refund completed for amount %s', 'wc_makecommerce_domain' ), $amount ) ); |
| 35 |
return true; |
| 36 |
break; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return false; |
| 42 |
|
| 43 |
} catch ( \Exception $e ) { |
| 44 |
return new \WP_Error( 'makecommerce_refund_error', $e->getMessage() ); |
| 45 |
} |
| 46 |
|
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
return false; |
| 51 |
} |
| 52 |
} |