Jump to content

Coupon API


FlamesRunner

Recommended Posts

Hello,

I've been trying to work with the Blesta PHP API library for a bit and I stumbled on an issue while trying to interface with it. I am currently using the following code:

<?php
    require_once "blesta_api.php";
    require_once "blesta_response.php";
    $user = "...";
    $key = "...";
    $url = "https://(snipped)/api";
    $api = new BlestaApi($url, $user, $key);
    $code = bin2hex(openssl_random_pseudo_bytes(5));
    $coupon_data = array(
            "code" => $code,
            "company_id" => 1,
            "used_qty" => 0,
            "max_qty" => 1,
            "start_date" => date("c"),
            "end_date" => "2025-12-30 11:59:59 UTC",
            "status" => "active",
            "recurring" => 0,
            "limit_recurring" => 0,
            "packages" => array(1, 2),
            "amounts" => array(
                    "currency" => "USD",
                    "amount" => 50,
                    "type" => "percent"
            )
    );
    $response = $api->post("coupons", "add", $coupon_data);
    echo var_dump($response);
?>

There doesn't seem to be an issue as when I execute this, I receive no errors back. However, the response is empty, and the coupon API suggests that it will respond with an integer. Am I doing something wrong?

Link to comment
Share on other sites

You are probably getting errors, or the API request could not be made. You would only get an integer response if the action was successful.

$response = $api->post("coupons", "add", $coupon_data);

if ($response->errors() !== false) {
    // There were errors attempting that action
    print_r($response->errors());
} elseif (($result = $response->response())) {
    // The response was successful and returned data
    print_r($result);
} else {
    // Something else happened
    echo "Response code: " . $response->responseCode();
    var_dump($response->raw());
}

 

Link to comment
Share on other sites

@Tyson

I appreciate the reply. I figured out what was causing the empty responses; turns out the URL requires a trailing slash. I don't believe there's anything wrong with my system, as I was able to obtain information about users, etc (using $response = $api->get("users", "get", array("user_id" => 1)); for example works and responds with data). However, when I attempt to use the coupon API to add something, I now get the following error: 

object(stdClass)#5 (1) { ["error"]=> object(stdClass)#6 (2) { ["message"]=> string(28) "An unexpected error occured." ["response"]=> string(52) "Internal error: Failed to retrieve the default value" } }

At this time, I'm also able to retrieve existing coupons and query for data. However, it is only the part that adds coupons that does not work.

I've also tried running it on another server, to no effect. Perhaps there's a bug, or I'm missing something?

 

Thanks,

Andrew

Link to comment
Share on other sites

Turns out I needed to encapsulate the array data with another keyed array called "vars."

For anyone who stumbles on this in the future, the following code will work:

<?php
    require_once "blesta_api.php";
    require_once "blesta_response.php";
    $user = "...";
    $key = "...";
    $url = "https://(snipped)/api";
    $api = new BlestaApi($url, $user, $key);
    $code = bin2hex(openssl_random_pseudo_bytes(5));
    $coupon_data = array(
	        "vars" => array(
                    "code" => $code,
                    "company_id" => 1,
                    "used_qty" => 0,
                    "max_qty" => 1,
                    "start_date" => date("c"),
                    "end_date" => "2025-12-30 11:59:59 UTC",
                    "status" => "active",
                    "recurring" => 0,
                    "limit_recurring" => 0,
                    "packages" => array(1, 2),
                    "amounts" => array(
                            "currency" => "USD",
                            "amount" => 50,
                            "type" => "percent"
                    )
	        )
    );
    $response = $api->post("coupons", "add", $coupon_data);
?>

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...