/** * Get the single instance aka Singleton * * @access public * @since 1.0.0 * @version 1.0.0 * @return bool */ public static function instance() { if (is_null(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
/** * Cancel authorization * * @param int $order_id */ public function cancel_payment($order_id) { $order = wc_get_order($order_id); if ('square' === $order->payment_method) { try { $this->log("Info: Cancel payment for order {$order_id}"); $trans_id = get_post_meta($order_id, '_transaction_id', true); $captured = get_post_meta($order_id, '_square_charge_captured', true); $location = Woocommerce_Square::instance()->integration->get_option('location'); $token = get_option('woocommerce_square_merchant_access_token'); $this->connect->set_access_token($token); $transaction_status = $this->connect->get_transaction_status($location, $trans_id); if ('AUTHORIZED' === $transaction_status) { $result = $this->connect->void_transaction($location, $trans_id); // returns empty object if (is_wp_error($result)) { $order->add_order_note(__('Unable to void charge!', 'woocommerce-square') . ' ' . $result->get_error_message()); throw new Exception($result->get_error_message()); } elseif (!empty($result->errors)) { $order->add_order_note(__('Unable to void charge!', 'woocommerce-square') . ' ' . print_r($result->errors, true)); throw new Exception(print_r($result->errors, true)); } else { $order->add_order_note(sprintf(__('Square charge voided! (Charge ID: %s)', 'woocommerce-square'), $trans_id)); delete_post_meta($order_id, '_square_charge_captured'); delete_post_meta($order_id, '_transaction_id'); } } } catch (Exception $e) { $this->log(sprintf(__('Unable to void charge!: %s', 'woocommerce-square'), $e->getMessage())); } } }
/** * Refund a charge * @param int $order_id * @param float $amount * @return bool */ public function process_refund($order_id, $amount = null, $reason = '') { $order = wc_get_order($order_id); if (!$order || !$order->get_transaction_id()) { return false; } if ('square' === $order->payment_method) { try { $this->log("Info: Begin refund for order {$order_id} for the amount of {$amount}"); $trans_id = get_post_meta($order_id, '_transaction_id', true); $captured = get_post_meta($order_id, '_square_charge_captured', true); $location = Woocommerce_Square::instance()->integration->get_option('location'); $transaction_status = $this->connect->get_transaction_status($location, $trans_id); if ('CAPTURED' === $transaction_status) { $tender_id = $this->connect->get_tender_id($location, $trans_id); $body = array(); $body['idempotency_key'] = uniqid(); $body['tender_id'] = $tender_id; if (!is_null($amount)) { $body['amount_money'] = array('amount' => $this->format_amount($amount), 'currency' => $order->get_order_currency()); } if ($reason) { $body['reason'] = $reason; } $result = $this->connect->refund_transaction($location, $trans_id, $body); if (is_wp_error($result)) { throw new Exception($result->get_error_message()); } elseif (!empty($result->errors)) { throw new Exception("Error: " . print_r($result->errors, true)); } else { if ('APPROVED' === $result->refund->status || 'PENDING' === $result->refund->status) { $refund_message = sprintf(__('Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-square'), wc_price($result->refund->amount_money->amount / 100), $result->refund->id, $reason); $order->add_order_note($refund_message); $this->log("Success: " . html_entity_decode(strip_tags($refund_message))); return true; } } } } catch (Exception $e) { $this->log(sprintf(__('Error: %s', 'woocommerce-square'), $e->getMessage())); return false; } } }
/** * Maybe render list of locations. * * @param array $form_fields Form fields * * @return array Form fields */ public function maybe_render_locations($form_fields) { $locations = Woocommerce_Square::instance()->square_connect->get_square_business_locations(); if (!empty($locations) && !empty($form_fields)) { $form_fields['location']['options'] = array_merge($form_fields['location']['options'], $locations); $form_fields['location']['disabled'] = false; } return $form_fields; }