如何使用chargify API创建优惠券 - PHP / Laravel

时间:2016-06-27 06:06:43

标签: php api laravel coupon chargify

这是我的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;

    }

这是我的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.");
        }

    }

当我试图查看结果时,我会得到"数组到字符串转换"

1 个答案:

答案 0 :(得分:0)

很抱歉迟到的回复..

经过如此多的关于chargify优惠券的研究,

只需删除/ product_families / {$ product_family_id}。

无需使用product_families / {$ product_family_id}发送优惠券数据。

有关详细信息,请参阅HERE

更新代码:

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.");
    }       
}