Skip to content

Commit

Permalink
Remove mondu payment methods if not enabled at mondu's merchant (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurmmoreira authored Aug 3, 2022
1 parent 923726e commit 1c1554c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 56 deletions.
29 changes: 18 additions & 11 deletions src/Mondu/Mondu/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
use Mondu\Exceptions\MonduException;
use Mondu\Exceptions\CredentialsNotSetException;
use Mondu\Exceptions\ResponseException;
use WC_Logger_Interface;

class Api {
private $global_settings;

private $logger;

public function __construct() {
$this->global_settings = get_option(Plugin::OPTION_NAME);
$this->logger = wc_get_logger();
}

public function register() {
Expand Down Expand Up @@ -184,18 +180,29 @@ public function cancel_invoice($mondu_uuid, $mondu_invoice_uuid) {
return json_decode($result['body'], true);
}

/**
* @param $mondu_invoice_uuid
* @param array $credit_note
* @return mixed
* @throws MonduException
* @throws ResponseException
*/
/**
* @param $mondu_invoice_uuid
* @param array $credit_note
* @return mixed
* @throws MonduException
* @throws ResponseException
*/
public function create_credit_note($mondu_invoice_uuid, array $credit_note) {
$result = $this->post(sprintf('/invoices/%s/credit_notes', $mondu_invoice_uuid), $credit_note);
return json_decode($result['body'], true);
}

/**
* @return string
* @throws MonduException
* @throws ResponseException
*/
public function get_payment_methods() {
$result = $this->get(sprintf('/payment_methods'), null);

return json_decode($result['body'], true);
}

/**
* @param $path
* @param array|string|null $body
Expand Down
4 changes: 4 additions & 0 deletions src/Mondu/Mondu/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Gateway extends WC_Payment_Gateway {
* @var string|void
*/
protected $method_name;
/**
* @var MonduRequestWrapper
*/
private $mondu_request_wrapper;

public function __construct() {
$this->global_settings = get_option(Plugin::OPTION_NAME);
Expand Down
32 changes: 22 additions & 10 deletions src/Mondu/Mondu/MonduRequestWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@

class MonduRequestWrapper {

private $logger;

/** @var Api */
private $api;

public function __construct() {
$this->api = new Api();
$this->logger = wc_get_logger();
}

/**
Expand Down Expand Up @@ -146,12 +143,12 @@ public function get_invoices($order_id) {
return $response['invoices'];
}

/**
* @param $invoice_id
* @return mixed | void
* @throws MonduException
* @throws ResponseException
*/
/**
* @param $invoice_id
*
* @throws MonduException
* @throws ResponseException
*/
public function get_invoice($order_id) {
$order = new WC_Order($order_id);
if (!in_array($order->get_payment_method(), Plugin::PAYMENT_METHODS)) {
Expand All @@ -166,10 +163,25 @@ public function get_invoice($order_id) {
return @$response['invoice'];
}

/**
* @return array
* @throws MonduException
* @throws ResponseException
*/
public function get_merchant_payment_methods(): array {
$response = $this->api->get_payment_methods();

# return only an array with the identifier (invoice or direct_debit)
$merchant_payment_methods = array_map(function($payment_method) {
return $payment_method['identifier'];
}, @$response['payment_methods']);

return $merchant_payment_methods;
}

/**
* @param int $order_id
*
* @return array
* @throws MonduException
* @throws ResponseException
* @throws WC_Data_Exception
Expand Down
85 changes: 50 additions & 35 deletions src/Mondu/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Mondu\Mondu\Controllers\WebhooksController;
use Mondu\Mondu\Presenters\PaymentInfo;
use Exception;
use WC_Customer;
use WC_Order;

class Plugin {
Expand All @@ -33,10 +34,16 @@ class Plugin {
* @var array|bool|mixed|void
*/
protected $global_settings;
/**
* @var MonduRequestWrapper
*/
private $mondu_request_wrapper;

public function __construct() {
$this->global_settings = get_option(Plugin::OPTION_NAME);

$this->mondu_request_wrapper = new MonduRequestWrapper();

# This is for trigger the open checkout plugin
add_action('woocommerce_after_checkout_validation', function () {
if ($_POST['confirm-order-flag'] === '1') {
Expand Down Expand Up @@ -70,9 +77,9 @@ public function init() {
/*
* These deal with order and status changes
*/
add_action('woocommerce_order_status_changed', [new MonduRequestWrapper(), 'order_status_changed'], 10, 3);
add_action('woocommerce_before_order_object_save', [new MonduRequestWrapper(), 'update_order_if_changed_some_fields'], 10, 2);
add_action('woocommerce_order_refunded', [new MonduRequestWrapper(), 'order_refunded'], 10, 2);
add_action('woocommerce_order_status_changed', [$this->mondu_request_wrapper, 'order_status_changed'], 10, 3);
add_action('woocommerce_before_order_object_save', [$this->mondu_request_wrapper, 'update_order_if_changed_some_fields'], 10, 1);
add_action('woocommerce_order_refunded', [$this->mondu_request_wrapper, 'order_refunded'], 10, 2);

add_action('rest_api_init', function () {
$orders = new OrdersController();
Expand All @@ -89,15 +96,14 @@ public function init() {
}, 10, 3);

/*
* This one does not allow to change address
* Does not allow to change address
*/
add_action('woocommerce_admin_order_data_after_billing_address', [$this, 'change_address_warning'], 10, 1);

/*
* This one adds the status to a WCPDF Invoice if the order is cancelled
* These methods add the Mondu invoice's info to a WCPDF Invoice
*/
add_action('wpo_wcpdf_after_order_details', [$this, 'wcpdf_add_mondu_payment_info_to_pdf'], 10, 2);

add_action('wpo_wcpdf_after_order_data', [$this, 'wcpdf_add_status_to_invoice_when_order_is_cancelled'], 10, 2 );
add_action('wpo_wcpdf_after_order_data', [$this, 'wcpdf_add_paid_to_invoice_when_invoice_is_paid'], 10, 2 );
add_action('wpo_wcpdf_after_order_data', [$this, 'wcpdf_add_status_to_invoice_when_invoice_is_cancelled'], 10, 2 );
Expand All @@ -118,7 +124,6 @@ public function change_address_warning(WC_Order $order) {
echo '<p>' . __('Since this order will be paid via Mondu you will not be able to change the addresses.', 'mondu') . '</p>';
}

// This method needs to be public
public function add_mondu_js() {
if (is_checkout()) {
if ($this->is_sandbox()) {
Expand All @@ -129,35 +134,18 @@ public function add_mondu_js() {
}
}

/**
* @return bool
*/
private function is_sandbox() {
$sandbox_env = true;
if (
is_array($this->global_settings) &&
isset($this->global_settings['field_sandbox_or_production']) &&
$this->global_settings['field_sandbox_or_production'] === 'production'
) {
$sandbox_env = false;
}

return $sandbox_env;
}

/**
* @return bool
*/
// This method needs to be public
public function remove_mondu_outside_germany($available_gateways) {
if (WC()->customer->get_billing_country() == 'DE') {
return $available_gateways;
}
if (isset($available_gateways[Plugin::PAYMENT_METHODS['invoice']])) {
unset($available_gateways[Plugin::PAYMENT_METHODS['invoice']]);
}
if (isset($available_gateways[Plugin::PAYMENT_METHODS['direct_debit']])) {
unset($available_gateways[Plugin::PAYMENT_METHODS['direct_debit']]);
$mondu_payments = $this->mondu_request_wrapper->get_merchant_payment_methods();

foreach (Plugin::PAYMENT_METHODS as $payment_method => $woo_payment_method) {
if (
$this->is_outside_germany() ||
!in_array($payment_method, $mondu_payments)
) {
if (isset($available_gateways[Plugin::PAYMENT_METHODS[$payment_method]])) {
unset($available_gateways[Plugin::PAYMENT_METHODS[$payment_method]]);
}
}
}

return $available_gateways;
Expand Down Expand Up @@ -272,4 +260,31 @@ public function wcpdf_add_status_to_invoice_admin_when_invoice_is_cancelled($doc
<?php
}
}

/**
* @return bool
*/
private function is_sandbox() {
$sandbox_env = true;
if (
is_array($this->global_settings) &&
isset($this->global_settings['field_sandbox_or_production']) &&
$this->global_settings['field_sandbox_or_production'] === 'production'
) {
$sandbox_env = false;
}

return $sandbox_env;
}

/**
* @return bool
*/
private function is_outside_germany() {
$customer = new WC_Customer(get_current_user_id());
if ($customer->get_billing_country() == 'DE') {
return false;
}
return true;
}
}

0 comments on commit 1c1554c

Please sign in to comment.