Skip to content

Commit

Permalink
Merge pull request #68 from Mondido/MDD-5601
Browse files Browse the repository at this point in the history
Subscription fixes
  • Loading branch information
atimos authored Sep 27, 2021
2 parents 4839203 + 2767813 commit e0e2d3d
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 253 deletions.
104 changes: 4 additions & 100 deletions includes/abstracts/class-wc-gateway-mondido-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
} // Exit if accessed directly

abstract class WC_Gateway_Mondido_Abstract extends WC_Payment_Gateway {
protected $api;
protected $transaction;
protected $logger;

public function add_dependencies(WC_Mondido_Transaction $transaction) {
public function add_dependencies(WC_Mondido_Api $api, WC_Mondido_Transaction $transaction) {
$this->api = $api;
$this->transaction = $transaction;
}

Expand Down Expand Up @@ -128,54 +131,6 @@ public function captureTransaction( $transaction_id, $amount ) {
return json_decode(json_encode($result), true);
}

/**
* Get Subscription Plans
* @return array|mixed|object
* @throws \Exception
*/
public function getSubscriptionPlans() {
$result = wp_remote_get( 'https://api.mondido.com/v1/plans', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$this->merchant_id}:{$this->password}" )
)
) );

if ( is_a( $result, 'WP_Error' ) ) {
throw new Exception( implode( $result->errors['http_request_failed'] ) );
}

if ( $result['response']['code'] != 200 ) {
throw new Exception( $result['body'] );
}

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

/**
* Get Subscription Plan
* @param $plan_id
*
* @return array|mixed|object
* @throws Exception
*/
public function getSubscriptionPlan( $plan_id ) {
$result = wp_remote_get( 'https://api.mondido.com/v1/plans/' . $plan_id, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$this->merchant_id}:{$this->password}" )
)
) );

if ( is_a( $result, 'WP_Error' ) ) {
throw new Exception( implode( $result->errors['http_request_failed'] ) );
}

if ( $result['response']['code'] != 200 ) {
throw new Exception( $result['body'] );
}

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

/**
* Update Order with Incoming Products
*
Expand Down Expand Up @@ -599,57 +554,6 @@ public function getMondidoCustomerId( $customer_reference ) {
return false;
}

/**
* Get Mondido Subscriptions by Customer Id
* @param $customer_id
*
* @return array|mixed|object
* @throws Exception
*/
public function getMondidoSubscriptions( $customer_id ) {
$result = wp_remote_get( 'https://api.mondido.com/v1/customers/' . $customer_id . '/subscriptions', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$this->merchant_id}:{$this->password}" )
)
) );

if ( is_a( $result, 'WP_Error' ) ) {
throw new Exception( 'Failed to get subscriptions' );
}

$subscriptions = json_decode( $result['body'], TRUE );
if (isset($subscriptions['name']) && $subscriptions['name'] === 'errors.customer.not_found') {
throw new Exception( 'Customer not found' );
}

return $subscriptions;
}

/**
* Cancel Mondido Subscription
* @param $subscription_id
*
* @return array
* @throws Exception
*/
public function cancelMondidoSubscription( $subscription_id ) {
$result = wp_remote_request( 'https://api.mondido.com/v1/subscriptions/' . $subscription_id, array(
'method' => 'PUT',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$this->merchant_id}:{$this->password}" )
),
'body' => array(
'status' => 'cancelled'
)
) );

if ( is_a( $result, 'WP_Error' ) ) {
throw new Exception( 'Failed to cancel subscription' );
}

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

public function get_payment_method_name($value, $order, $default_value)
{
$transaction = get_post_meta( $order->get_id(), '_mondido_transaction_data', TRUE );
Expand Down
1 change: 0 additions & 1 deletion includes/class-wc-gateway-mondido-hw.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class WC_Gateway_Mondido_HW extends WC_Gateway_Mondido_Abstract {

protected $preselected_method = null;
protected $logger = null;

/**
* Init
Expand Down
32 changes: 32 additions & 0 deletions includes/class-wc-mondido-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ public function capture_transaction($transaction_id, $amount)
]);
}

public function list_plans() {
return $this->list_all(__METHOD__, "v1/plans", []);
}

public function list_customer_subscriptions($customer_id) {
return $this->get(__METHOD__, "v1/customers/$customer_id/subscriptions", ['extend' => 'plan']);
}

public function cancel_subscription($id) {
return $this->put(__METHOD__, "v1/subscriptions/$id", ['status' => 'cancelled']);
}

private function list_all($context, $path, $query) {
$query = $query + ['limit' => 100, 'offset' => 0];
$result = [];

if ($query['limit'] == 0) {
return $result;
}

do {
$items = $this->send_request('GET', $path, $context, $query, []);
if (is_wp_error($items)) {
return $items;
}
$result = array_merge($result, $items);
$query['offset'] += $query['limit'];
} while (count($items) === $query['limit']);

return $result;
}

private function get($context, $path, $query) {
return $this->send_request('GET', $path, $context, $query, []);
}
Expand Down
116 changes: 46 additions & 70 deletions includes/class-wc-mondido-subscriptions-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
} // Exit if accessed directly

class WC_Mondido_Subscriptions_Account {
private $api;
private $gateway;
/**
* Constructor
*/
public function __construct() {
public function __construct($api, $gateway) {
$this->api = $api;
$this->gateway = $gateway;

add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
add_filter( 'woocommerce_account_menu_items', array( $this, 'menu_items' ) );
add_filter( 'the_title', array( $this, 'endpoint_title' ) );
add_action( 'woocommerce_account_mondido-subscriptions_endpoint', array( $this, 'endpoint_content' ) );
add_action( 'after_switch_theme', array( __CLASS__, 'flush_rewrite_rules' ) );


add_action( 'after_switch_theme', array( $this, 'flush_rewrite_rules' ) );

add_action( 'wp_ajax_mondido_cancel_subscription', array( $this, 'cancel_subscription' ) );
}
Expand Down Expand Up @@ -56,7 +59,9 @@ public function flush_rewrite_rules() {
* @return array
*/
public function menu_items( $items ) {
$items['mondido-subscriptions'] = __( 'Mondido Subscriptions', 'woocommerce-gateway-mondido' );
if (count($this->get_subscriptions()) > 0) {
$items['mondido-subscriptions'] = __( 'Mondido Subscriptions', 'woocommerce-gateway-mondido' );
}

return $items;
}
Expand Down Expand Up @@ -88,92 +93,63 @@ public function endpoint_title( $title ) {
* Endpoint HTML content
*/
public function endpoint_content() {
$user_id = get_current_user_id();

$subscriptions = array();
$references = $this->getMondidoGateway()->getCustomerReferences( $user_id );
foreach ( $references as $reference ) {
$customer_id = $this->getMondidoGateway()->getMondidoCustomerId( $reference['customer_reference'] );
if ( ! $customer_id ) {
continue;
}

$result = $this->getMondidoGateway()->getMondidoSubscriptions( $customer_id );
$subscriptions = array_merge($subscriptions, $result);
}

wc_get_template(
'myaccount/mondido-subscriptions.php',
array(
'user_id' => $user_id,
'subscriptions' => $subscriptions,
'subscriptions' => array_reverse($this->get_subscriptions()),
),
'',
dirname( __FILE__ ) . '/../templates/'
);
}

/**
* Get Mondido Gateway Instance
* @return WC_Gateway_Mondido_HW
* Cancel Mondido Subscription
*/
public static function getMondidoGateway()
{
$payment_gateways = WC()->payment_gateways->payment_gateways();
return $payment_gateways['mondido_hw'];
}
public function cancel_subscription() {
if ( !is_user_logged_in() || ! check_ajax_referer( 'mondido_subscriptions', 'nonce', false ) ) {
exit( 'No naughty business' );
}

/**
* Get Subscription Plan
* @param $plan_id
*
* @return array|bool|mixed|object
*/
public static function getSubscriptionPlan($plan_id)
{
try {
$result = self::getMondidoGateway()->getSubscriptionPlan( $plan_id );
} catch (Exception $e) {
return false;
$customer_subscriptions = $this->get_subscriptions();

foreach ($customer_subscriptions as $subscription) {
if ($subscription->id === (int) $_POST['id']) {
$result = $this->api->cancel_subscription( $subscription->id );
if (!is_wp_error($result) && $result->stauts === 'cancelled') {
wp_send_json_success( __( 'Success', 'woocommerce-gateway-mondido' ) );
return;
} else {
break;
}
}
}

return $result;
wp_send_json_error( __( 'Failed to cancel subscription', 'woocommerce-gateway-mondido' ) );
}

/**
* Format Subscription Description
* @param array $subscription
*
* @return string
*/
public static function formatSubscriptionDescription( array $subscription )
{
$plan_id = $subscription['plan']['id'];
$plan = self::getSubscriptionPlan( $plan_id );
if ( ! $plan ) {
return sprintf( __( 'Subscription #%s', 'woocommerce-gateway-mondido' ), $subscription['id'] );
private function get_subscriptions() {
if (!is_user_logged_in()) {
return [];
}
$user_id = get_current_user_id();

$description = ( ! empty( $plan['description'] ) ? '(' . $plan['description'] . ')' : '');
return sprintf( __( '%s %s (Subscription #%s)', 'woocommerce-gateway-mondido' ), $plan['name'], $description, $subscription['id'] );
}
$subscriptions = [];
$references = $this->gateway->getCustomerReferences( $user_id );
foreach ( $references as $reference ) {
$customer_id = $this->gateway->getMondidoCustomerId( $reference['customer_reference'] );
if ( ! $customer_id ) {
continue;
}

/**
* Cancel Mondido Subscription
*/
public function cancel_subscription() {
if ( ! check_ajax_referer( 'mondido_subscriptions', 'nonce', false ) ) {
exit( 'No naughty business' );
$result = $this->api->list_customer_subscriptions( $customer_id );
if (is_wp_error($result)) {
return [];
}
$subscriptions = array_merge($subscriptions, $result);
}

$subscription_id = wc_clean( $_POST['id'] );
$result = self::getMondidoGateway()->cancelMondidoSubscription( $subscription_id );
if ($result['status'] === 'cancelled') {
wp_send_json_success( __( 'Success', 'woocommerce-gateway-mondido' ) );
} else {
wp_send_json_error( __( 'Failed to cancel subscription', 'woocommerce-gateway-mondido' ) );
}
return $subscriptions;
}
}

new WC_Mondido_Subscriptions_Account();
Loading

0 comments on commit e0e2d3d

Please sign in to comment.