| 1 |
<?php |
| 2 |
namespace Payplug\Resource; |
| 3 |
use Payplug; |
| 4 |
|
| 5 |
/** |
| 6 |
* A Payment refund. |
| 7 |
*/ |
| 8 |
class Refund extends APIResource implements IVerifiableAPIResource |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The factory method that constructs the API resource. |
| 12 |
* |
| 13 |
* @param array $attributes the default attributes. |
| 14 |
* |
| 15 |
* @return Refund The new resource. |
| 16 |
*/ |
| 17 |
public static function fromAttributes(array $attributes) |
| 18 |
{ |
| 19 |
$object = new Refund(); |
| 20 |
$object->initialize($attributes); |
| 21 |
return $object; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Creates a refund on a payment. |
| 26 |
* |
| 27 |
* @param string|Payment $payment the payment id or the payment object |
| 28 |
* @param array $data API data for refund |
| 29 |
* @param Payplug\Payplug $payplug the client configuration |
| 30 |
* |
| 31 |
* @return null|Refund the refund object |
| 32 |
* @throws Payplug\Exception\ConfigurationNotSetException |
| 33 |
*/ |
| 34 |
public static function create($payment, array $data = null, Payplug\Payplug $payplug = null) |
| 35 |
{ |
| 36 |
if ($payplug === null) { |
| 37 |
$payplug = Payplug\Payplug::getDefaultConfiguration(); |
| 38 |
} |
| 39 |
if ($payment instanceof Payment) { |
| 40 |
$payment = $payment->id; |
| 41 |
} |
| 42 |
|
| 43 |
$httpClient = new Payplug\Core\HttpClient($payplug); |
| 44 |
$response = $httpClient->post( |
| 45 |
Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::REFUND_RESOURCE, null, array('PAYMENT_ID' => $payment)), |
| 46 |
$data |
| 47 |
); |
| 48 |
|
| 49 |
return Refund::fromAttributes($response['httpResponse']); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Retrieves a refund object on a payment. |
| 54 |
* |
| 55 |
* @param string|Payment $payment the payment id or the payment object |
| 56 |
* @param string $refundId the refund id |
| 57 |
* @param Payplug\Payplug $payplug the client configuration |
| 58 |
* |
| 59 |
* @return null|Payplug\Resource\APIResource|Refund the refund object |
| 60 |
* |
| 61 |
* @throws Payplug\Exception\ConfigurationNotSetException |
| 62 |
*/ |
| 63 |
public static function retrieve($payment, $refundId, Payplug\Payplug $payplug = null) |
| 64 |
{ |
| 65 |
if ($payplug === null) { |
| 66 |
$payplug = Payplug\Payplug::getDefaultConfiguration(); |
| 67 |
} |
| 68 |
if ($payment instanceof Payment) { |
| 69 |
$payment = $payment->id; |
| 70 |
} |
| 71 |
|
| 72 |
$httpClient = new Payplug\Core\HttpClient($payplug); |
| 73 |
$response = $httpClient->get( |
| 74 |
Payplug\Core\APIRoutes::getRoute( |
| 75 |
Payplug\Core\APIRoutes::REFUND_RESOURCE, $refundId, array('PAYMENT_ID' => $payment) |
| 76 |
) |
| 77 |
); |
| 78 |
|
| 79 |
return Refund::fromAttributes($response['httpResponse']); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Lists the last refunds of a payment. |
| 84 |
* |
| 85 |
* @param string|Payment $payment the payment id or the payment object |
| 86 |
* @param Payplug\Payplug $payplug the client configuration |
| 87 |
* |
| 88 |
* @return null|Refund[] an array containing the refunds on success. |
| 89 |
* |
| 90 |
* @throws Payplug\Exception\ConfigurationNotSetException |
| 91 |
* @throws Payplug\Exception\UnexpectedAPIResponseException |
| 92 |
*/ |
| 93 |
public static function listRefunds($payment, Payplug\Payplug $payplug = null) |
| 94 |
{ |
| 95 |
if ($payplug === null) { |
| 96 |
$payplug = Payplug\Payplug::getDefaultConfiguration(); |
| 97 |
} |
| 98 |
if ($payment instanceof Payment) { |
| 99 |
$payment = $payment->id; |
| 100 |
} |
| 101 |
|
| 102 |
$httpClient = new Payplug\Core\HttpClient($payplug); |
| 103 |
|
| 104 |
$response = $httpClient->get( |
| 105 |
Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::REFUND_RESOURCE, null, array('PAYMENT_ID' => $payment)) |
| 106 |
); |
| 107 |
|
| 108 |
if (!array_key_exists('data', $response['httpResponse']) || !is_array($response['httpResponse']['data'])) { |
| 109 |
throw new Payplug\Exception\UnexpectedAPIResponseException( |
| 110 |
"Expected API response to contain 'data' key referencing an array.", |
| 111 |
$response['httpResponse'] |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
$refunds = array(); |
| 116 |
foreach ($response['httpResponse']['data'] as &$refund) { |
| 117 |
$refunds[] = Refund::fromAttributes($refund); |
| 118 |
} |
| 119 |
|
| 120 |
return $refunds; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns an API resource that you can trust. |
| 125 |
* |
| 126 |
* @param Payplug\Payplug $payplug the client configuration. |
| 127 |
* |
| 128 |
* @return Payplug\Resource\APIResource The consistent API resource. |
| 129 |
* |
| 130 |
* @throws Payplug\Exception\UndefinedAttributeException when the local resource is invalid. |
| 131 |
*/ |
| 132 |
function getConsistentResource(Payplug\Payplug $payplug = null) |
| 133 |
{ |
| 134 |
if (!array_key_exists('id', $this->_attributes)) { |
| 135 |
throw new Payplug\Exception\UndefinedAttributeException('The id of the refund is not set.'); |
| 136 |
} |
| 137 |
else if (!array_key_exists('payment_id', $this->_attributes)) { |
| 138 |
throw new Payplug\Exception\UndefinedAttributeException('The payment_id of the refund is not set.'); |
| 139 |
} |
| 140 |
|
| 141 |
return Payplug\Resource\Refund::retrieve($this->_attributes['payment_id'], $this->_attributes['id'], $payplug); |
| 142 |
} |
| 143 |
} |
| 144 |
|