protected function setUp() { $this->_configuration = new Payplug\Payplug('abc'); Payplug\Payplug::setDefaultConfiguration($this->_configuration); $this->_requestMock = $this->getMock('\\Payplug\\Core\\IHttpRequest'); Payplug\Core\HttpClient::$REQUEST_HANDLER = $this->_requestMock; }
/** * 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']); }
/** * 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; }
function testGetUserAgentWithAdditionalProduct() { \Payplug\Core\HttpClient::addDefaultUserAgentProduct('PayPlug-Test', '1.0.0', 'Comment/1.6.3'); \Payplug\Core\HttpClient::addDefaultUserAgentProduct('Another-Test', '5.8.13'); $userAgent = $this->_httpClient->getUserAgent(); $this->assertStringEndsWith(' PayPlug-Test/1.0.0 (Comment/1.6.3) Another-Test/5.8.13', $userAgent); }
/** * 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; }
/** * 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; }
* @throws Payplug\Exception\HttpException the generated exception from the request */ private function throwRequestException($httpResponse, $httpStatus) { $exception = null; // Error 5XX if (substr($httpStatus, 0, 1) === '5') { throw new Payplug\Exception\PayplugServerException('Unexpected server error during the request.', $httpResponse, $httpStatus); } switch ($httpStatus) { case 400: throw new Payplug\Exception\BadRequestException('Bad request.', $httpResponse, $httpStatus); break; case 401: throw new Payplug\Exception\UnauthorizedException('Unauthorized. Please check your credentials.', $httpResponse, $httpStatus); break; case 403: throw new Payplug\Exception\ForbiddenException('Forbidden error. You are not allowed to access this resource.', $httpResponse, $httpStatus); break; case 404: throw new Payplug\Exception\NotFoundException('The resource you requested could not be found.', $httpResponse, $httpStatus); break; case 405: throw new Payplug\Exception\NotAllowedException('The requested method is not supported by this resource.', $httpResponse, $httpStatus); break; } throw new Payplug\Exception\HttpException('Unhandled HTTP error.', $httpResponse, $httpStatus); } } HttpClient::$CACERT_PATH = realpath(__DIR__ . str_replace('/', DIRECTORY_SEPARATOR, '/../../certs/cacert.pem'));