Skip to content

Commit

Permalink
Merge pull request #274 from mwarzybok-sumoheavy/feature/SP-581
Browse files Browse the repository at this point in the history
SP-581 PHP - Implement better refund exceptions
  • Loading branch information
bobbrodie authored Nov 10, 2023
2 parents d1d9888 + bb9fb84 commit 30ca2b9
Show file tree
Hide file tree
Showing 130 changed files with 1,446 additions and 6,167 deletions.
245 changes: 129 additions & 116 deletions src/BitPaySDK/Client.php

Large diffs are not rendered by default.

165 changes: 49 additions & 116 deletions src/BitPaySDK/Client/BillClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

namespace BitPaySDK\Client;

use BitPaySDK\Exceptions\BillCreationException;
use BitPaySDK\Exceptions\BillDeliveryException;
use BitPaySDK\Exceptions\BillQueryException;
use BitPaySDK\Exceptions\BillUpdateException;
use BitPaySDK\Exceptions\BitPayException;
use BitPaySDK\Exceptions\BitPayApiException;
use BitPaySDK\Exceptions\BitPayExceptionProvider;
use BitPaySDK\Exceptions\BitPayGenericException;
use BitPaySDK\Model\Bill\Bill;
use BitPaySDK\Model\Facade;
use BitPaySDK\Tokens;
Expand Down Expand Up @@ -58,36 +56,23 @@ public static function getInstance(Tokens $tokenCache, RESTcli $restCli): self
/**
* Create a BitPay Bill.
*
* @param Bill $bill A Bill object with request parameters defined.
* @param string $facade The facade used to create it.
* @param bool $signRequest Signed request.
* @param Bill $bill A Bill object with request parameters defined.
* @param string $facade The facade used to create it.
* @param bool $signRequest Signed request.
* @return Bill
* @throws BitPayException
* @throws BitPayApiException
* @throws BitPayGenericException
*/
public function create(Bill $bill, string $facade = Facade::MERCHANT, bool $signRequest = true): Bill
{
try {
$bill->setToken($this->tokenCache->getTokenByFacade($facade));
$bill->setToken($this->tokenCache->getTokenByFacade($facade));

$responseJson = $this->restCli->post("bills", $bill->toArray(), $signRequest);
} catch (BitPayException $e) {
throw new BillCreationException(
"failed to serialize Bill object : " .
$e->getMessage(),
null,
null,
$e->getApiCode()
);
} catch (Exception $e) {
throw new BillCreationException("failed to serialize Bill object : " . $e->getMessage());
}
$responseJson = $this->restCli->post("bills", $bill->toArray(), $signRequest);

try {
return $this->mapJsonToBillClass($responseJson);
} catch (Exception $e) {
throw new BillCreationException(
"failed to deserialize BitPay server response (Bill) : " . $e->getMessage()
);
BitPayExceptionProvider::throwDeserializeResourceException('Bill', $e->getMessage());
}
}

Expand All @@ -98,107 +83,68 @@ public function create(Bill $bill, string $facade = Facade::MERCHANT, bool $sign
* @param $facade string The facade used to retrieve it.
* @param $signRequest bool Signed request.
* @return Bill
* @throws BitPayException
* @throws BitPayApiException
* @throws BitPayGenericException
*/
public function get(string $billId, string $facade = Facade::MERCHANT, bool $signRequest = true): Bill
{
try {
$params = [];
$params["token"] = $this->tokenCache->getTokenByFacade($facade);
$params = [];
$params["token"] = $this->tokenCache->getTokenByFacade($facade);

$responseJson = $this->restCli->get("bills/" . $billId, $params, $signRequest);
} catch (BitPayException $e) {
throw new BillQueryException(
"failed to serialize Bill object : " .
$e->getMessage(),
null,
null,
$e->getApiCode()
);
} catch (Exception $e) {
throw new BillQueryException("failed to serialize Bill object : " . $e->getMessage());
}
$responseJson = $this->restCli->get("bills/" . $billId, $params, $signRequest);

try {
return $this->mapJsonToBillClass($responseJson);
} catch (Exception $e) {
throw new BillQueryException(
"failed to deserialize BitPay server response (Bill) : " . $e->getMessage()
);
BitPayExceptionProvider::throwDeserializeResourceException('Bill', $e->getMessage());
}
}

/**
* Retrieve a collection of BitPay bills.
*
* @param string|null The status to filter the bills.
* @param string|null $status The status to filter the bills.
* @return Bill[]
* @throws BitPayException
* @throws BitPayApiException
* @throws BitPayGenericException
*/
public function getBills(string $status = null): array
{
try {
$params = [];
$params["token"] = $this->tokenCache->getTokenByFacade(Facade::MERCHANT);
if ($status) {
$params["status"] = $status;
}

$responseJson = $this->restCli->get("bills", $params);
} catch (BitPayException $e) {
throw new BillQueryException(
"failed to serialize Bill object : " .
$e->getMessage(),
null,
null,
$e->getApiCode()
);
} catch (Exception $e) {
throw new BillQueryException("failed to serialize Bill object : " . $e->getMessage());
$params = [];
$params["token"] = $this->tokenCache->getTokenByFacade(Facade::MERCHANT);
if ($status) {
$params["status"] = $status;
}

$responseJson = $this->restCli->get("bills", $params);

try {
$mapper = JsonMapperFactory::create();
$bills = $mapper->mapArray(
return $mapper->mapArray(
json_decode($responseJson, true, 512, JSON_THROW_ON_ERROR),
[],
Bill::class
);
} catch (Exception $e) {
throw new BillQueryException(
"failed to deserialize BitPay server response (Bill) : " . $e->getMessage()
);
BitPayExceptionProvider::throwDeserializeResourceException('Bill', $e->getMessage());
}

return $bills;
}

/**
* Update a BitPay Bill.
*
* @param Bill $bill A Bill object with the parameters to update defined.
* @param string $billId The Id of the Bill to update.
* @param Bill $bill A Bill object with the parameters to update defined.
* @param string $billId The Id of the Bill to update.
* @return Bill
* @throws BitPayException
* @throws BitPayApiException
* @throws BitPayGenericException
*/
public function update(Bill $bill, string $billId): Bill
{
try {
$billToken = $this->get($bill->getId())->getToken();
$bill->setToken($billToken);
$billToken = $this->get($bill->getId())->getToken();
$bill->setToken($billToken);

$responseJson = $this->restCli->update("bills/" . $billId, $bill->toArray());
} catch (BitPayException $e) {
throw new BillUpdateException(
"failed to serialize Bill object : " .
$e->getMessage(),
null,
null,
$e->getApiCode()
);
} catch (Exception $e) {
throw new BillUpdateException("failed to serialize Bill object : " . $e->getMessage());
}
$responseJson = $this->restCli->update("bills/" . $billId, $bill->toArray());

try {
$mapper = JsonMapperFactory::create();
Expand All @@ -208,57 +154,44 @@ public function update(Bill $bill, string $billId): Bill
$bill
);
} catch (Exception $e) {
throw new BillUpdateException("failed to deserialize BitPay server response (Bill) : " . $e->getMessage());
BitPayExceptionProvider::throwDeserializeResourceException('Bill', $e->getMessage());
}
}

/**
* Deliver a BitPay Bill.
*
* @param string $billId The id of the requested bill.
* @param string $billToken The token of the requested bill.
* @param bool $signRequest Allow unsigned request
* @param string $billId The id of the requested bill.
* @param string $billToken The token of the requested bill.
* @param bool $signRequest Allow unsigned request
* @return bool
* @throws BitPayException
* @throws BitPayApiException
* @throws BitPayGenericException
*/
public function deliver(string $billId, string $billToken, bool $signRequest = true): bool
{
try {
$responseJson = $this->restCli->post(
"bills/" . $billId . "/deliveries",
['token' => $billToken],
$signRequest
);
} catch (BitPayException $e) {
throw new BillDeliveryException(
"failed to serialize Bill object : " .
$e->getMessage(),
null,
null,
$e->getApiCode()
);
} catch (Exception $e) {
throw new BillDeliveryException("failed to serialize Bill object : " . $e->getMessage());
}
$responseJson = $this->restCli->post(
"bills/" . $billId . "/deliveries",
['token' => $billToken],
$signRequest
);

try {
$result = str_replace("\"", "", $responseJson);

return strtolower($result) === 'success';
} catch (Exception $e) {
throw new BillDeliveryException(
"failed to deserialize BitPay server response (Bill) : " . $e->getMessage()
);
BitPayExceptionProvider::throwDeserializeResourceException('Bill', $e->getMessage());
}
}

/**
* @param string|null $responseJson
* @return mixed
* @return Bill
* @throws \JsonException
* @throws \JsonMapper_Exception
*/
private function mapJsonToBillClass(?string $responseJson): mixed
private function mapJsonToBillClass(?string $responseJson): Bill
{
$mapper = JsonMapperFactory::create();

Expand Down
Loading

0 comments on commit 30ca2b9

Please sign in to comment.