예제 #1
0
/**
 * Update a discount in Stripe when a local code is updated
 *
 * @access      private
 * @param       $discount_id int the id of the discount being updated
 * @param       $args array the array of discount args
 *              array(
 *					'name',
 *					'description',
 *					'amount',
 *					'unit',
 *					'code',
 *					'status',
 *					'expiration',
 *					'max_uses',
 *					'subscription_id'
 *				)
 * @since       2.1
 */
function rcp_stripe_update_discount($discount_id, $args)
{
    if (!is_admin()) {
        return;
    }
    // bail if the discount id or args are empty
    if (empty($discount_id) || empty($args)) {
        return;
    }
    if (function_exists('rcp_stripe_add_discount')) {
        return;
        // Old Stripe gateway is active
    }
    if (!rcp_is_gateway_enabled('stripe') && !rcp_is_gateway_enabled('stripe_checkout')) {
        return;
    }
    global $rcp_options;
    if (!class_exists('Stripe\\Stripe')) {
        require_once RCP_PLUGIN_DIR . 'includes/libraries/stripe/init.php';
    }
    if (!empty($_REQUEST['deactivate_discount']) || !empty($_REQUEST['activate_discount'])) {
        return;
    }
    if (rcp_is_sandbox()) {
        $secret_key = trim($rcp_options['stripe_test_secret']);
    } else {
        $secret_key = trim($rcp_options['stripe_live_secret']);
    }
    \Stripe\Stripe::setApiKey($secret_key);
    $discount_details = rcp_get_discount_details($discount_id);
    $discount_name = $discount_details->code;
    if (!rcp_stripe_does_coupon_exists($discount_name)) {
        try {
            if ($args['unit'] == '%') {
                \Stripe\Coupon::create(array("percent_off" => sanitize_text_field($args['amount']), "duration" => "forever", "id" => sanitize_text_field($discount_name), "currency" => strtolower($rcp_options['currency'])));
            } else {
                \Stripe\Coupon::create(array("amount_off" => sanitize_text_field($args['amount']) * rcp_stripe_get_currency_multiplier(), "duration" => "forever", "id" => sanitize_text_field($discount_name), "currency" => strtolower($rcp_options['currency'])));
            }
        } catch (Exception $e) {
            wp_die('<pre>' . $e . '</pre>', __('Error', 'rcp'));
        }
    } else {
        // first delete the discount in Stripe
        try {
            $cpn = \Stripe\Coupon::retrieve($discount_name);
            $cpn->delete();
        } catch (Exception $e) {
            wp_die('<pre>' . $e . '</pre>', __('Error', 'rcp'));
        }
        // now add a new one. This is a fake "update"
        try {
            if ($args['unit'] == '%') {
                \Stripe\Coupon::create(array("percent_off" => sanitize_text_field($args['amount']), "duration" => "forever", "id" => sanitize_text_field($discount_name), "currency" => strtolower($rcp_options['currency'])));
            } else {
                \Stripe\Coupon::create(array("amount_off" => sanitize_text_field($args['amount']) * rcp_stripe_get_currency_multiplier(), "duration" => "forever", "id" => sanitize_text_field($discount_name), "currency" => strtolower($rcp_options['currency'])));
            }
        } catch (\Stripe\Error\InvalidRequest $e) {
            // Invalid parameters were supplied to Stripe's API
            $body = $e->getJsonBody();
            $err = $body['error'];
            $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>';
            if (isset($err['code'])) {
                $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>';
            }
            $error .= "<p>Status: " . $e->getHttpStatus() . "</p>";
            $error .= "<p>Message: " . $err['message'] . "</p>";
            wp_die($error, __('Error', 'rcp'), array('response' => 401));
        } catch (\Stripe\Error\Authentication $e) {
            // Authentication with Stripe's API failed
            // (maybe you changed API keys recently)
            $body = $e->getJsonBody();
            $err = $body['error'];
            $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>';
            if (isset($err['code'])) {
                $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>';
            }
            $error .= "<p>Status: " . $e->getHttpStatus() . "</p>";
            $error .= "<p>Message: " . $err['message'] . "</p>";
            wp_die($error, __('Error', 'rcp'), array('response' => 401));
        } catch (\Stripe\Error\ApiConnection $e) {
            // Network communication with Stripe failed
            $body = $e->getJsonBody();
            $err = $body['error'];
            $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>';
            if (isset($err['code'])) {
                $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>';
            }
            $error .= "<p>Status: " . $e->getHttpStatus() . "</p>";
            $error .= "<p>Message: " . $err['message'] . "</p>";
            wp_die($error, __('Error', 'rcp'), array('response' => 401));
        } catch (\Stripe\Error\Base $e) {
            // Display a very generic error to the user
            $body = $e->getJsonBody();
            $err = $body['error'];
            $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>';
            if (isset($err['code'])) {
                $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>';
            }
            $error .= "<p>Status: " . $e->getHttpStatus() . "</p>";
            $error .= "<p>Message: " . $err['message'] . "</p>";
            wp_die($error, __('Error', 'rcp'), array('response' => 401));
        } catch (Exception $e) {
            // Something else happened, completely unrelated to Stripe
            $error = '<p>' . __('An unidentified error occurred.', 'rcp') . '</p>';
            $error .= print_r($e, true);
            wp_die($error, __('Error', 'rcp'), array('response' => 401));
        }
    }
}
예제 #2
0
<?php

$code = rcp_get_discount_details(urldecode($_GET['edit_discount']));
?>
<h2>
	<?php 
_e('Edit Discount Code:', 'rcp');
echo ' ' . $code->name;
?>
	<a href="<?php 
echo admin_url('/admin.php?page=rcp-discounts');
?>
" class="add-new-h2">
		<?php 
_e('Cancel', 'rcp');
?>
	</a>
</h2>
<form id="rcp-edit-discount" action="" method="post">
	<table class="form-table">
		<tbody>
			<tr class="form-field">
				<th scope="row" valign="top">
					<label for="rcp-name"><?php 
_e(' Name', 'rcp');
?>
</label>
				</th>
				<td>
					<input name="name" id="rcp-name" type="text" value="<?php 
echo esc_html(stripslashes($code->name));
예제 #3
0
function rcp_get_discount_status($code_id)
{
    global $wpdb, $rcp_discounts_db_name;
    $code = rcp_get_discount_details($code_id);
    if ($code) {
        return $code->status;
    }
    return false;
}