Refund.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. class Laybuy_Processes_Refund extends Laybuy_Processes_AbstractProcess
  3. {
  4. public $orderId;
  5. public $amount;
  6. public $reason;
  7. public function setOrderId($orderId)
  8. {
  9. $this->orderId = (int) $orderId;
  10. return $this;
  11. }
  12. public function getOrderId()
  13. {
  14. return $this->orderId;
  15. }
  16. public function setAmount($amount)
  17. {
  18. $this->amount = $amount;
  19. return $this;
  20. }
  21. public function getAmount()
  22. {
  23. return $this->amount;
  24. }
  25. public function setReason($reason)
  26. {
  27. $this->reason = $reason;
  28. return $this;
  29. }
  30. public function getReason()
  31. {
  32. return $this->reason;
  33. }
  34. public function execute()
  35. {
  36. $order_id = $this->getOrderId();
  37. $amount = $this->getAmount();
  38. $reason = $this->getReason();
  39. WC_Laybuy_Logger::log("Refunding WooCommerce Order #{$order_id} for \${$amount}...");
  40. if (function_exists('wc_get_order')) {
  41. $order = wc_get_order( $order_id );
  42. } else {
  43. $order = new WC_Order( $order_id );
  44. }
  45. $laybuy_token = get_post_meta($order_id, '_laybuy_token', TRUE);
  46. $laybuy_order_id = get_post_meta( $order_id, '_laybuy_order_id', true );
  47. if (!$laybuy_token) {
  48. WC_Laybuy_Logger::error("Refund order {$order_id} failed:\n | Token not found");
  49. $ret['error'] = 'Token not found error';
  50. return $ret;
  51. }
  52. $response = $this->getProcessManager()->getApiGateway()->refund([
  53. 'orderId' => $laybuy_order_id,
  54. 'token' => $laybuy_token,
  55. 'amount' => $amount,
  56. 'refundReference' => $this->_makeUniqueReference($order_id),
  57. ]
  58. );
  59. if( is_wp_error( $response ) ) {
  60. WC_Laybuy_Logger::error("Failed to refund the order {$order_id}. " . print_r($response, true));
  61. $order->update_status( 'failed', __( $response->get_error_message(), 'woocommerce_laybuy' ) );
  62. wc_add_notice( __( 'Failed to refund the order, please try again, Error message: ', 'woocommerce_laybuy' ) . $response->get_error_message(), 'error' );
  63. return $response;
  64. }
  65. if ('ERROR' === $response->result) {
  66. $order->update_status( 'failed', __( $response->error, 'woocommerce_laybuy' ) );
  67. $order->add_order_note( __( "Failed to send refund of \${$amount} to Laybuy.", 'woo_laybuy' ) );
  68. wc_add_notice( __( "Failed to refund order {$order_id}", 'woocommerce_laybuy' ) . $response->error, 'error' );
  69. return false;
  70. }
  71. WC_Laybuy_Logger::log("Successfully refunded {$amount} for order {$order_id}. " . print_r($response, true));
  72. $order->add_order_note( __( "Refunded \${$amount} via Laybuy (Refund ID: {$response->refundId}). Reason: {$reason}", 'woo_laybuy' ) );
  73. update_post_meta( $order_id, '_laybuy_refund_id', $response->refundId );
  74. return true;
  75. }
  76. }