André Castro
André Castro

Reputation: 1565

Stripe API create SKU

I need to create a SKU via stripe API.

The problem is in inventory field. Stripe api response is:

'error' => [
    'message' => 'Invalid hash',
    'param' => 'inventory',
    'type' => 'invalid_request_error'
]

My php code is:

    $endPoint = 'https://api.stripe.com/v1/skus';
    $APIKEY_TEST = 'my_api_key';
    $headers = array('Authorization: Bearer '.$APIKEY_TEST);

    $sku = [
        'active' => 'true',
        'inventory' => ['quantity' => 10000000 ,'type' => 'infinite', 'value' => null],
        "currency" => "eur",
        "price" => $price,
        "product" => $stripe_product_id
    ];

    $array_string ='';
    foreach($sku as $key => $value) {
        $array_string .= $key.'='.$value.'&';
    }
    rtrim($array_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $endPoint);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $array_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);

In stripe api docs inventory is hash type field. I have tried json_encode() without luck. Maybe the problem is in sending an array instead of a hash. In $sku array, inventory field is also an nested associative array. Maybe the problem resides there as well.

Is there a way to send CURLOPT_POSTFIELDS containing inventory so that stripe accepts it?

EDIT: In Stripe dashboard i can see my request:

  {
  "active": "true",
  "inventory": "Array",
  "currency": "eur",
  "price": "3",
  "product": "prod_F6ipvfYFvOxxQq"
}

Inventory field has no data, but instead "Array".

Upvotes: 0

Views: 552

Answers (1)

André Castro
André Castro

Reputation: 1565

After trying a lot of possible solutions, i found the answer:

$post_array = http_build_query($sku);

And know stripe accepts the $sku array with nested inventory array.

It worth notice that stripe does not accept JSON in requests.

The request has to be url encoded.

Upvotes: 1

Related Questions