-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support open amount and line item refunds #133
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
HTTP/1.1 201 Created | ||
Content-Type: application/xml; charset=utf-8 | ||
Location: https://api.recurly.com/v2/invoices/created-invoice | ||
|
||
<invoice href="https://api.recurly.com/v2/invoices/1001"> | ||
<account href="https://api.recurly.com/v2/accounts/1"/> | ||
<subscription href="https://api.recurly.com/v2/subscriptions/1234567890abcdef"/> | ||
<uuid>012345678901234567890123456789aa</uuid> | ||
<state>collected</state> | ||
<invoice_number type="integer">abcdef1234567890</invoice_number> | ||
<po_number></po_number> | ||
<vat_number></vat_number> | ||
<subtotal_in_cents type="integer">-1000</subtotal_in_cents> | ||
<tax_in_cents type="integer">0</tax_in_cents> | ||
<total_in_cents type="integer">2995</total_in_cents> | ||
<currency>USD</currency> | ||
<created_at type="datetime">2012-05-28T17:44:13Z</created_at> | ||
<closed_at type="datetime">2012-05-28T17:44:13Z</closed_at> | ||
<net_terms type="integer">0</net_terms> | ||
<collection_method>automatic</collection_method> | ||
</invoice> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,47 @@ public function invoiceNumberWithPrefix() { | |
return $this->invoice_number_prefix . $this->invoice_number; | ||
} | ||
|
||
/** | ||
* Refunds an open amount from the invoice and returns a new refund invoice | ||
* @param Integer amount in cents to refund from this invoice | ||
* @return Recurly_Invoice a new refund invoice | ||
*/ | ||
public function refundAmount($amount_in_cents) { | ||
$doc = $this->createDocument(); | ||
|
||
$root = $doc->appendChild($doc->createElement($this->getNodeName())); | ||
$root->appendChild($doc->createElement('amount_in_cents', $amount_in_cents)); | ||
|
||
return $this->createRefundInvoice($this->renderXML($doc)); | ||
} | ||
|
||
/** | ||
* Refunds given line items from an invoice and returns new refund invoice | ||
* @param Array refund attributes or Array of refund attributes to refund (see 'REFUND ATTRIBUTES' in docs or Recurly_Adjustment#toRefundAttributes) | ||
* @return Recurly_Invoice a new refund invoice | ||
*/ | ||
public function refund($line_items) { | ||
if (isset($line_items['uuid'])) { $line_items = array($line_items); } | ||
|
||
$doc = $this->createDocument(); | ||
|
||
$root = $doc->appendChild($doc->createElement($this->getNodeName())); | ||
$line_items_node = $root->appendChild($doc->createElement('line_items')); | ||
|
||
foreach ($line_items as $line_item) { | ||
$adjustment_node = $line_items_node->appendChild($doc->createElement('adjustment')); | ||
$adjustment_node->appendChild($doc->createElement('uuid', $line_item['uuid'])); | ||
$adjustment_node->appendChild($doc->createElement('quantity', $line_item['quantity'])); | ||
$adjustment_node->appendChild($doc->createElement('prorate', $line_item['prorate'] ? 'true' : 'false')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any way we can provide defaults for the quantity and proration? |
||
} | ||
|
||
return $this->createRefundInvoice($this->renderXML($doc)); | ||
} | ||
|
||
protected function createRefundInvoice($xml_string) { | ||
return self::_post($this->uri() . '/refund', $xml_string, $this->_client); | ||
} | ||
|
||
protected function getNodeName() { | ||
return 'invoice'; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,11 +91,19 @@ protected function _save($method, $uri) | |
|
||
public function xml() | ||
{ | ||
$doc = new DOMDocument("1.0"); | ||
$doc = $this->createDocument(); | ||
$root = $doc->appendChild($doc->createElement($this->getNodeName())); | ||
$this->populateXmlDoc($doc, $root, $this); | ||
// To be able to consistently run tests across different XML libraries, | ||
// favor `<foo></foo>` over `<foo/>`. | ||
return $this->renderXML($doc); | ||
} | ||
|
||
public function createDocument() { | ||
return new DOMDocument("1.0"); | ||
} | ||
|
||
public function renderXML($doc) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this whole refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return $doc->saveXML(null, LIBXML_NOEMPTYTAG); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the caller have to do this transformation seems a little unnecessary. I can see where you'd want to specify values for quantity/prorate but I wonder if we can come up with something a little nicer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the pattern we use for the other libs:
https://github.com/recurly/recurly-client-ruby/blob/master/spec/recurly/invoice_spec.rb#L54
https://github.com/recurly/recurly-client-python/blob/master/tests/test_resources.py#L573
I agree it's not ideal. What did you have in mind? Perhaps we have a method on the
Recurly_Adjustment
class? We can't just use the adjustment itself because it needs theprorated
field and they may not want to refund the entire quantity.