Skip to content

Commit

Permalink
[ECP-8715] Update to model based PHP Library
Browse files Browse the repository at this point in the history
  • Loading branch information
hossam-adyen committed Jan 22, 2024
1 parent 3128284 commit ebea23e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
40 changes: 24 additions & 16 deletions Gateway/Http/Client/TransactionPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Adyen\AdyenException;
use Adyen\Client;
use Adyen\Model\Checkout\Amount;
use Adyen\Model\Checkout\CheckoutPaymentMethod;
use Adyen\Model\Checkout\PaymentRequest;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Helper\GiftcardPayment;
use Adyen\Payment\Helper\Idempotency;
Expand Down Expand Up @@ -59,50 +62,55 @@ public function __construct(

public function placeRequest(TransferInterface $transferObject): array
{
$request = $transferObject->getBody();
$requestData = $transferObject->getBody();
$headers = $transferObject->getHeaders();
$clientConfig = $transferObject->getClientConfig();
$this->remainingOrderAmount = $request['amount']['value'];
$this->remainingOrderAmount = $requestData['amount']['value'];

// If the payments call is already done return the request
if (!empty($request['resultCode'])) {
if (!empty($requestData['resultCode'])) {
//Initiate has already a response
return $request;
return $requestData;
}

$client = $this->adyenHelper->initializeAdyenClientWithClientConfig($clientConfig);
$service = $this->adyenHelper->createAdyenCheckoutService($client);
$service = $this->adyenHelper->createAdyenPaymentsApiService($client);
$oldService = $this->adyenHelper->createAdyenCheckoutService($client);
$paymentRequest = new PaymentRequest($requestData);
$responseArray = [];

try {
list($request, $giftcardResponse) = $this->processGiftcards($request, $service);
list($requestData, $giftcardResponse) = $this->processGiftcards($requestData, $oldService);
if (isset($giftcardResponse) && $this->remainingOrderAmount === 0) {
return $giftcardResponse;
}

$idempotencyKey = $this->idempotencyHelper->generateIdempotencyKey(
$request,
$requestData,
$headers['idempotencyExtraData'] ?? null
);
$requestOptions['idempotencyKey'] = $idempotencyKey;

$this->adyenHelper->logRequest($request, Client::API_CHECKOUT_VERSION, '/payments');
$response = $service->payments($request, $requestOptions);
$this->adyenHelper->logRequest($requestData, Client::API_CHECKOUT_VERSION, '/payments');
$response = $service->payments($paymentRequest, $requestOptions);

// Store the /payments response in the database in case it is needed in order to finish the payment
/** @var PaymentResponse $paymentResponse */
$paymentResponse = $this->paymentResponseFactory->create();
$paymentResponse->setResponse(json_encode($response));
$paymentResponse->setResultCode($response['resultCode']);
$paymentResponse->setMerchantReference($request["reference"]);
$jsonResponse = json_encode($response);
$paymentResponse->setResponse($jsonResponse);
$responseArray = json_decode($jsonResponse, true);
$paymentResponse->setResultCode($responseArray['resultCode']);
$paymentResponse->setMerchantReference($requestData["reference"]);

$this->paymentResponseResourceModel->save($paymentResponse);
} catch (AdyenException $e) {
$response['error'] = $e->getMessage();
$response['errorCode'] = $e->getAdyenErrorCode();
$responseArray['error'] = $e->getMessage();
$responseArray['errorCode'] = $e->getAdyenErrorCode();
}
$this->adyenHelper->logResponse($response);
$this->adyenHelper->logResponse($responseArray);

return $response;
return $responseArray;
}

private function handleGiftcardPayments(
Expand Down
7 changes: 7 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Adyen\Client;
use Adyen\Environment;
use Adyen\Service\Checkout;
use Adyen\Service\Checkout\PaymentsApi;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\Config\Source\RenderMode;
use Adyen\Payment\Model\RecurringType;
Expand Down Expand Up @@ -1353,6 +1354,7 @@ public function isHppVaultEnabled($storeId = null)
* @return Checkout
* @throws AdyenException
* @throws NoSuchEntityException
* @deprecared use createAdyenPaymentsApiService() instead
*/
public function createAdyenCheckoutService(Client $client = null): Checkout
{
Expand All @@ -1363,6 +1365,11 @@ public function createAdyenCheckoutService(Client $client = null): Checkout
return new Checkout($client);
}

public function createAdyenPaymentsApiService(Client $client = null): PaymentsApi
{
return new PaymentsApi($client ?? $this->initializeAdyenClient());
}

/**
* @param $client
* @return Recurring
Expand Down

0 comments on commit ebea23e

Please sign in to comment.