Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
feat(Payment): support get/update/delete payment by key
Browse files Browse the repository at this point in the history
Closes #349
  • Loading branch information
Jens Schulze committed Oct 4, 2017
1 parent e68aaf0 commit da32ce6
Showing 5 changed files with 175 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Core/Request/Payments/PaymentByKeyGetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\Payments;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Model\Type\Type;
use Commercetools\Core\Request\AbstractByKeyGetRequest;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\Payment\Payment;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\Payments
* @link https://dev.commercetools.com/http-api-projects-payments.html#get-payment-by-key
* @method Payment mapResponse(ApiResponseInterface $response)
* @method Payment mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class PaymentByKeyGetRequest extends AbstractByKeyGetRequest
{
protected $resultClass = Payment::class;

/**
* @param string $key
* @param Context $context
*/
public function __construct($key, Context $context = null)
{
parent::__construct(PaymentsEndpoint::endpoint(), $key, $context);
}

/**
* @param string $key
* @param Context $context
* @return static
*/
public static function ofKey($key, Context $context = null)
{
return new static($key, $context);
}
}
45 changes: 45 additions & 0 deletions src/Core/Request/Payments/PaymentDeleteByKeyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\Payments;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
use Commercetools\Core\Model\Type\Type;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\Payment\Payment;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\Payments
* @link https://dev.commercetools.com/http-api-projects-payments.html#delete-payment-by-key
* @method Payment mapResponse(ApiResponseInterface $response)
* @method Payment mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class PaymentDeleteByKeyRequest extends AbstractDeleteByKeyRequest
{
protected $resultClass = Payment::class;

/**
* @param string $id
* @param int $version
* @param Context $context
*/
public function __construct($id, $version, Context $context = null)
{
parent::__construct(PaymentsEndpoint::endpoint(), $id, $version, $context);
}

/**
* @param string $key
* @param int $version
* @param Context $context
* @return static
*/
public static function ofKeyAndVersion($key, $version, Context $context = null)
{
return new static($key, $version, $context);
}
}
46 changes: 46 additions & 0 deletions src/Core/Request/Payments/PaymentUpdateByKeyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\Payments;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractUpdateByKeyRequest;
use Commercetools\Core\Request\AbstractUpdateRequest;
use Commercetools\Core\Model\Payment\Payment;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\Payments
* @link https://dev.commercetools.com/http-api-projects-payments.html#update-payment-by-key
* @method Payment mapResponse(ApiResponseInterface $response)
* @method Payment mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class PaymentUpdateByKeyRequest extends AbstractUpdateByKeyRequest
{
protected $resultClass = Payment::class;

/**
* @param string $key
* @param string $version
* @param array $actions
* @param Context $context
*/
public function __construct($key, $version, array $actions = [], Context $context = null)
{
parent::__construct(PaymentsEndpoint::endpoint(), $key, $version, $actions, $context);
}

/**
* @param string $key
* @param int $version
* @param Context $context
* @return static
*/
public static function ofKeyAndVersion($key, $version, Context $context = null)
{
return new static($key, $version, [], $context);
}
}
18 changes: 18 additions & 0 deletions tests/integration/Payment/PaymentQueryRequestTest.php
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
use Commercetools\Core\Model\Payment\PaymentDraft;
use Commercetools\Core\Model\Payment\PaymentMethodInfo;
use Commercetools\Core\Request\Payments\PaymentByIdGetRequest;
use Commercetools\Core\Request\Payments\PaymentByKeyGetRequest;
use Commercetools\Core\Request\Payments\PaymentCreateRequest;
use Commercetools\Core\Request\Payments\PaymentDeleteRequest;
use Commercetools\Core\Request\Payments\PaymentQueryRequest;
@@ -63,6 +64,23 @@ public function testQuery()
$this->assertSame($payment->getId(), $result->getAt(0)->getId());
}

public function testGetByKey()
{
$key = $this->getTestRun() . '-key';
$draft = $this->getDraft();
$draft->setKey($key);

$payment = $this->createPayment($draft);

$request = PaymentByKeyGetRequest::ofKey($key);
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);

$this->assertInstanceOf(Payment::class, $payment);
$this->assertSame($payment->getId(), $result->getId());

}

public function testGetById()
{
$draft = $this->getDraft();
23 changes: 23 additions & 0 deletions tests/integration/Payment/PaymentUpdateRequestTest.php
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
use Commercetools\Core\Request\Payments\Command\PaymentTransitionStateAction;
use Commercetools\Core\Request\Payments\PaymentCreateRequest;
use Commercetools\Core\Request\Payments\PaymentDeleteRequest;
use Commercetools\Core\Request\Payments\PaymentUpdateByKeyRequest;
use Commercetools\Core\Request\Payments\PaymentUpdateRequest;

class PaymentUpdateRequestTest extends ApiTestCase
@@ -93,6 +94,28 @@ public function testPaymentPlanned()
$this->assertNotSame($payment->getVersion(), $result->getVersion());
}

public function testUpdateByKey()
{
$key = $this->getTestRun() . '-key';
$draft = $this->getDraft();
$draft->setKey($this->getTestRun() . '-key');
$payment = $this->createPayment($draft);

$customer = $this->getCustomer();
$request = PaymentUpdateByKeyRequest::ofKeyAndVersion($key, $payment->getVersion())
->addAction(
PaymentSetCustomerAction::of()->setCustomer($customer->getReference())
)
;
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);
$this->deleteRequest->setVersion($result->getVersion());

$this->assertInstanceOf(Payment::class, $result);
$this->assertSame($customer->getId(), $result->getCustomer()->getId());
$this->assertNotSame($payment->getVersion(), $result->getVersion());
}

public function testSetCustomer()
{
$draft = $this->getDraft();

0 comments on commit da32ce6

Please # to comment.