Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/BitPaySDK/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,53 @@ public function cancelInvoice(
return $invoice;
}

/**
* Cancel a BitPay invoice by GUID.
*
* @param string $guid The guid of the invoice to cancel.
* @return Invoice $invoice Cancelled invoice object.
* @throws InvoiceCancellationException
* @throws BitPayException
*/
public function cancelInvoiceByGuid(
string $guid,
bool $forceCancel = false
): Invoice {
try {
$params = [];
$params["token"] = $this->_tokenCache->getTokenByFacade(Facade::Merchant);
if ($forceCancel) {
$params["forceCancel"] = $forceCancel;
}

$responseJson = $this->_RESTcli->delete("invoices/guid/" . $guid, $params);
} catch (BitPayException $e) {
throw new InvoiceCancellationException(
"failed to serialize Invoice object : " .
$e->getMessage(),
null,
null,
$e->getApiCode()
);
} catch (Exception $e) {
throw new InvoiceCancellationException("failed to serialize Invoice object : " . $e->getMessage());
}

try {
$mapper = new JsonMapper();
$invoice = $mapper->map(
json_decode($responseJson),
new Invoice()
);
} catch (Exception $e) {
throw new InvoiceCancellationException(
"failed to deserialize BitPay server response (Invoice) : " . $e->getMessage()
);
}

return $invoice;
}

/**
* Pay an invoice with a mock transaction
*
Expand Down
104 changes: 104 additions & 0 deletions test/unit/BitPaySDK/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,110 @@ public function testCancelInvoiceShouldCatchJsonMapperException($testedObject)
$testedObject->cancelInvoice($invoiceId, true);
}

/**
* @depends testWithFileJsonConfig
*/
public function testCancelInvoiceByGuid($testedObject)
{
$params['token'] = $this->getMerchantTokenFromFile();
$params['forceCancel'] = true;
$guid = 'chc9kj52-04g0-4b6f-941d-3a844e352758';

$restCliMock = $this->getRestCliMock();
$restCliMock
->expects($this->once())
->method('delete')
->with('invoices/guid/' . $guid, $params)
->willReturn(file_get_contents('json/getInvoice.json', true));
$setRestCli = function () use ($restCliMock) {
$this->_RESTcli = $restCliMock;
};
$doSetRestCli = $setRestCli->bindTo($testedObject, get_class($testedObject));
$doSetRestCli();

$result = $testedObject->cancelInvoiceByGuid($guid, true);
$this->assertInstanceOf(Invoice::class, $result);
$this->assertEquals('UZjwcYkWAKfTMn9J1yyfs4', $result->getId());
$this->assertEquals('USD', $result->getCurrency());
$this->assertEquals(12.0, $result->getPrice());
$this->assertEquals('new', $result->getStatus());
}

/**
* @depends testWithFileJsonConfig
*/
public function testCancelInvoiceByGuidShouldCatchRestCliBitPayException($testedObject)
{
$params['token'] = $this->getMerchantTokenFromFile();
$params['forceCancel'] = true;
$guid = 'chc9kj52-04g0-4b6f-941d-3a844e352758';

$restCliMock = $this->getRestCliMock();
$restCliMock
->expects($this->once())
->method('delete')
->with('invoices/guid/' . $guid, $params)
->willThrowException(new BitPayException());
$setRestCli = function () use ($restCliMock) {
$this->_RESTcli = $restCliMock;
};
$doSetRestCli = $setRestCli->bindTo($testedObject, get_class($testedObject));
$doSetRestCli();

$this->expectException(InvoiceCancellationException::class);
$testedObject->cancelInvoiceByGuid($guid, true);
}

/**
* @depends testWithFileJsonConfig
*/
public function testCancelInvoiceByGuidShouldCatchRestCliException($testedObject)
{
$params['token'] = $this->getMerchantTokenFromFile();
$params['forceCancel'] = true;
$guid = 'chc9kj52-04g0-4b6f-941d-3a844e352758';

$restCliMock = $this->getRestCliMock();
$restCliMock
->expects($this->once())
->method('delete')
->with('invoices/guid/' . $guid, $params)
->willThrowException(new Exception());
$setRestCli = function () use ($restCliMock) {
$this->_RESTcli = $restCliMock;
};
$doSetRestCli = $setRestCli->bindTo($testedObject, get_class($testedObject));
$doSetRestCli();

$this->expectException(InvoiceCancellationException::class);
$testedObject->cancelInvoiceByGuid($guid, true);
}

/**
* @depends testWithFileJsonConfig
*/
public function testCancelInvoiceByGuidShouldCatchJsonMapperException($testedObject)
{
$params['token'] = $this->getMerchantTokenFromFile();
$params['forceCancel'] = true;
$guid = 'chc9kj52-04g0-4b6f-941d-3a844e352758';

$restCliMock = $this->getRestCliMock();
$restCliMock
->expects($this->once())
->method('delete')
->with('invoices/guid/' . $guid, $params)
->willReturn(self::CORRUPT_JSON_STRING);
$setRestCli = function () use ($restCliMock) {
$this->_RESTcli = $restCliMock;
};
$doSetRestCli = $setRestCli->bindTo($testedObject, get_class($testedObject));
$doSetRestCli();

$this->expectException(InvoiceCancellationException::class);
$testedObject->cancelInvoiceByGuid($guid, true);
}

/**
* @depends testWithFileJsonConfig
*/
Expand Down
Loading