This is my ChargifyCoupon.php
public function create($product_family_id = null, $details = []) {
$details = array_replace_recursive([
"name" => "15% off",
"code" => "15OFF",
"description" => "15% off for life",
"percentage" => "15",
"allow_negative_balance" => "false",
"recurring" => "false",
"end_date" => "2016-12-31T23:59:59-04:00",
"product_family_id" => $product_family_id
], $details);
$returnData = $this->connector->createCoupon($details, 'JSON');
var_dump($returnData);
die;
}
This is my ChargifyConnector.php
function createCoupon($product_family_id, $coupon_details = [], $format = 'XML') {
$extension = strtoupper($format) == 'XML' ? '.xml' : '.json';
$base_url = "/product_families/{$product_family_id}/coupons" . $extension;
$data = json_encode([
"coupon" => $coupon_details
]);
$coupon = $this->sendRequest($base_url, $format, 'POST', $data);
if ($coupon->code == 200) {
return $coupon->response;
} elseif ($coupon->code == 404 || $coupon->code == 500) {
var_dump($coupon);
die;
throw new \Exception($coupon->code, "Coupon was not created.");
}
}
When i'm trying to see result i'm getting "array to string conversion"
Sorry for late reply..
After so much research on chargify coupon,
simply remove /product_families/{$product_family_id}.
no need to send coupon data with product_families/{$product_family_id}.
For more details see HERE.
Updated Code :
function createCoupon($product_family_id, $coupon_details = [], $format = 'XML') {
$extension = strtoupper($format) == 'XML' ? '.xml' : '.json';
// $base_url = "/product_families/{$product_family_id}/coupons" . $extension;
$base_url = "/coupons" . $extension;
$data = json_encode([
"coupon" => $coupon_details
]);
$coupon = $this->sendRequest($base_url, $format, 'POST', $data);
if ($coupon->code == 200) {
return $coupon->response;
} elseif ($coupon->code == 404 || $coupon->code == 500) {
// var_dump($coupon);
// die;
throw new \Exception($coupon->code, "Coupon was not created.");
}
}