/** * List the refunds of this payment. * * @param Payplug\Payplug $payplug the client configuration * * @return null|Refund[] the array of refunds of this payment * * @throws Payplug\Exception\InvalidPaymentException * @throws Payplug\Exception\UnexpectedAPIResponseException */ public function listRefunds(Payplug\Payplug $payplug = null) { if (!array_key_exists('id', $this->getAttributes())) { throw new Payplug\Exception\InvalidPaymentException("This payment object has no id. You can't list refunds on it."); } return Refund::listRefunds($this->id, $payplug); }
/** * Retrieve a card object on a customer. * * @param string|Customer $customer the customer id or the customer object * @param string $cardId the card id * @param Payplug\Payplug $payplug the client configuration * * @return null|Payplug\Resource\APIResource|Card the card object * * @throws Payplug\Exception\ConfigurationNotSetException */ public static function retrieve($customer, $cardId, Payplug\Payplug $payplug = null) { if ($payplug === null) { $payplug = Payplug\Payplug::getDefaultConfiguration(); } if ($customer instanceof Customer) { $customer = $customer->id; } $httpClient = new Payplug\Core\HttpClient($payplug); $response = $httpClient->get(Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::CARD_RESOURCE, $cardId, array('CUSTOMER_ID' => $customer))); return Refund::fromAttributes($response['httpResponse']); }
/** * Tries to recompose an API Resource from its attributes. For example, when you got it from a notification. * The API Resource must have a known 'object' property. * * @param array $attributes The attributes of the object. * * @return IVerifiableAPIResource An unsafe API Resource. * * @throws Payplug\Exception\UnknownAPIResourceException When the given object is unknown. */ public static function factory(array $attributes) { if (!array_key_exists('object', $attributes)) { throw new Payplug\Exception\UnknownAPIResourceException('Missing "object" property.'); } switch ($attributes['object']) { case 'payment': return Payplug\Resource\Payment::fromAttributes($attributes); case 'refund': return Payplug\Resource\Refund::fromAttributes($attributes); } throw new Payplug\Exception\UnknownAPIResourceException('Unknown "object" property "' . $attributes['object'] . '".'); }
/** * Lists the last refunds of a payment. * * @param string|Payment $payment the payment id or the payment object * @param Payplug\Payplug $payplug the client configuration * * @return null|Refund[] an array containing the refunds on success. * * @throws Payplug\Exception\ConfigurationNotSetException * @throws Payplug\Exception\UnexpectedAPIResponseException */ public static function listRefunds($payment, Payplug\Payplug $payplug = null) { if ($payplug === null) { $payplug = Payplug\Payplug::getDefaultConfiguration(); } if ($payment instanceof Payment) { $payment = $payment->id; } $httpClient = new Payplug\Core\HttpClient($payplug); $response = $httpClient->get(Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::REFUND_RESOURCE, null, array('PAYMENT_ID' => $payment))); if (!array_key_exists('data', $response['httpResponse']) || !is_array($response['httpResponse']['data'])) { throw new Payplug\Exception\UnexpectedAPIResponseException("Expected API response to contain 'data' key referencing an array.", $response['httpResponse']); } $refunds = array(); foreach ($response['httpResponse']['data'] as &$refund) { $refunds[] = Refund::fromAttributes($refund); } return $refunds; }
/** * Returns an API resource that you can trust. * * @param Payplug\Payplug $payplug the client configuration. * * @return Payplug\Resource\APIResource The consistent API resource. * * @throws Payplug\Exception\UndefinedAttributeException when the local resource is invalid. */ function getConsistentResource(Payplug\Payplug $payplug = null) { if (!array_key_exists('id', $this->_attributes)) { throw new Payplug\Exception\UndefinedAttributeException('The id of the refund is not set.'); } else { if (!array_key_exists('payment_id', $this->_attributes)) { throw new Payplug\Exception\UndefinedAttributeException('The payment_id of the refund is not set.'); } } return Payplug\Resource\Refund::retrieve($this->_attributes['payment_id'], $this->_attributes['id'], $payplug); }
public function testRetrieveConsistentRefund() { function testRetrieveConsistentRefund_getinfo($option) { switch ($option) { case CURLINFO_HTTP_CODE: return 200; } return null; } $this->_requestMock->expects($this->once())->method('exec')->will($this->returnValue('{"id": "re_345", "payment_id": "pay_789"}')); $this->_requestMock->expects($this->any())->method('setopt')->will($this->returnValue(true)); $this->_requestMock->expects($this->any())->method('getinfo')->will($this->returnCallback(function ($option) { switch ($option) { case CURLINFO_HTTP_CODE: return 200; } return null; })); $refund1 = Refund::fromAttributes(array('id' => 're_123', 'payment_id' => 'pay_321')); $refund2 = $refund1->getConsistentResource($this->_configuration); $this->assertEquals('re_123', $refund1->id); $this->assertEquals('pay_321', $refund1->payment_id); $this->assertEquals('re_345', $refund2->id); $this->assertEquals('pay_789', $refund2->payment_id); }