From 05785945efd41e79a866cc723d4446d89fdf87a6 Mon Sep 17 00:00:00 2001 From: Volodymyr Stolyarchuk Date: Wed, 28 Feb 2024 13:52:07 +0200 Subject: [PATCH 1/3] init PoC --- .env.example | 3 + .gitignore | 2 + Resources/ExternalConfiguration.php | 14 +- ...nWithInstrumentIdentifierTokenCreation.php | 301 ++++++++++++++++++ composer.json | 4 +- 5 files changed, 320 insertions(+), 4 deletions(-) create mode 100644 .env.example create mode 100644 Samples/PoC/AuthorizationWithInstrumentIdentifierTokenCreation.php diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d7770e9 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +MERCHANT_ID= +API_KEY_ID= +SECRET_KEY= \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4950b51..e43f74e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ vendor Log .idea *.log +/composer.lock +/.env diff --git a/Resources/ExternalConfiguration.php b/Resources/ExternalConfiguration.php index 0d9f72e..6ce83fd 100644 --- a/Resources/ExternalConfiguration.php +++ b/Resources/ExternalConfiguration.php @@ -3,6 +3,8 @@ * Purpose : passing Authentication config object to the configuration */ namespace CyberSource; +use Dotenv\Dotenv; + require_once __DIR__. DIRECTORY_SEPARATOR .'../vendor/autoload.php'; class ExternalConfiguration @@ -13,10 +15,11 @@ class ExternalConfiguration //initialize variable on constructor function __construct() { + $this->loadEnvConfig(); $this->authType = "http_signature";//http_signature/jwt - $this->merchantID = "testrest"; - $this->apiKeyID = "08c94330-f618-42a3-b09d-e1e43be5efda"; - $this->secretKey = "yBJxy6LjM2TmcPGu+GaJrHtkke25fPpUX+UY6/L/1tE="; + $this->merchantID = getenv("MERCHANT_ID"); + $this->apiKeyID = getenv("API_KEY_ID"); + $this->secretKey = getenv("SECRET_KEY"); // MetaKey configuration [Start] $this->useMetaKey = false; @@ -59,6 +62,11 @@ function __construct() $this->merchantConfigObjectForIntermediateHost(); $this->jwePEMFileDirectory = "Resources".DIRECTORY_SEPARATOR."NetworkTokenCert.pem"; } + private function loadEnvConfig() + { + $dotenv = Dotenv::createUnsafeImmutable(__DIR__.DIRECTORY_SEPARATOR.".."); + $dotenv->safeLoad(); + } //creating merchant config object function merchantConfigObject() diff --git a/Samples/PoC/AuthorizationWithInstrumentIdentifierTokenCreation.php b/Samples/PoC/AuthorizationWithInstrumentIdentifierTokenCreation.php new file mode 100644 index 0000000..d5b7834 --- /dev/null +++ b/Samples/PoC/AuthorizationWithInstrumentIdentifierTokenCreation.php @@ -0,0 +1,301 @@ +toString(); + $clientReferenceInformationArr = [ + "code" => $merchantReferenceCode + ]; + $clientReferenceInformation = new CyberSource\Model\Ptsv2paymentsClientReferenceInformation($clientReferenceInformationArr); + $processingInformationArr = [ + "actionList" => [ + "DECISION_SKIP", + "TOKEN_CREATE", + ], + "actionTokenTypes" => [ + "instrumentIdentifier", + "customer", + "paymentInstrument", + ], + "capture" => false, + "commerceIndicator" => "internet" + ]; + $processingInformation = new CyberSource\Model\Ptsv2paymentsProcessingInformation($processingInformationArr); + + $paymentInformationCardArr = [ + "number" => "4111111111111111", + "expirationMonth" => "12", + "expirationYear" => "2031", + "securityCode" => "123" + ]; + $paymentInformationCard = new CyberSource\Model\Ptsv2paymentsPaymentInformationCard($paymentInformationCardArr); + + $paymentInformationArr = [ + "card" => $paymentInformationCard + ]; + $paymentInformation = new CyberSource\Model\Ptsv2paymentsPaymentInformation($paymentInformationArr); + + $orderInformationAmountDetailsArr = [ + "totalAmount" => "102.21", + "currency" => "USD" + ]; + $orderInformationAmountDetails = new CyberSource\Model\Ptsv2paymentsOrderInformationAmountDetails($orderInformationAmountDetailsArr); + + $orderInformationBillToArr = [ + "firstName" => "John", + "lastName" => "Doe", + "address1" => "1 Market St", + "locality" => "san francisco", + "administrativeArea" => "CA", + "postalCode" => "94105", + "country" => "US", + "email" => "test@cybs.com", + "phoneNumber" => "4158880000" + ]; + $orderInformationBillTo = new CyberSource\Model\Ptsv2paymentsOrderInformationBillTo($orderInformationBillToArr); + + $orderInformationShipToArr = [ + "firstName" => "John", + "lastName" => "Doe", + "address1" => "1 Market St", + "locality" => "san francisco", + "administrativeArea" => "CA", + "postalCode" => "94105", + "country" => "US" + ]; + $orderInformationShipTo = new CyberSource\Model\Ptsv2paymentsOrderInformationShipTo($orderInformationShipToArr); + + $orderInformationArr = [ + "amountDetails" => $orderInformationAmountDetails, + "billTo" => $orderInformationBillTo, + "shipTo" => $orderInformationShipTo + ]; + $orderInformation = new CyberSource\Model\Ptsv2paymentsOrderInformation($orderInformationArr); + + $requestObjArr = [ + "clientReferenceInformation" => $clientReferenceInformation, + "processingInformation" => $processingInformation, + "paymentInformation" => $paymentInformation, + "orderInformation" => $orderInformation + ]; + $requestObj = new CyberSource\Model\CreatePaymentRequest($requestObjArr); + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\PaymentsApi($api_client); + + try { + $apiResponse = $api_instance->createPayment($requestObj); + /** @var \CyberSource\Model\PtsV2PaymentsPost201Response $paymentResponse */ + $paymentResponse = $apiResponse[0]; + var_dump($paymentResponse->getId()); + $tokenInformation = $paymentResponse->getTokenInformation(); + var_dump($tokenInformation->getCustomer()->getId()); + var_dump($tokenInformation->getPaymentInstrument()->getId()); + var_dump($tokenInformation->getInstrumentIdentifier()->getId()); + var_dump($apiResponse); + CapturePayment($paymentResponse->getId(), 50); + RefundCapture($paymentResponse->getId(),50); + PurchaseWithCustomerTokenId($tokenInformation->getCustomer()->getId()); + + WriteLogAudit($paymentResponse); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + var_dump($e->getResponseBody()); + var_dump($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + +function RefundCapture(string $gatewayAuthorisation, float $amount) +{ + $merchantReferenceCode = Uuid::uuid4()->toString(); + $clientReferenceInformationArr = [ + "code" => $merchantReferenceCode + ]; + $clientReferenceInformation = new CyberSource\Model\Ptsv2paymentsidrefundsClientReferenceInformation($clientReferenceInformationArr); + + $orderInformationAmountDetailsArr = [ + "totalAmount" => $amount, + "currency" => "USD" + ]; + $orderInformationAmountDetails = new CyberSource\Model\Ptsv2paymentsidcapturesOrderInformationAmountDetails($orderInformationAmountDetailsArr); + + $orderInformationArr = [ + "amountDetails" => $orderInformationAmountDetails + ]; + $orderInformation = new CyberSource\Model\Ptsv2paymentsidrefundsOrderInformation($orderInformationArr); + + $requestObjArr = [ + "clientReferenceInformation" => $clientReferenceInformation, + "orderInformation" => $orderInformation + ]; + $requestObj = new CyberSource\Model\RefundCaptureRequest($requestObjArr); + + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\RefundApi($api_client); + + try { + $apiResponse = $api_instance->refundCapture($requestObj, $gatewayAuthorisation); + print_r(PHP_EOL); + print_r($apiResponse); + + WriteLogAudit($apiResponse[1]); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + print_r($e->getResponseBody()); + print_r($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + + +function CapturePayment(string $id, float $amount) +{ + $merchantReferenceCode = Uuid::uuid4()->toString(); + $clientReferenceInformationArr = [ + "code" => $merchantReferenceCode + ]; + $clientReferenceInformation = new CyberSource\Model\Ptsv2paymentsClientReferenceInformation($clientReferenceInformationArr); + + $orderInformationAmountDetailsArr = [ + "totalAmount" => $amount, + "currency" => "USD" + ]; + $orderInformationAmountDetails = new CyberSource\Model\Ptsv2paymentsidcapturesOrderInformationAmountDetails($orderInformationAmountDetailsArr); + + $orderInformationArr = [ + "amountDetails" => $orderInformationAmountDetails + ]; + $orderInformation = new CyberSource\Model\Ptsv2paymentsidcapturesOrderInformation($orderInformationArr); + + $requestObjArr = [ + "clientReferenceInformation" => $clientReferenceInformation, + "orderInformation" => $orderInformation + ]; + $requestObj = new CyberSource\Model\CapturePaymentRequest($requestObjArr); + + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\CaptureApi($api_client); + + try { + $apiResponse = $api_instance->capturePayment($requestObj, $id); + print_r(PHP_EOL); + print_r($apiResponse); + + WriteLogAudit($apiResponse[1]); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + print_r($e->getResponseBody()); + print_r($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + + + + +function PurchaseWithCustomerTokenId(string $customerId) +{ + $merchantReferenceCode = Uuid::uuid4()->toString(); + $clientReferenceInformationArr = [ + "code" => $merchantReferenceCode + ]; + + $processingInformationArr = [ + "capture" => true, + ]; + $processingInformation = new CyberSource\Model\Ptsv2paymentsProcessingInformation($processingInformationArr); + + + $clientReferenceInformation = new CyberSource\Model\Ptsv2paymentsClientReferenceInformation($clientReferenceInformationArr); + + $paymentInformationCustomerArr = [ + "id" => $customerId, + ]; + $paymentInformationCustomer = new CyberSource\Model\Ptsv2paymentsPaymentInformationCustomer($paymentInformationCustomerArr); + + $paymentInformationArr = [ + "customer" => $paymentInformationCustomer + ]; + $paymentInformation = new CyberSource\Model\Ptsv2paymentsPaymentInformation($paymentInformationArr); + + $orderInformationAmountDetailsArr = [ + "totalAmount" => "102.21", + "currency" => "USD" + ]; + $orderInformationAmountDetails = new CyberSource\Model\Ptsv2paymentsOrderInformationAmountDetails($orderInformationAmountDetailsArr); + + $orderInformationArr = [ + "amountDetails" => $orderInformationAmountDetails + ]; + $orderInformation = new CyberSource\Model\Ptsv2paymentsOrderInformation($orderInformationArr); + + $requestObjArr = [ + "clientReferenceInformation" => $clientReferenceInformation, + "processingInformation" => $processingInformation, + "paymentInformation" => $paymentInformation, + "orderInformation" => $orderInformation + ]; + $requestObj = new CyberSource\Model\CreatePaymentRequest($requestObjArr); + + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\PaymentsApi($api_client); + + try { + $apiResponse = $api_instance->createPayment($requestObj); + print_r(PHP_EOL); + print_r($apiResponse); + + WriteLogAudit($apiResponse[1]); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + print_r($e->getResponseBody()); + print_r($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + + + + +if (!function_exists('WriteLogAudit')) { + function WriteLogAudit($status) + { + $sampleCode = basename(__FILE__, '.php'); + print_r("\n[Sample Code Testing] [$sampleCode] $status"); + } +} + +if (!defined('DO_NOT_RUN_SAMPLES')) { + echo "\nAuthorizationWithInstrumentIdentifierTokenCreation Sample Code is Running..." . PHP_EOL; + AuthorizationWithInstrumentIdentifierTokenCreation(); +} +?> diff --git a/composer.json b/composer.json index 454b039..6c0e6c0 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,8 @@ "require": { "php": ">=8.0.0", "cybersource/rest-client-php": "0.0.47", - "firebase/php-jwt": "^6.0.0" + "firebase/php-jwt": "^6.0.0", + "ramsey/uuid": "4.7.5", + "vlucas/phpdotenv": "^5.5" } } From 9903c13e02911fa969f7bff7d1d8ef5d3081f0a2 Mon Sep 17 00:00:00 2001 From: Volodymyr Stolyarchuk Date: Wed, 28 Feb 2024 15:32:50 +0200 Subject: [PATCH 2/3] add card --- Samples/PoC/CreatePaymentInstrumentCard.php | 285 ++++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 Samples/PoC/CreatePaymentInstrumentCard.php diff --git a/Samples/PoC/CreatePaymentInstrumentCard.php b/Samples/PoC/CreatePaymentInstrumentCard.php new file mode 100644 index 0000000..1c299b4 --- /dev/null +++ b/Samples/PoC/CreatePaymentInstrumentCard.php @@ -0,0 +1,285 @@ +} $response */ + $response = CreateInstrumentIdentifierCard(); + + $instrumentIdentifierId = $response[0]->getId(); + var_dump("Payment instrument identifier: {$instrumentIdentifierId}"); + $cardArr = [ + "expirationMonth" => "12", + "expirationYear" => "2031", + "type" => "visa" + ]; + $card = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentCard($cardArr); + + $billToArr = [ + "firstName" => "John", + "lastName" => "Doe", + "company" => "Cybersource", + "address1" => "1 Market St", + "locality" => "San Francisco", + "administrativeArea" => "CA", + "postalCode" => "94105", + "country" => "US", + "email" => "test@cybs.com", + "phoneNumber" => "4158880000" + ]; + $billTo = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentBillTo($billToArr); + + $instrumentIdentifierArr = [ + "id" => $instrumentIdentifierId + ]; + $instrumentIdentifier = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentInstrumentIdentifier($instrumentIdentifierArr); + + $requestObjArr = [ + "card" => $card, + "billTo" => $billTo, + "instrumentIdentifier" => $instrumentIdentifier + ]; + $requestObj = new CyberSource\Model\PostPaymentInstrumentRequest($requestObjArr); + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\PaymentInstrumentApi($api_client); + + try { + /** @var array{\CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrument, string, array} $apiResponse */ + $apiResponse = $api_instance->postPaymentInstrument($requestObj); + $instrumentId = $apiResponse[0]->getId(); + var_dump("Payment instrument identifier: {$instrumentId}"); + print_r(PHP_EOL); + print_r($apiResponse); + + WriteLogAudit($apiResponse[1]); + CreateCustomer($instrumentId); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + print_r($e->getResponseBody()); + print_r($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + +function CreateCustomer(string $paymentInstrumentId) +{ + $merchantCustomerId = Uuid::uuid4()->toString(); + var_dump("merchantCustomerId - $merchantCustomerId"); + $buyerInformationArr = [ + "merchantCustomerID" => $merchantCustomerId, + "email" => "test@cybs.com" + ]; + $buyerInformation = new CyberSource\Model\Tmsv2customersBuyerInformation($buyerInformationArr); + + $clientReferenceInformationArr = [ + "code" => "TC50171_3" + ]; + $clientReferenceInformation = new CyberSource\Model\Tmsv2customersClientReferenceInformation($clientReferenceInformationArr); + + $merchantDefinedInformation = array(); + $merchantDefinedInformation_0 = [ + "name" => "data1", + "value" => "Your customer data" + ]; + $merchantDefinedInformation[0] = new CyberSource\Model\Tmsv2customersMerchantDefinedInformation($merchantDefinedInformation_0); + + $requestObjArr = [ + "buyerInformation" => $buyerInformation, + "clientReferenceInformation" => $clientReferenceInformation, + "merchantDefinedInformation" => $merchantDefinedInformation, + ]; + $requestObj = new CyberSource\Model\PostCustomerRequest($requestObjArr); + + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\CustomerApi($api_client); + + try { + /** @var array{\CyberSource\Model\TmsV2CustomersResponse, string, array} $apiResponse */ + $apiResponse = $api_instance->postCustomer($requestObj); + $customerId = $apiResponse[0]->getId(); + var_dump("Customer id: {$customerId}"); + UpdateCustomersDefaultPaymentInstrument($customerId, $paymentInstrumentId); + PurchaseWithCustomerTokenId($customerId); + print_r(PHP_EOL); + print_r($apiResponse); + + WriteLogAudit($apiResponse[1]); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + print_r($e->getResponseBody()); + print_r($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + +function PurchaseWithCustomerTokenId(string $customerId) +{ + $merchantReferenceCode = Uuid::uuid4()->toString(); + $clientReferenceInformationArr = [ + "code" => $merchantReferenceCode + ]; + + $processingInformationArr = [ + "capture" => true, + ]; + $processingInformation = new CyberSource\Model\Ptsv2paymentsProcessingInformation($processingInformationArr); + + + $clientReferenceInformation = new CyberSource\Model\Ptsv2paymentsClientReferenceInformation($clientReferenceInformationArr); + + $paymentInformationCustomerArr = [ + "id" => $customerId, + ]; + $paymentInformationCustomer = new CyberSource\Model\Ptsv2paymentsPaymentInformationCustomer($paymentInformationCustomerArr); + + $paymentInformationArr = [ + "customer" => $paymentInformationCustomer + ]; + $paymentInformation = new CyberSource\Model\Ptsv2paymentsPaymentInformation($paymentInformationArr); + + $orderInformationAmountDetailsArr = [ + "totalAmount" => "102.21", + "currency" => "USD" + ]; + $orderInformationAmountDetails = new CyberSource\Model\Ptsv2paymentsOrderInformationAmountDetails($orderInformationAmountDetailsArr); + + $orderInformationArr = [ + "amountDetails" => $orderInformationAmountDetails + ]; + $orderInformation = new CyberSource\Model\Ptsv2paymentsOrderInformation($orderInformationArr); + + $requestObjArr = [ + "clientReferenceInformation" => $clientReferenceInformation, + "processingInformation" => $processingInformation, + "paymentInformation" => $paymentInformation, + "orderInformation" => $orderInformation + ]; + $requestObj = new CyberSource\Model\CreatePaymentRequest($requestObjArr); + + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\PaymentsApi($api_client); + + try { + $apiResponse = $api_instance->createPayment($requestObj); + print_r(PHP_EOL); + print_r($apiResponse); + + WriteLogAudit($apiResponse[1]); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + print_r($e->getResponseBody()); + print_r($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + +function UpdateCustomersDefaultPaymentInstrument(string $customerTokenId, string $paymentInstrumentId) +{ + $defaultPaymentInstrumentArr = [ + "id" => $paymentInstrumentId + ]; + $defaultPaymentInstrument = new CyberSource\Model\Tmsv2customersDefaultPaymentInstrument($defaultPaymentInstrumentArr); + + $requestObjArr = [ + "defaultPaymentInstrument" => $defaultPaymentInstrument + ]; + $requestObj = new CyberSource\Model\PatchCustomerRequest($requestObjArr); + + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\CustomerApi($api_client); + + try { + $apiResponse = $api_instance->patchCustomer($customerTokenId, $requestObj, null, null); + print_r(PHP_EOL); + print_r($apiResponse); + + WriteLogAudit($apiResponse[1]); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + print_r($e->getResponseBody()); + print_r($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + + + + +function CreateInstrumentIdentifierCard() +{ + $cardArr = [ + "number" => "4111111111111111" + ]; + $card = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierCard($cardArr); + + $requestObjArr = [ + "card" => $card + ]; + $requestObj = new CyberSource\Model\PostInstrumentIdentifierRequest($requestObjArr); + + $commonElement = new CyberSource\ExternalConfiguration(); + $config = $commonElement->ConnectionHost(); + $merchantConfig = $commonElement->merchantConfigObject(); + + $api_client = new CyberSource\ApiClient($config, $merchantConfig); + $api_instance = new CyberSource\Api\InstrumentIdentifierApi($api_client); + + try { + $apiResponse = $api_instance->postInstrumentIdentifier($requestObj); + print_r(PHP_EOL); + print_r($apiResponse); + + WriteLogAudit($apiResponse[1]); + return $apiResponse; + } catch (Cybersource\ApiException $e) { + print_r($e->getResponseBody()); + print_r($e->getMessage()); + $errorCode = $e->getCode(); + WriteLogAudit($errorCode); + } +} + + + + +if (!function_exists('WriteLogAudit')){ + function WriteLogAudit($status){ + $sampleCode = basename(__FILE__, '.php'); + print_r("\n[Sample Code Testing] [$sampleCode] $status"); + } +} + +if(!defined('DO_NOT_RUN_SAMPLES')){ + echo "\nCreatePaymentInstrumentCard Sample Code is Running..." . PHP_EOL; + CreatePaymentInstrumentCard(); +} +?> From b83d5cf8fd0534abd402a737fd487ae0592af7f7 Mon Sep 17 00:00:00 2001 From: Volodymyr Stolyarchuk Date: Thu, 29 Feb 2024 11:47:32 +0200 Subject: [PATCH 3/3] poc 2 scenarios --- ...entCard.php => addNewCardAndProcessIt.php} | 158 ++++++------------ ...on.php => createNewCardWithProcessing.php} | 0 2 files changed, 55 insertions(+), 103 deletions(-) rename Samples/PoC/{CreatePaymentInstrumentCard.php => addNewCardAndProcessIt.php} (71%) rename Samples/PoC/{AuthorizationWithInstrumentIdentifierTokenCreation.php => createNewCardWithProcessing.php} (100%) diff --git a/Samples/PoC/CreatePaymentInstrumentCard.php b/Samples/PoC/addNewCardAndProcessIt.php similarity index 71% rename from Samples/PoC/CreatePaymentInstrumentCard.php rename to Samples/PoC/addNewCardAndProcessIt.php index 1c299b4..6757f5c 100644 --- a/Samples/PoC/CreatePaymentInstrumentCard.php +++ b/Samples/PoC/addNewCardAndProcessIt.php @@ -1,79 +1,16 @@ } $response */ $response = CreateInstrumentIdentifierCard(); - $instrumentIdentifierId = $response[0]->getId(); - var_dump("Payment instrument identifier: {$instrumentIdentifierId}"); - $cardArr = [ - "expirationMonth" => "12", - "expirationYear" => "2031", - "type" => "visa" - ]; - $card = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentCard($cardArr); - - $billToArr = [ - "firstName" => "John", - "lastName" => "Doe", - "company" => "Cybersource", - "address1" => "1 Market St", - "locality" => "San Francisco", - "administrativeArea" => "CA", - "postalCode" => "94105", - "country" => "US", - "email" => "test@cybs.com", - "phoneNumber" => "4158880000" - ]; - $billTo = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentBillTo($billToArr); - - $instrumentIdentifierArr = [ - "id" => $instrumentIdentifierId - ]; - $instrumentIdentifier = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentInstrumentIdentifier($instrumentIdentifierArr); - - $requestObjArr = [ - "card" => $card, - "billTo" => $billTo, - "instrumentIdentifier" => $instrumentIdentifier - ]; - $requestObj = new CyberSource\Model\PostPaymentInstrumentRequest($requestObjArr); - - $commonElement = new CyberSource\ExternalConfiguration(); - $config = $commonElement->ConnectionHost(); - $merchantConfig = $commonElement->merchantConfigObject(); - - $api_client = new CyberSource\ApiClient($config, $merchantConfig); - $api_instance = new CyberSource\Api\PaymentInstrumentApi($api_client); - - try { - /** @var array{\CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrument, string, array} $apiResponse */ - $apiResponse = $api_instance->postPaymentInstrument($requestObj); - $instrumentId = $apiResponse[0]->getId(); - var_dump("Payment instrument identifier: {$instrumentId}"); - print_r(PHP_EOL); - print_r($apiResponse); - - WriteLogAudit($apiResponse[1]); - CreateCustomer($instrumentId); - return $apiResponse; - } catch (Cybersource\ApiException $e) { - print_r($e->getResponseBody()); - print_r($e->getMessage()); - $errorCode = $e->getCode(); - WriteLogAudit($errorCode); - } -} - -function CreateCustomer(string $paymentInstrumentId) -{ $merchantCustomerId = Uuid::uuid4()->toString(); var_dump("merchantCustomerId - $merchantCustomerId"); $buyerInformationArr = [ @@ -100,8 +37,6 @@ function CreateCustomer(string $paymentInstrumentId) "merchantDefinedInformation" => $merchantDefinedInformation, ]; $requestObj = new CyberSource\Model\PostCustomerRequest($requestObjArr); - - $commonElement = new CyberSource\ExternalConfiguration(); $config = $commonElement->ConnectionHost(); $merchantConfig = $commonElement->merchantConfigObject(); @@ -114,13 +49,8 @@ function CreateCustomer(string $paymentInstrumentId) $apiResponse = $api_instance->postCustomer($requestObj); $customerId = $apiResponse[0]->getId(); var_dump("Customer id: {$customerId}"); - UpdateCustomersDefaultPaymentInstrument($customerId, $paymentInstrumentId); + CreateCustomerDefaultPaymentInstrumentCard($customerId, $instrumentIdentifierId); PurchaseWithCustomerTokenId($customerId); - print_r(PHP_EOL); - print_r($apiResponse); - - WriteLogAudit($apiResponse[1]); - return $apiResponse; } catch (Cybersource\ApiException $e) { print_r($e->getResponseBody()); print_r($e->getMessage()); @@ -182,11 +112,9 @@ function PurchaseWithCustomerTokenId(string $customerId) $api_instance = new CyberSource\Api\PaymentsApi($api_client); try { + /** @var array{PtsV2PaymentsPost201Response, string, array} $apiResponse */ $apiResponse = $api_instance->createPayment($requestObj); - print_r(PHP_EOL); - print_r($apiResponse); - - WriteLogAudit($apiResponse[1]); + var_dump("Payment ID: {$apiResponse[0]->getId()}"); return $apiResponse; } catch (Cybersource\ApiException $e) { print_r($e->getResponseBody()); @@ -196,32 +124,58 @@ function PurchaseWithCustomerTokenId(string $customerId) } } -function UpdateCustomersDefaultPaymentInstrument(string $customerTokenId, string $paymentInstrumentId) +/** + * @param string $customerTokenId + * @param string $instrumentIdentifierId + * @return array{Tmsv2customersEmbeddedDefaultPaymentInstrument, string, array} + */ +function CreateCustomerDefaultPaymentInstrumentCard(string $customerTokenId, string $instrumentIdentifierId) { - $defaultPaymentInstrumentArr = [ - "id" => $paymentInstrumentId + $cardArr = [ + "expirationMonth" => "12", + "expirationYear" => "2031", + "type" => "001" ]; - $defaultPaymentInstrument = new CyberSource\Model\Tmsv2customersDefaultPaymentInstrument($defaultPaymentInstrumentArr); + $card = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentCard($cardArr); - $requestObjArr = [ - "defaultPaymentInstrument" => $defaultPaymentInstrument + $billToArr = [ + "firstName" => "John", + "lastName" => "Doe", + "company" => "CyberSource", + "address1" => "1 Market St", + "locality" => "San Francisco", + "administrativeArea" => "CA", + "postalCode" => "94105", + "country" => "US", + "email" => "test@cybs.com", + "phoneNumber" => "4158880000" ]; - $requestObj = new CyberSource\Model\PatchCustomerRequest($requestObjArr); + $billTo = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentBillTo($billToArr); + $instrumentIdentifierArr = [ + "id" => $instrumentIdentifierId + ]; + $instrumentIdentifier = new CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentInstrumentIdentifier($instrumentIdentifierArr); + + $requestObjArr = [ + "_default" => true, + "card" => $card, + "billTo" => $billTo, + "instrumentIdentifier" => $instrumentIdentifier + ]; + $requestObj = new CyberSource\Model\PostCustomerPaymentInstrumentRequest($requestObjArr); $commonElement = new CyberSource\ExternalConfiguration(); $config = $commonElement->ConnectionHost(); $merchantConfig = $commonElement->merchantConfigObject(); $api_client = new CyberSource\ApiClient($config, $merchantConfig); - $api_instance = new CyberSource\Api\CustomerApi($api_client); + $api_instance = new CyberSource\Api\CustomerPaymentInstrumentApi($api_client); try { - $apiResponse = $api_instance->patchCustomer($customerTokenId, $requestObj, null, null); - print_r(PHP_EOL); - print_r($apiResponse); - - WriteLogAudit($apiResponse[1]); + /** @var array{Tmsv2customersEmbeddedDefaultPaymentInstrument, string, array} $apiResponse */ + $apiResponse = $api_instance->postCustomerPaymentInstrument($customerTokenId, $requestObj, null); + var_dump("Payment Instrument created: {$apiResponse[0]->getId()}"); return $apiResponse; } catch (Cybersource\ApiException $e) { print_r($e->getResponseBody()); @@ -232,8 +186,9 @@ function UpdateCustomersDefaultPaymentInstrument(string $customerTokenId, string } - - +/** + * @return array{\CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifier, string,array } + */ function CreateInstrumentIdentifierCard() { $cardArr = [ @@ -254,11 +209,9 @@ function CreateInstrumentIdentifierCard() $api_instance = new CyberSource\Api\InstrumentIdentifierApi($api_client); try { + /** @var array{\CyberSource\Model\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifier, string,array } $apiResponse */ $apiResponse = $api_instance->postInstrumentIdentifier($requestObj); - print_r(PHP_EOL); - print_r($apiResponse); - - WriteLogAudit($apiResponse[1]); + var_dump("Instrument Identifier created. ID: {$apiResponse[0]->getId()}"); return $apiResponse; } catch (Cybersource\ApiException $e) { print_r($e->getResponseBody()); @@ -269,17 +222,16 @@ function CreateInstrumentIdentifierCard() } - - -if (!function_exists('WriteLogAudit')){ - function WriteLogAudit($status){ +if (!function_exists('WriteLogAudit')) { + function WriteLogAudit($status) + { $sampleCode = basename(__FILE__, '.php'); print_r("\n[Sample Code Testing] [$sampleCode] $status"); } } -if(!defined('DO_NOT_RUN_SAMPLES')){ +if (!defined('DO_NOT_RUN_SAMPLES')) { echo "\nCreatePaymentInstrumentCard Sample Code is Running..." . PHP_EOL; - CreatePaymentInstrumentCard(); + run(); } ?> diff --git a/Samples/PoC/AuthorizationWithInstrumentIdentifierTokenCreation.php b/Samples/PoC/createNewCardWithProcessing.php similarity index 100% rename from Samples/PoC/AuthorizationWithInstrumentIdentifierTokenCreation.php rename to Samples/PoC/createNewCardWithProcessing.php