Skip to content

Commit

Permalink
Merge pull request #386 from aarontkennedy/master
Browse files Browse the repository at this point in the history
Add Capture and Cancel enpoints to go with the Authorize Endpoint
  • Loading branch information
Aaron Suarez authored Dec 18, 2018
2 parents 5a14889 + 7d13d4e commit dee8217
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Tests/Recurly/Purchase_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ function defaultResponses() {
array('POST', '/purchases/preview', 'purchases/preview-200.xml'),
array('POST', '/purchases/authorize', 'purchases/authorize-200.xml'),
array('POST', '/purchases/pending', 'purchases/pending-200.xml'),
/* Transaction IDs here come from the /fixtures/purchases/authorize-200.xml */
array('POST', '/purchases/transaction-uuid-a1b2c3d4e5f6g7h8i9j/cancel', 'purchases/cancel-200.xml'),
array('POST', '/purchases/transaction-uuid-a1b2c3d4e5f6g7h8i9j/capture', 'purchases/capture-200.xml'),
);
}

Expand Down Expand Up @@ -93,6 +96,34 @@ public function testAuthorizePurchase() {
$this->assertNull($collection->charge_invoice->uuid);
}

public function testAuthorizeAndCapturePurchase() {
$purchase = $this->mockPurchase();
$purchase->account->email = 'benjamin.dumonde@example.com';
$purchase->account->billing_info->external_hpp_type = 'adyen';
$authorizeCollection = Recurly_Purchase::authorize($purchase, $this->client);

$transactionUUID = $authorizeCollection->charge_invoice->transactions->current()->uuid;

$captureCollection = Recurly_Purchase::capture($transactionUUID, $this->client);

$this->assertInstanceOf('Recurly_InvoiceCollection', $captureCollection);
$this->assertInstanceOf('Recurly_Invoice', $captureCollection->charge_invoice);
}

public function testAuthorizeAndCancelPurchase() {
$purchase = $this->mockPurchase();
$purchase->account->email = 'benjamin.dumonde@example.com';
$purchase->account->billing_info->external_hpp_type = 'adyen';
$authorizeCollection = Recurly_Purchase::authorize($purchase, $this->client);

$transactionUUID = $authorizeCollection->charge_invoice->transactions->current()->uuid;

$cancelCollection = Recurly_Purchase::cancel($transactionUUID, $this->client);

$this->assertInstanceOf('Recurly_InvoiceCollection', $cancelCollection);
$this->assertInstanceOf('Recurly_Invoice', $cancelCollection->charge_invoice);
}

public function testPendingPurchase() {
$purchase = $this->mockPurchase();
$purchase->account->email = 'benjamin.dumonde@example.com';
Expand Down
3 changes: 3 additions & 0 deletions Tests/fixtures/purchases/authorize-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Location: https://api.recurly.com/v2/invoices
</adjustment>
</line_items>
<transactions type="array">
<transaction href="https://api.recurly.com/v2/transactions/a1b2c3d4e5f6g7h8i9j" type="credit_card">
<uuid>a1b2c3d4e5f6g7h8i9j</uuid>
</transaction>
</transactions>
</charge_invoice>
</invoice_collection>
70 changes: 70 additions & 0 deletions Tests/fixtures/purchases/cancel-200.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Location: https://api.recurly.com/v2/invoices

<?xml version="1.0" encoding="UTF-8"?>
<invoice_collection>
<charge_invoice href="">
<account href="https://api.recurly.com/v2/accounts/c442b36c-c64f-41d7-b8e1-9c04e7a6ff82"/>
<address>
<address1>400 Alabama St</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>94110</zip>
<country>US</country>
<phone nil="nil"></phone>
</address>
<uuid nil="nil"></uuid>
<state>closed</state>
<invoice_number_prefix></invoice_number_prefix>
<invoice_number nil="nil"></invoice_number>
<po_number nil="nil"></po_number>
<vat_number nil="nil"></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">1000</total_in_cents>
<subtotal_after_discount_in_cents type="integer">1000</subtotal_after_discount_in_cents>
<currency>USD</currency>
<created_at nil="nil"></created_at>
<updated_at nil="nil"></updated_at>
<attempt_next_collection_at type="datetime">2017-05-17T16:36:56Z</attempt_next_collection_at>
<closed_at nil="nil"></closed_at>
<terms_and_conditions nil="nil"></terms_and_conditions>
<customer_notes nil="nil"></customer_notes>
<recovery_reason nil="nil"></recovery_reason>
<net_terms type="integer">0</net_terms>
<collection_method>automatic</collection_method>
<line_items type="array">
<adjustment href="" type="charge">
<account href="https://api.recurly.com/v2/accounts/c442b36c-c64f-41d7-b8e1-9c04e7a6ff82"/>
<uuid>3d86103d63f614d7aeee8341c98b2655</uuid>
<state>pending</state>
<description nil="nil"></description>
<accounting_code nil="nil"></accounting_code>
<product_code>4549449c-5870-4845-b672-1d07f15e87dd</product_code>
<origin>debit</origin>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">1000</total_in_cents>
<currency>USD</currency>
<taxable type="boolean">false</taxable>
<tax_exempt type="boolean">false</tax_exempt>
<tax_code nil="nil"></tax_code>
<start_date type="datetime">2017-05-17T16:36:56Z</start_date>
<end_date nil="nil"></end_date>
<created_at nil="nil"></created_at>
<updated_at nil="nil"></updated_at>
<revenue_schedule_type>at_invoice</revenue_schedule_type>
</adjustment>
</line_items>
<transactions type="array">
<transaction href="https://api.recurly.com/v2/transactions/a1b2c3d4e5f6g7h8i9j" type="credit_card">
<uuid>a1b2c3d4e5f6g7h8i9j</uuid>
</transaction>
</transactions>
<a name="refund" href="https://api.recurly.com/v2/invoices/1234/refund" method="post"/>
</charge_invoice>
</invoice_collection>
70 changes: 70 additions & 0 deletions Tests/fixtures/purchases/capture-200.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Location: https://api.recurly.com/v2/invoices

<?xml version="1.0" encoding="UTF-8"?>
<invoice_collection>
<charge_invoice href="">
<account href="https://api.recurly.com/v2/accounts/c442b36c-c64f-41d7-b8e1-9c04e7a6ff82"/>
<address>
<address1>400 Alabama St</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>94110</zip>
<country>US</country>
<phone nil="nil"></phone>
</address>
<uuid nil="nil"></uuid>
<state>paid</state>
<invoice_number_prefix></invoice_number_prefix>
<invoice_number nil="nil"></invoice_number>
<po_number nil="nil"></po_number>
<vat_number nil="nil"></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">1000</total_in_cents>
<subtotal_after_discount_in_cents type="integer">1000</subtotal_after_discount_in_cents>
<currency>USD</currency>
<created_at nil="nil"></created_at>
<updated_at nil="nil"></updated_at>
<attempt_next_collection_at type="datetime">2017-05-17T16:36:56Z</attempt_next_collection_at>
<closed_at nil="nil"></closed_at>
<terms_and_conditions nil="nil"></terms_and_conditions>
<customer_notes nil="nil"></customer_notes>
<recovery_reason nil="nil"></recovery_reason>
<net_terms type="integer">0</net_terms>
<collection_method>automatic</collection_method>
<line_items type="array">
<adjustment href="" type="charge">
<account href="https://api.recurly.com/v2/accounts/c442b36c-c64f-41d7-b8e1-9c04e7a6ff82"/>
<uuid>3d86103d63f614d7aeee8341c98b2655</uuid>
<state>pending</state>
<description nil="nil"></description>
<accounting_code nil="nil"></accounting_code>
<product_code>4549449c-5870-4845-b672-1d07f15e87dd</product_code>
<origin>debit</origin>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">1000</total_in_cents>
<currency>USD</currency>
<taxable type="boolean">false</taxable>
<tax_exempt type="boolean">false</tax_exempt>
<tax_code nil="nil"></tax_code>
<start_date type="datetime">2017-05-17T16:36:56Z</start_date>
<end_date nil="nil"></end_date>
<created_at nil="nil"></created_at>
<updated_at nil="nil"></updated_at>
<revenue_schedule_type>at_invoice</revenue_schedule_type>
</adjustment>
</line_items>
<transactions type="array">
<transaction href="https://api.recurly.com/v2/transactions/a1b2c3d4e5f6g7h8i9j" type="credit_card">
<uuid>a1b2c3d4e5f6g7h8i9j</uuid>
</transaction>
</transactions>
<a name="refund" href="https://api.recurly.com/v2/invoices/1234/refund" method="post"/>
</charge_invoice>
</invoice_collection>
30 changes: 30 additions & 0 deletions lib/recurly/purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,41 @@ public static function preview($purchase, $client = null) {
* @param Recurly_Client $client Optional client for the request, useful for mocking the client
* @return object Recurly_InvoiceCollection
* @throws Recurly_Error
*
* Note: To use this endpoint, you may have to contact Recurly support to have it enabled on your subdomain.
*/
public static function authorize($purchase, $client = null) {
return Recurly_Base::_post('/purchases/authorize', $purchase->xml(), $client);
}

/**
* Capture an open Authorization request
*
* @param $transactionUUID string To get this uuid, do something like: $invoiceCollection->charge_invoice->transactions->current()->uuid;.
* @param Recurly_Client $client Optional client for the request, useful for mocking the client
* @return object Recurly_InvoiceCollection
* @throws Recurly_Error
*
* Note: To use this endpoint, you may have to contact Recurly support to have it enabled on your subdomain.
*/
public static function capture($transactionUUID, $client = null) {
return Recurly_Base::_post('/purchases/transaction-uuid-' . rawurlencode($transactionUUID) . '/capture', null, $client);
}

/**
* Cancel an open Authorization request
*
* @param $transactionUUID string To get this uuid, do something like: $invoiceCollection->charge_invoice->transactions->current()->uuid;.
* @param Recurly_Client $client Optional client for the request, useful for mocking the client
* @return object Recurly_InvoiceCollection
* @throws Recurly_Error
*
* Note: To use this endpoint, you may have to contact Recurly support to have it enabled on your subdomain.
*/
public static function cancel($transactionUUID, $client = null) {
return Recurly_Base::_post('/purchases/transaction-uuid-' . rawurlencode($transactionUUID) . '/cancel', null, $client);
}

/**
* Use for Adyen HPP transaction requests. This runs
* the validations but not the transactions.
Expand Down

0 comments on commit dee8217

Please sign in to comment.