Skip to content

Commit

Permalink
Merge pull request #190 from antondrob/master
Browse files Browse the repository at this point in the history
v 1.4.49 - Keys info feature
  • Loading branch information
markusbrunke authored Jan 23, 2023
2 parents c3556cb + ce7b82e commit 6a76258
Show file tree
Hide file tree
Showing 7 changed files with 562 additions and 237 deletions.
3 changes: 3 additions & 0 deletions Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ The Reepay plugin extends WooCommerce allowing you to take payments on your stor
See installation guide right here: https://intercom.help/reepay/reepay-plugins/woocommerce-plugin

== Changelog ==
v 1.4.51 - Fix invoice email settle
v 1.4.50 - Fix capture exception
v 1.4.49 - Keys info feature
v 1.4.48 - Subscriptions widget
v 1.4.47 - Enable sync default
v 1.4.46 - Fix status gateway
Expand Down
130 changes: 113 additions & 17 deletions includes/abstracts/abstract-wc-gateway-reepay.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,124 @@ public function __construct() {
add_action( 'wp_ajax_nopriv_reepay_cancel_payment', array( $this, 'reepay_cancel_payment' ) );
}

public function check_is_active() {
$gateway = new WC_Gateway_Reepay_Checkout();
$this->api = new WC_Reepay_Api( $gateway );

$gateways_reepay = get_transient( 'gateways_reepay' );
if ( empty( $gateways_reepay ) ) {
$gateways_reepay = $this->api->request( 'GET', 'https://api.reepay.com/v1/agreement?only_active=true' );
set_transient( 'gateways_reepay', $gateways_reepay, 5 );
}

$current_name = str_replace( 'reepay_', '', $this->id );


if ( ! empty( $gateways_reepay ) ) {
foreach ( $gateways_reepay as $app ) {
if ( stripos( $app['type'], $current_name ) !== false ) {
return true;
}
}
}

return false;
}

public function is_configured() {
$configured = false;

if ( isset( $_GET['tab'] ) && $_GET['tab'] == 'checkout' && ! empty( $_GET['section'] ) ) {
$gateway = new WC_Gateway_Reepay_Checkout();
$this->api = new WC_Reepay_Api( $gateway );
$gateways_app = $this->api->request( 'GET', 'https://api.reepay.com/v1/agreement?only_active=true' );
if ( isset( $_GET['tab'] ) && $_GET['tab'] == 'checkout' && ! empty( $_GET['section'] ) && $_GET['section'] != 'reepay_checkout' ) {
$configured = $this->check_is_active();
}

$current_name = str_replace( 'reepay_', '', $this->id );
return $configured;
}


if ( ! empty( $gateways_app ) ) {
foreach ( $gateways_app as $app ) {
if ( stripos( $app['type'], $current_name ) !== false ) {
$configured = true;
}
public function get_account_info( $gateway ) {
if ( isset( $_GET['tab'] ) && $_GET['tab'] == 'checkout' && ! empty( $_GET['section'] ) && $_GET['section'] == 'reepay_checkout' ) {

$this->api = new WC_Reepay_Api( $gateway );

$account_info = get_transient( 'account_info' );
if ( empty( $account_info ) ) {
$account_info = $this->api->request( 'GET', 'https://api.reepay.com/v1/account' );
set_transient( 'account_info', $account_info, 5 );
}

return $account_info;
}

return [];

}

public function is_webhook_configured( $gateway ) {
try {
$this->api = new WC_Reepay_Api( $gateway );
$result = $this->api->request( 'GET', 'https://api.reepay.com/v1/account/webhook_settings' );
if ( is_wp_error( $result ) ) {
/** @var WP_Error $result */
throw new Exception( $result->get_error_message(), $result->get_error_code() );
}

// The webhook settings
$urls = $result['urls'];
$alert_emails = $result['alert_emails'];

// The webhook settings of the payment plugin
$webhook_url = WC()->api_request_url( get_class() );
$alert_email = '';
if ( ! empty( $this->settings['failed_webhooks_email'] ) &&
is_email( $this->settings['failed_webhooks_email'] )
) {
$alert_email = $this->settings['failed_webhooks_email'];
}

// Verify the webhook settings
if ( in_array( $webhook_url, $urls ) &&
( ! empty( $alert_email ) ? in_array( $alert_email, $alert_emails ) : true )
) {
// Skip the update
return true;
}

// Update the webhook settings
try {
$urls[] = $webhook_url;

if ( ! empty( $alert_email ) && is_email( $alert_email ) ) {
$alert_emails[] = $alert_email;
}

$data = array(
'urls' => array_unique( $urls ),
'disabled' => false,
'alert_emails' => array_unique( $alert_emails )
);
$this->test_mode = 'yes';
$result = $this->api->request( 'PUT', 'https://api.reepay.com/v1/account/webhook_settings', $data );
if ( is_wp_error( $result ) ) {
/** @var WP_Error $result */
throw new Exception( $result->get_error_message(), $result->get_error_code() );
}

return true;
} catch ( Exception $e ) {
return false;
}
} catch ( Exception $e ) {
return false;
}
}

public function needs_setup() {
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'woocommerce_toggle_gateway_enabled' ) {
return ! $this->check_is_active();
}

return $configured;
return false;
}

/**
Expand Down Expand Up @@ -613,7 +709,7 @@ public function process_payment( $order_id ) {
// Initialize Payment
$params = [
'locale' => $this->get_language(),
'recurring' => apply_filters('order_contains_reepay_subscription', $maybe_save_card || order_contains_subscription( $order ) || wcs_is_payment_change(), $order),
'recurring' => apply_filters( 'order_contains_reepay_subscription', $maybe_save_card || order_contains_subscription( $order ) || wcs_is_payment_change(), $order ),
'order' => [
'handle' => $order_handle,
'amount' => $this->skip_order_lines === 'yes' ? rp_prepare_amount( $order->get_total(), $order->get_currency() ) : null,
Expand Down Expand Up @@ -728,7 +824,7 @@ public function process_payment( $order_id ) {
if ( abs( $order->get_total() ) < 0.01 ) {
// Don't charge payment if zero amount
if ( wcs_cart_only_subscriptions() ) {
$result = $this->api->recurring( $this->payment_methods, $order, $data, $token->get_token(), $params['button_text'] );
$result = $this->api->recurring( $this->payment_methods, $order, $data, $token->get_token(), $params['button_text'] );

if ( is_wp_error( $result ) ) {
/** @var WP_Error $result */
Expand Down Expand Up @@ -827,7 +923,7 @@ public function process_payment( $order_id ) {
// If here's Subscription or zero payment
if ( ( wc_cart_only_reepay_subscriptions() || wcs_cart_only_subscriptions() ) && ( abs( $order->get_total() ) < 0.01 || empty( $params['order']['order_lines'] ) ) ) {

$result = $this->api->recurring( $this->payment_methods, $order, $data, false, $params['button_text'] );
$result = $this->api->recurring( $this->payment_methods, $order, $data, false, $params['button_text'] );

if ( is_wp_error( $result ) ) {
/** @var WP_Error $result */
Expand Down Expand Up @@ -859,9 +955,9 @@ public function process_payment( $order_id ) {
}

public function process_session_charge( $params, $order ) {
if (empty($params['button_text'])) {
$params['button_text'] = strip_tags(__('Pay', 'reepay-checkout-gateway') . ' ' . $order->get_formatted_order_total());
}
if ( empty( $params['button_text'] ) ) {
$params['button_text'] = strip_tags( __( 'Pay', 'reepay-checkout-gateway' ) . ' ' . $order->get_formatted_order_total() );
}
$result = $this->api->request(
'POST',
'https://checkout-api.reepay.com/v1/session/charge',
Expand Down
15 changes: 14 additions & 1 deletion includes/class-wc-gateway-reepay-capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function unset_specific_order_item_meta_data( $formatted_meta, $item ) {
// Only on emails notifications


if ( is_admin() ) {
if ( is_admin() && isset( $_GET['post'] ) ) {
foreach ( $formatted_meta as $key => $meta ) {
if ( in_array( $meta->key, array( 'settled' ) ) ) {
$meta->display_key = 'Settle';
Expand Down Expand Up @@ -242,7 +242,14 @@ public function get_no_settled_amount( $order ) {
}

public function capture_full_order_button( $order ) {
$payment_method = $order->get_payment_method();

if ( strpos( $payment_method, 'reepay' ) === false ) {
return;
}

$gateway = rp_get_payment_method( $order );

if ( ! empty( $gateway ) && ! empty( $gateway->api ) ) {
$order_data = $gateway->api->get_invoice_data( $order );
if ( ! is_wp_error( $order_data ) ) {
Expand All @@ -265,6 +272,12 @@ public function add_item_capture_button( $item_id, $item, $product ) {
$order_id = wc_get_order_id_by_order_item_id( $item_id );
$order = wc_get_order( $order_id );

$payment_method = $order->get_payment_method();

if ( strpos( $payment_method, 'reepay' ) === false ) {
return;
}

$settled = $item->get_meta( 'settled' );
$data = $item->get_data();

Expand Down
Loading

0 comments on commit 6a76258

Please sign in to comment.