Beispiel #1
0
 public function testListRefundsRoute()
 {
     $route = APIRoutes::getRoute(APIRoutes::REFUND_RESOURCE, null, array('PAYMENT_ID' => 'foo'));
     $expected = '/payments/foo/refunds';
     $endRoute = substr($route, -strlen($expected));
     $this->assertEquals($expected, $endRoute);
 }
Beispiel #2
0
 /**
  * Creates a Payment.
  *
  * @param   array               $data       API data for payment creation
  * @param   Payplug\Payplug    $payplug    the client configuration
  *
  * @return  null|Payment the created payment instance
  *
  * @throws  Payplug\Exception\ConfigurationNotSetException
  */
 public static function create(array $data, Payplug\Payplug $payplug = null)
 {
     if ($payplug === null) {
         $payplug = Payplug\Payplug::getDefaultConfiguration();
     }
     $httpClient = new Payplug\Core\HttpClient($payplug);
     $response = $httpClient->post(Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::PAYMENT_RESOURCE), $data);
     return Payment::fromAttributes($response['httpResponse']);
 }
Beispiel #3
0
 /**
  * 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;
 }
Beispiel #4
0
 /**
  * List the cards of a customer.
  *
  * @param   string|Customer $customer the customer id or the customer object
  * @param   int $perPage the number of results per page
  * @param   int $page the page number
  * @param   Payplug\Payplug $payplug the client configuration
  *
  * @return  Card[] an array containing the cards.
  *
  * @throws Payplug\Exception\ConfigurationNotSetException
  * @throws Payplug\Exception\UnexpectedAPIResponseException
  */
 public static function listCards($customer, $perPage = null, $page = null, Payplug\Payplug $payplug = null)
 {
     if ($payplug === null) {
         $payplug = Payplug\Payplug::getDefaultConfiguration();
     }
     if ($customer instanceof Customer) {
         $customer = $customer->id;
     }
     $httpClient = new Payplug\Core\HttpClient($payplug);
     $pagination = array('per_page' => $perPage, 'page' => $page);
     $response = $httpClient->get(Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::CARD_RESOURCE, null, array('CUSTOMER_ID' => $customer), $pagination));
     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']);
     }
     $cards = array();
     foreach ($response['httpResponse']['data'] as &$card) {
         $cards[] = Card::fromAttributes($card);
     }
     return $cards;
 }
Beispiel #5
0
 /**
  * List customers.
  *
  * @param   Payplug\Payplug $payplug the client configuration
  *
  * @param   int $perPage the number of results per page
  * @param   int $page the page number
  * @return  Customer[] the array of payments
  *
  * @throws  Payplug\Exception\InvalidPaymentException
  * @throws  Payplug\Exception\UnexpectedAPIResponseException
  */
 public static function listCustomers($perPage = null, $page = null, Payplug\Payplug $payplug = null)
 {
     if ($payplug === null) {
         $payplug = Payplug\Payplug::getDefaultConfiguration();
     }
     $httpClient = new Payplug\Core\HttpClient($payplug);
     $pagination = array('per_page' => $perPage, 'page' => $page);
     $response = $httpClient->get(Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::CUSTOMER_RESOURCE, null, array(), $pagination));
     if (!array_key_exists('data', $response['httpResponse']) || !is_array($response['httpResponse']['data'])) {
         throw new Payplug\Exception\UnexpectedAPIResponseException("Expected 'data' key in API response.", $response['httpResponse']);
     }
     $customers = array();
     foreach ($response['httpResponse']['data'] as &$customer) {
         $customers[] = Customer::fromAttributes($customer);
     }
     return $customers;
 }
Beispiel #6
0
 /**
  * Sends a test request to the remote API.
  *
  * @return  array   the response in a dictionary with following keys:
  * <pre>
  *  'httpStatus'    => The 2xx HTTP status code {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2}
  *  'httpResponse'  => The HTTP response
  * </pre>
  *
  * @throws  Payplug\Exception\UnexpectedAPIResponseException  When the API response is not parsable in JSON.
  * @throws  Payplug\Exception\HttpException                   When status code is not 2xx.
  * @throws  Payplug\Exception\ConnectionException             When an error was encountered while connecting to the resource.
  */
 public function testRemote()
 {
     return $this->request('GET', APIRoutes::getTestRoute(), null, false);
 }