jforrest / Chargify-PHP-Client

PHP client for Chargify
Please let me know if you have any questions.
MIT License
19 stars 16 forks source link

There is not provide functionality for qty allocation #19

Closed dhaval24071991 closed 8 years ago

dhaval24071991 commented 8 years ago

Please Review this changes and merge with your code so it is useful for futute.

**_Chargify Allocation php file _****

//Reference Documentation:Allocation api

class ChargifyAllocation extends ChargifyBase { //** //\ INPUT & OUTPUT VARIABLES //**** var $quantity; var $memo;

//******************************
//*** OUTPUT ONLY VARIABLES ****
//******************************    
var $id;

private $connector;
public function __construct(SimpleXMLElement $allocation_xml_node = null, $test_mode = false)
{
    $this->connector = new ChargifyConnector($test_mode);
    if ($allocation_xml_node) {
        //Load object dynamically and convert SimpleXMLElements into strings
        foreach($allocation_xml_node as $key => $element) { 
            $this->$key = (string)$element; 
        }
    }
}

protected function getName() {
    return "allocation";
}

public function create($subscription_id, $component_id) {
    return $this->connector->createQtyComponent($subscription_id, $component_id, $this);
}}

Add two functions in ChargifyConnector.php

public function createQtyComponent($subscription_id, $component_id, $chargify_usage) {
    $xml = $this->requestQtyComponent($subscription_id, $component_id,  $chargify_usage->getXML());
    $usage = new SimpleXMLElement($xml);
    return new ChargifyUsage($usage, $this->test_mode);
}

public function requestQtyComponent($subscription_id, $component_id, $componentRequest, $format = 'XML') {
    $extension = strtoupper($format) == 'XML' ? '.xml' : '.json';
    $base_url = "/subscriptions/{$subscription_id}/components/{$component_id}/allocations";

    $xml = $this->sendRequest($base_url, $format, 'POST', $componentRequest);

    if ($xml->code == 201) { //CREATED
        return $xml->response;
    } elseif ($xml->code == 422) { //UNPROCESSABLE ENTITY
        $errors = new SimpleXMLElement($xml->response);
        throw new ChargifyValidationException($xml->code, $errors);
    }
}

ChargifyAllocation.txt ChargifyConnector.txt

dhaval24071991 commented 8 years ago

Please review above changes for update qty based component chargify allocation qty