1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Controller_Plan extends WC_Braintree_Rest_Controller {
protected $namespace = 'plans/';
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'fetch',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_plans' ),
'permission_callback' => array( $this, 'admin_permission_check' )
)
);
}
public function get_plans( $request ) {
$envs = array( 'sandbox', 'production' );
foreach ( $envs as $env ) {
if ( ( $gateway = braintree()->gateway( $env ) ) !== null ) {
try {
$subscription_plans = array();
$plans = $gateway->plan()->all();
foreach ( $plans as $plan ) {
$subscription_plans[ $plan->id ] = array(
'id' => $plan->id,
'name' => $plan->name,
'numberOfBillingCycles' => $plan->numberOfBillingCycles,
'billingDayOfMonth' => $plan->billingDayOfMonth,
'billingFrequency' => $plan->billingFrequency,
'createdAt' => $plan->createdAt,
'currencyIsoCode' => $plan->currencyIsoCode,
'description' => $plan->description,
'price' => $plan->price,
'trialDuration' => $plan->price,
'trialDurationUnit' => $plan->trialDurationUnit,
'trialPeriod' => $plan->trialPeriod,
'updatedAt' => $plan->updatedAt,
);
}
update_option( "wc_braintree_{$env}_plans", $subscription_plans );
} catch ( \Braintree\Exception $e ) {
$message = sprintf( __( 'Error fetching plans. Environment: %1$s. Reason: %2$s', 'woo-payment-gateway' ), $env, wc_braintree_errors_from_object( $e ) );
wc_braintree_log_error( $message );
return new WP_Error( 'plan-error', $message, array( 'status' => 200 ) );
} catch ( Exception $e ) {
$message = sprintf( __( 'Error fetching plans. Environment: %1$s. Message: %2$s', 'woo-payment-gateway' ), $e->getMessage() );
wc_braintree_log_error( $message );
return new WP_Error( 'plan-error', $message, array( 'status' => 200 ) );
}
}
}
return rest_ensure_response(
array(
'sandbox' => get_option( 'wc_braintree_sandbox_plans', array() ),
'production' => array(
get_option(
'wc_braintree_production_plans',
array()
),
),
)
);
}
}