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

Commit da32ce6

Browse files
author
Jens Schulze
committed
feat(Payment): support get/update/delete payment by key
Closes #349
1 parent e68aaf0 commit da32ce6

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Payments;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\Type\Type;
10+
use Commercetools\Core\Request\AbstractByKeyGetRequest;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Commercetools\Core\Model\Payment\Payment;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Payments
17+
* @link https://dev.commercetools.com/http-api-projects-payments.html#get-payment-by-key
18+
* @method Payment mapResponse(ApiResponseInterface $response)
19+
* @method Payment mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class PaymentByKeyGetRequest extends AbstractByKeyGetRequest
22+
{
23+
protected $resultClass = Payment::class;
24+
25+
/**
26+
* @param string $key
27+
* @param Context $context
28+
*/
29+
public function __construct($key, Context $context = null)
30+
{
31+
parent::__construct(PaymentsEndpoint::endpoint(), $key, $context);
32+
}
33+
34+
/**
35+
* @param string $key
36+
* @param Context $context
37+
* @return static
38+
*/
39+
public static function ofKey($key, Context $context = null)
40+
{
41+
return new static($key, $context);
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Payments;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
10+
use Commercetools\Core\Model\Type\Type;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Commercetools\Core\Model\Payment\Payment;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Payments
17+
* @link https://dev.commercetools.com/http-api-projects-payments.html#delete-payment-by-key
18+
* @method Payment mapResponse(ApiResponseInterface $response)
19+
* @method Payment mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class PaymentDeleteByKeyRequest extends AbstractDeleteByKeyRequest
22+
{
23+
protected $resultClass = Payment::class;
24+
25+
/**
26+
* @param string $id
27+
* @param int $version
28+
* @param Context $context
29+
*/
30+
public function __construct($id, $version, Context $context = null)
31+
{
32+
parent::__construct(PaymentsEndpoint::endpoint(), $id, $version, $context);
33+
}
34+
35+
/**
36+
* @param string $key
37+
* @param int $version
38+
* @param Context $context
39+
* @return static
40+
*/
41+
public static function ofKeyAndVersion($key, $version, Context $context = null)
42+
{
43+
return new static($key, $version, $context);
44+
}
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Payments;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractUpdateByKeyRequest;
10+
use Commercetools\Core\Request\AbstractUpdateRequest;
11+
use Commercetools\Core\Model\Payment\Payment;
12+
use Commercetools\Core\Response\ApiResponseInterface;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Payments
17+
* @link https://dev.commercetools.com/http-api-projects-payments.html#update-payment-by-key
18+
* @method Payment mapResponse(ApiResponseInterface $response)
19+
* @method Payment mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class PaymentUpdateByKeyRequest extends AbstractUpdateByKeyRequest
22+
{
23+
protected $resultClass = Payment::class;
24+
25+
/**
26+
* @param string $key
27+
* @param string $version
28+
* @param array $actions
29+
* @param Context $context
30+
*/
31+
public function __construct($key, $version, array $actions = [], Context $context = null)
32+
{
33+
parent::__construct(PaymentsEndpoint::endpoint(), $key, $version, $actions, $context);
34+
}
35+
36+
/**
37+
* @param string $key
38+
* @param int $version
39+
* @param Context $context
40+
* @return static
41+
*/
42+
public static function ofKeyAndVersion($key, $version, Context $context = null)
43+
{
44+
return new static($key, $version, [], $context);
45+
}
46+
}

tests/integration/Payment/PaymentQueryRequestTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Commercetools\Core\Model\Payment\PaymentDraft;
1313
use Commercetools\Core\Model\Payment\PaymentMethodInfo;
1414
use Commercetools\Core\Request\Payments\PaymentByIdGetRequest;
15+
use Commercetools\Core\Request\Payments\PaymentByKeyGetRequest;
1516
use Commercetools\Core\Request\Payments\PaymentCreateRequest;
1617
use Commercetools\Core\Request\Payments\PaymentDeleteRequest;
1718
use Commercetools\Core\Request\Payments\PaymentQueryRequest;
@@ -63,6 +64,23 @@ public function testQuery()
6364
$this->assertSame($payment->getId(), $result->getAt(0)->getId());
6465
}
6566

67+
public function testGetByKey()
68+
{
69+
$key = $this->getTestRun() . '-key';
70+
$draft = $this->getDraft();
71+
$draft->setKey($key);
72+
73+
$payment = $this->createPayment($draft);
74+
75+
$request = PaymentByKeyGetRequest::ofKey($key);
76+
$response = $request->executeWithClient($this->getClient());
77+
$result = $request->mapResponse($response);
78+
79+
$this->assertInstanceOf(Payment::class, $payment);
80+
$this->assertSame($payment->getId(), $result->getId());
81+
82+
}
83+
6684
public function testGetById()
6785
{
6886
$draft = $this->getDraft();

tests/integration/Payment/PaymentUpdateRequestTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Commercetools\Core\Request\Payments\Command\PaymentTransitionStateAction;
3939
use Commercetools\Core\Request\Payments\PaymentCreateRequest;
4040
use Commercetools\Core\Request\Payments\PaymentDeleteRequest;
41+
use Commercetools\Core\Request\Payments\PaymentUpdateByKeyRequest;
4142
use Commercetools\Core\Request\Payments\PaymentUpdateRequest;
4243

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

97+
public function testUpdateByKey()
98+
{
99+
$key = $this->getTestRun() . '-key';
100+
$draft = $this->getDraft();
101+
$draft->setKey($this->getTestRun() . '-key');
102+
$payment = $this->createPayment($draft);
103+
104+
$customer = $this->getCustomer();
105+
$request = PaymentUpdateByKeyRequest::ofKeyAndVersion($key, $payment->getVersion())
106+
->addAction(
107+
PaymentSetCustomerAction::of()->setCustomer($customer->getReference())
108+
)
109+
;
110+
$response = $request->executeWithClient($this->getClient());
111+
$result = $request->mapResponse($response);
112+
$this->deleteRequest->setVersion($result->getVersion());
113+
114+
$this->assertInstanceOf(Payment::class, $result);
115+
$this->assertSame($customer->getId(), $result->getCustomer()->getId());
116+
$this->assertNotSame($payment->getVersion(), $result->getVersion());
117+
}
118+
96119
public function testSetCustomer()
97120
{
98121
$draft = $this->getDraft();

0 commit comments

Comments
 (0)