Skip to content

Commit

Permalink
Merge branch 'release/1.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Oct 4, 2019
2 parents c087b53 + 203c2f4 commit d4ad7c8
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 82 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased][unreleased]
-

## [1.0.4] - 2019-10-04
- Improved some exception messages.

## [1.0.3] - 2019-09-10
- Added context to the 'notification' translatable strings.

Expand All @@ -22,7 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## 1.0.0 - 2019-03-28
- First release.

[unreleased]: https://github.com/wp-pay-gateways/adyen/compare/1.0.3...HEAD
[unreleased]: https://github.com/wp-pay-gateways/adyen/compare/1.0.4...HEAD
[1.0.4]: https://github.com/wp-pay-gateways/adyen/compare/1.0.3...1.0.4
[1.0.3]: https://github.com/wp-pay-gateways/adyen/compare/1.0.2...1.0.3
[1.0.2]: https://github.com/wp-pay-gateways/adyen/compare/1.0.1...1.0.2
[1.0.1]: https://github.com/wp-pay-gateways/adyen/compare/1.0.0...1.0.1
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -47,7 +47,7 @@
"phpmd/phpmd": "^2.6",
"phpunit/phpunit": "^5.7",
"roots/wordpress": "^5.2",
"squizlabs/php_codesniffer": "^3.4",
"squizlabs/php_codesniffer": "^3.5",
"wp-coding-standards/wpcs": "^2.1",
"wp-phpunit/wp-phpunit": "^5.2"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "adyen",
"version": "1.0.3",
"version": "1.0.4",
"description": "Adyen driver for the WordPress payment processing library.",
"repository": {
"type": "git",
Expand Down
27 changes: 16 additions & 11 deletions src/Amount.php
Expand Up @@ -10,11 +10,6 @@

namespace Pronamic\WordPress\Pay\Gateways\Adyen;

use InvalidArgumentException;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Exception\ValidationException;
use JsonSchema\Validator;

/**
* Amount
*
Expand All @@ -24,7 +19,7 @@
* @version 1.0.0
* @since 1.0.0
*/
class Amount {
class Amount implements \JsonSerializable {
/**
* Currency.
*
Expand All @@ -45,11 +40,11 @@ class Amount {
* @param string $currency Currency.
* @param int $value Value.
*
* @throws InvalidArgumentException Throws invalid argument exception when Adyen amount requirements are not met.
* @throws \InvalidArgumentException Throws invalid argument exception when Adyen amount requirements are not met.
*/
public function __construct( $currency, $value ) {
if ( 3 !== strlen( $currency ) ) {
throw new InvalidArgumentException(
throw new \InvalidArgumentException(
sprintf(
'Given currency `%s` not a three-character ISO currency code.',
$currency
Expand Down Expand Up @@ -91,22 +86,32 @@ public function get_json() {
);
}

/**
* JSON serialize.
*
* @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php
* @return object
*/
public function jsonSerialize() {
return $this->get_json();
}

/**
* Create amount from object.
*
* @param object $object Object.
* @return Amount
* @throws ValidationException Throws validation exception when object does not contains the required properties.
* @throws \JsonSchema\Exception\ValidationException Throws validation exception when object does not contains the required properties.
*/
public static function from_object( $object ) {
$validator = new Validator();
$validator = new \JsonSchema\Validator();

$validator->validate(
$object,
(object) array(
'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/amount.json' ),
),
Constraint::CHECK_MODE_EXCEPTIONS
\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
);

return new self(
Expand Down
2 changes: 0 additions & 2 deletions src/Channel.php
Expand Up @@ -10,8 +10,6 @@

namespace Pronamic\WordPress\Pay\Gateways\Adyen;

use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses;

/**
* Channel
*
Expand Down
57 changes: 34 additions & 23 deletions src/Client.php
Expand Up @@ -10,16 +10,13 @@

namespace Pronamic\WordPress\Pay\Gateways\Adyen;

use Exception;
use WP_Error;

/**
* Adyen client
*
* @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
*
* @author Remco Tolsma
* @version 1.0.0
* @version 1.0.4
* @since 1.0.0
*/
class Client {
Expand All @@ -45,50 +42,64 @@ public function __construct( Config $config ) {
* @param string $method Adyen API method.
* @param Request $request Request object.
* @return object
* @throws Exception Throws exception when error occurs.
* @throws \Exception Throws exception when error occurs.
*/
private function send_request( $method, $request ) {
// Request.
$url = $this->config->get_api_url( $method );

$response = wp_remote_request(
$response = \wp_remote_request(
$url,
array(
'method' => 'POST',
'headers' => array(
'X-API-key' => $this->config->get_api_key(),
'Content-Type' => 'application/json',
),
'body' => wp_json_encode( $request->get_json() ),
'body' => \wp_json_encode( $request->get_json() ),
)
);

if ( $response instanceof WP_Error ) {
throw new Exception( $response->get_error_message() );
if ( $response instanceof \WP_Error ) {
throw new \Exception( $response->get_error_message() );
}

// Body.
$body = wp_remote_retrieve_body( $response );
$body = \wp_remote_retrieve_body( $response );

// Response.
$response_code = \wp_remote_retrieve_response_code( $response );
$response_message = \wp_remote_retrieve_response_message( $response );

// Data.
$data = json_decode( $body );

// JSON error.
$json_error = json_last_error();

if ( JSON_ERROR_NONE !== $json_error ) {
throw new Exception(
sprintf( 'JSON: %s', json_last_error_msg() ),
if ( \JSON_ERROR_NONE !== $json_error ) {
throw new \Exception(
\sprintf(
'Could not JSON decode Adyen response, HTTP response: "%s %s", HTTP body length: "%d", JSON error: "%s".',
$response_code,
$response_message,
\strlen( $body ),
\json_last_error_msg()
),
$json_error
);
}

// Object.
if ( ! is_object( $data ) ) {
$code = wp_remote_retrieve_response_code( $response );

throw new Exception(
sprintf( 'Could not JSON decode Adyen response to an object (HTTP Status Code: %s).', $code ),
intval( $code )
if ( ! \is_object( $data ) ) {
throw new \Exception(
\sprintf(
'Could not JSON decode Adyen response to an object, HTTP response: "%s %s", HTTP body: "%s".',
$response_code,
$response_message,
$body
),
\intval( $response_code )
);
}

Expand Down Expand Up @@ -117,7 +128,7 @@ private function send_request( $method, $request ) {
*
* @return PaymentResponse
*
* @throws Exception Throws error if request fails.
* @throws \Exception Throws error if request fails.
*/
public function create_payment( PaymentRequest $request ) {
$data = $this->send_request( 'payments', $request );
Expand All @@ -132,7 +143,7 @@ public function create_payment( PaymentRequest $request ) {
*
* @return PaymentSessionResponse
*
* @throws Exception Throws error if request fails.
* @throws \Exception Throws error if request fails.
*/
public function create_payment_session( PaymentSessionRequest $request ) {
$data = $this->send_request( 'paymentSession', $request );
Expand All @@ -147,7 +158,7 @@ public function create_payment_session( PaymentSessionRequest $request ) {
*
* @return PaymentResultResponse
*
* @throws Exception Throws error if request fails.
* @throws \Exception Throws error if request fails.
*/
public function get_payment_result( PaymentResultRequest $request ) {
$data = $this->send_request( 'payments/result', $request );
Expand All @@ -160,7 +171,7 @@ public function get_payment_result( PaymentResultRequest $request ) {
*
* @return PaymentMethodsResponse
*
* @throws Exception Throws error if request fails.
* @throws \Exception Throws error if request fails.
*/
public function get_payment_methods() {
$request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
Expand Down
7 changes: 3 additions & 4 deletions src/Config.php
Expand Up @@ -10,15 +10,14 @@

namespace Pronamic\WordPress\Pay\Gateways\Adyen;

use Exception;
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
use Pronamic\WordPress\Pay\Core\GatewayConfig;

/**
* Config
*
* @author Remco Tolsma
* @version 1.0.0
* @version 1.0.4
* @since 1.0.0
*/
class Config extends GatewayConfig {
Expand Down Expand Up @@ -66,15 +65,15 @@ public function get_merchant_account() {
*
* @param string $method API method.
* @return string
* @throws Exception Throws exception when mode is live and API live URL prefix is empty.
* @throws \Exception Throws exception when mode is live and API live URL prefix is empty.
*/
public function get_api_url( $method ) {
if ( Core_Gateway::MODE_TEST === $this->mode ) {
return sprintf( Endpoint::API_URL_TEST, $method );
}

if ( empty( $this->api_live_url_prefix ) ) {
throw new Exception( 'API Live URL prefix is required for live configurations.' );
throw new \Exception( 'Adyen API Live URL prefix is required for live configurations.' );
}

return sprintf( Endpoint::API_URL_LIVE, $this->api_live_url_prefix, $method );
Expand Down
18 changes: 13 additions & 5 deletions src/LineItem.php
Expand Up @@ -10,16 +10,14 @@

namespace Pronamic\WordPress\Pay\Gateways\Adyen;

use InvalidArgumentException;

/**
* Line item.
*
* @author Reüel van der Steege
* @version 1.0.0
* @since 1.0.0
*/
class LineItem {
class LineItem implements \JsonSerializable {
/**
* Amount excluding tax.
*
Expand Down Expand Up @@ -83,7 +81,7 @@ class LineItem {
* @param int $quantity Quantity.
* @param int $amount_including_tax Amount (including tax).
*
* @throws InvalidArgumentException Throws invalid argument exception when arguments are invalid.
* @throws \InvalidArgumentException Throws invalid argument exception when arguments are invalid.
*/
public function __construct( $description, $quantity, $amount_including_tax ) {
$this->set_description( $description );
Expand Down Expand Up @@ -143,7 +141,7 @@ public function get_description() {
*
* @param string|null $description Description.
* @return void
* @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 100`.
* @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 100`.
*/
public function set_description( $description = null ) {
$this->description = $description;
Expand Down Expand Up @@ -267,4 +265,14 @@ public function get_json() {

return $object;
}

/**
* JSON serialize.
*
* @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php
* @return object
*/
public function jsonSerialize() {
return $this->get_json();
}
}
18 changes: 13 additions & 5 deletions src/LineItems.php
Expand Up @@ -10,16 +10,14 @@

namespace Pronamic\WordPress\Pay\Gateways\Adyen;

use InvalidArgumentException;

/**
* Line items.
*
* @author Reüel van der Steege
* @version 1.0.0
* @since 1.0.0
*/
class LineItems {
class LineItems implements \JsonSerializable {
/**
* Line items.
*
Expand Down Expand Up @@ -51,7 +49,7 @@ public function __construct( $items = null ) {
*
* @return LineItem
*
* @throws InvalidArgumentException Throws invalid argument exception when arguments are invalid.
* @throws \InvalidArgumentException Throws invalid argument exception when arguments are invalid.
*/
public function new_item( $description, $quantity, $amount_including_tax ) {
$item = new LineItem( $description, $quantity, $amount_including_tax );
Expand Down Expand Up @@ -83,7 +81,7 @@ public function get_line_items() {
/**
* Get JSON.
*
* @return array|null
* @return array<object>
*/
public function get_json() {
$data = array_map(
Expand All @@ -101,4 +99,14 @@ function( LineItem $item ) {

return $data;
}

/**
* JSON serialize.
*
* @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php
* @return array<object>
*/
public function jsonSerialize() {
return $this->get_json();
}
}
2 changes: 1 addition & 1 deletion src/NotificationsController.php
Expand Up @@ -11,7 +11,7 @@
namespace Pronamic\WordPress\Pay\Gateways\Adyen;

use JsonSchema\Exception\ValidationException;
use Pronamic\WordPress\Pay\Core\Statuses as PaymentStatus;
use Pronamic\WordPress\Pay\Payments\PaymentStatus as PaymentStatus;
use WP_Error;
use WP_REST_Request;

Expand Down

0 comments on commit d4ad7c8

Please sign in to comment.