PHP code example of payever / sdk-php

1. Go to this page and download the library: Download payever/sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

payever / sdk-php example snippets




\Payever\ExternalIntegration\Core\Engine::registerAutoloader();

use Payever\ExternalIntegration\Core\ClientConfiguration;
use Payever\ExternalIntegration\Core\Enum\ChannelSet;

$clientId = 'your-oauth2-client-id';
$clientSecret = 'your-oauth2-client-secret';
$businessUuid = '88888888-4444-4444-4444-121212121212';

$clientConfiguration = new ClientConfiguration();

$clientConfiguration
    ->setClientId($clientId)
    ->setClientSecret($clientSecret)
    ->setBusinessUuid($businessUuid)
    ->setChannelSet(ChannelSet::CHANNEL_MAGENTO)
    ->setApiMode(ClientConfiguration::API_MODE_LIVE)
;

use Psr\Log\LogLevel;
use Payever\ExternalIntegration\Core\Logger\FileLogger;

$logger = new FileLogger(__DIR__.'/payever.log', LogLevel::INFO);
$clientConfiguration->setLogger($logger);

use Payever\ExternalIntegration\Payments\Enum\PaymentMethod;
use Payever\ExternalIntegration\Payments\PaymentsApiClient;
use Payever\ExternalIntegration\Payments\Http\RequestEntity\CreatePaymentRequest;

$paymentsApiClient = new PaymentsApiClient($clientConfiguration);

$createPaymentEntity = new CreatePaymentRequest();

$createPaymentEntity
    ->setOrderId('1001')
    ->setAmount(100.5)
    ->setFee(10)
    ->setCurrency('EUR')
    ->setPaymentMethod(PaymentMethod::METHOD_SANTANDER_DE_INSTALLMENT)
    ->setSalutation('mr')
    ->setFirstName('John')
    ->setLastName('Doe')
    ->setCity('Hamburg')
    ->setCountry('DE')
    ->setZip('10111')
    ->setStreet('Awesome street, 10')
    ->setEmail('[email protected]')
    ->setPhone('+450001122')
    ->setSuccessUrl('https://your.domain/success?paymentId=--PAYMENT-ID--')
    ->setCancelUrl('https://your.domain/checkout?reason=cancel')
    ->setFailureUrl('https://your.domain/checkout?reason=failure')
    ->setNoticeUrl('https://your.domain/async-payment-callback?paymentId=--PAYMENT-ID--')
    ->setCart([
        [
            'name'        => 'test',
            'sku'         => 'test',
            'identifier'  => 'test',
            'price'       => 100.5,
            'vatRate'     => 0,
            'quantity'    => 1,
        ]
    ])
;

try {
    $response = $paymentsApiClient->createPaymentRequest($createPaymentEntity);
    $responseEntity = $response->getResponseEntity();
    
    header(sprintf('Location: %s', $responseEntity->getRedirectUrl()), true);
    exit;
} catch (\Exception $exception) {
    echo $exception->getMessage();
}


use Payever\ExternalIntegration\Payments\PaymentsApiClient;
use Payever\ExternalIntegration\Payments\Http\MessageEntity\RetrievePaymentResultEntity;

$paymentId = '--PAYMENT-ID--';
$paymentsApiClient = new PaymentsApiClient($clientConfiguration);

try {
    $response = $paymentsApiClient->retrievePaymentRequest($paymentId);
    /** @var RetrievePaymentResultEntity $payment */
    $payment = $response->getResponseEntity()->getResult();
    $status = $payment->getStatus();
} catch(\Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\Payments\PaymentsApiClient;
use Payever\ExternalIntegration\Payments\Action\ActionDecider;

$paymentId = '--PAYMENT-ID--';
$paymentsApiClient = new PaymentsApiClient($clientConfiguration);
$actionDecider = new ActionDecider($paymentsApiClient);

try {
    if ($actionDecider->isActionAllowed($paymentId, ActionDecider::ACTION_CANCEL, false)) {
        $paymentsApiClient->cancelPaymentRequest($paymentId);
    }
} catch(\Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\Payments\PaymentsApiClient;
use Payever\ExternalIntegration\Payments\Action\ActionDecider;

$paymentId = '--PAYMENT-ID--';
$paymentsApiClient = new PaymentsApiClient($clientConfiguration);
$actionDecider = new ActionDecider($paymentsApiClient);

try {
    if ($actionDecider->isActionAllowed($paymentId, ActionDecider::ACTION_SHIPPING_GOODS, false)) {
        $paymentsApiClient->shippingGoodsPaymentRequest($paymentId);
    }
} catch(\Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\Core\PseudoRandomStringGenerator;
use Payever\ExternalIntegration\ThirdParty\Enum\ActionEnum;
use Payever\ExternalIntegration\ThirdParty\ThirdPartyApiClient;
use Payever\ExternalIntegration\ThirdParty\Http\RequestEntity\SubscriptionRequestEntity;
use Payever\ExternalIntegration\ThirdParty\Http\MessageEntity\SubscriptionActionEntity;

$tmpApiClient = new ThirdPartyApiClient($clientConfiguration);
$randomSource = new PseudoRandomStringGenerator();
$subscriptionEntity = new SubscriptionRequestEntity();

// save it in persistent storage for future use 
$externalId = $randomSource->generate();
$subscriptionEntity->setExternalId($externalId);

$actionEntity = new SubscriptionActionEntity();
// your webhook details
$actionEntity->setName(ActionEnum::ACTION_CREATE_PRODUCT)
    ->setUrl('https://my.shop.com/webhook/?action=create-product&token=' . $externalId)
    ->setMethod('POST');

$subscriptionEntity->addAction($actionEntity);

try {
    $tmpApiClient->subscribe($subscriptionEntity);
} catch(\Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\ThirdParty\ThirdPartyApiClient;
use Payever\ExternalIntegration\ThirdParty\Http\RequestEntity\SubscriptionRequestEntity;
use Payever\ExternalIntegration\ThirdParty\Http\ResponseEntity\SubscriptionResponseEntity;

$tmpApiClient = new ThirdPartyApiClient($clientConfiguration);
$subscriptionEntity = new SubscriptionRequestEntity();
 
$externalId = ''; // retrieve it from your persistent storage
$subscriptionEntity->setExternalId($externalId);

try {
    $response = $tmpApiClient->getSubscriptionStatus($subscriptionEntity);
    /** @var SubscriptionResponseEntity $subscription */
    $subscription = $response->getResponseEntity(); 
    $id = $subscription->getId();
} catch(\Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\ThirdParty\ThirdPartyApiClient;
use Payever\ExternalIntegration\ThirdParty\Http\RequestEntity\SubscriptionRequestEntity;

$tmpApiClient = new ThirdPartyApiClient($clientConfiguration);
$subscriptionEntity = new SubscriptionRequestEntity();
 
$externalId = ''; // retrieve it from your persistent storage
$subscriptionEntity->setExternalId($externalId);

try {
    $tmpApiClient->unsubscribe($subscriptionEntity);
} catch(\Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\ThirdParty\Action\ActionHandlerInterface;
use Payever\ExternalIntegration\ThirdParty\Action\ActionPayload;
use Payever\ExternalIntegration\ThirdParty\Action\ActionResult;
use Payever\ExternalIntegration\ThirdParty\Enum\ActionEnum;
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRequestEntity;

class MyCreateProductActionHandler implements ActionHandlerInterface
{
  public function getSupportedAction()
  {
    return ActionEnum::ACTION_CREATE_PRODUCT; // Which webhook type this class intends to handle?
  }
  
  public function handle(ActionPayload $actionPayload, ActionResult $actionResult)
  {
    /** @var ProductRequestEntity $payeverProduct */
    $payeverProduct = $actionPayload->getPayloadEntity();

    if (!$payeverProduct->getSku()) {
      $actionResult->addError('Product must have SKU');
      
      return;
    }
    
    if (productExists()) {
      // TODO: update product inside your system
      $actionResult->incrementUpdated();
    } else {
      // TODO: create product inside you system
      $actionResult->incrementCreated();
    }
    
    // no need to handle exceptions here
  }
}

use Psr\Log\LogLevel;
use Payever\ExternalIntegration\Core\Logger\FileLogger;
use Payever\ExternalIntegration\ThirdParty\Action\ActionHandlerPool;
use Payever\ExternalIntegration\ThirdParty\Action\InwardActionProcessor;
use Payever\ExternalIntegration\ThirdParty\Action\ActionResult;

$actionName = $_REQUEST['action'];
$token = $_REQUEST['token'];

if (!$actionName) {
    // respond with 400 HTTP status code
    exit;
}

$externalId = '';// retrieve from your persistent storage

if (!$token || $token !== $externalId) {
    // respond with 401 HTTP status code
    exit;
}

$logger = new FileLogger(__DIR__.'/payever.log', LogLevel::INFO);

try {
    $actionResult = new ActionResult();
    $actionHandlerPool = new ActionHandlerPool([
      new MyCreateProductActionHandler(),
      new MyUpdateProductActionHandler(),
      //...  
    ]);
    $actionProcessor = new InwardActionProcessor(
        $actionHandlerPool,
        $actionResult,
        $logger
    );

    $actionResult = $actionProcessor->process($actionName);

    echo (string) $actionResult;
    // respond with 200 status
} catch (\Exception $exception) {
    $logger->critical(sprintf('Webhook processing failed: %s', $exception->getMessage()));
    // respond with 500 status
}

use Payever\ExternalIntegration\Products\ProductsApiClient;
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRequestEntity;

$productsApiClient = new ProductsApiClient($clientConfiguration);
$productEntity = new ProductRequestEntity();

$productEntity->setTitle('Awesome Product')
    ->setActive(true)
    ->setSku('AWSM1')
    ->setPrice(100.5)
    ->setCurrency('EUR')
    ->setDescription('Awesome product description')
    ->setImages([
        'https://my.shop.com/images/awsm1.png',
    ])
    ->setCategories([
        'Awesome Goods',
    ])
    ->setShipping([
        'width' => 1,
        'height' => 1,
        'length' => 1,
        'weight' => 2,
    ]);

$externalId = '';// retrieve it from your persistent storage
$productEntity->setExternalId($externalId);

try {
    $productsApiClient->createOrUpdateProduct($productEntity);
} catch(\Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\Products\ProductsApiClient;
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRemovedRequestEntity;

$productsApiClient = new ProductsApiClient($clientConfiguration);
$productRemovedEntity = new ProductRemovedRequestEntity();
$productRemovedEntity->setSku('AWSM1');

$externalId = '';// retrieve it from your persistent storage
$productRemovedEntity->setExternalId($externalId);

try {
    $productsApiClient->removeProduct($productRemovedEntity);
} catch(\Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\Inventory\InventoryApiClient;
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryCreateRequestEntity;

$inventoryApiClient = new InventoryApiClient($clientConfiguration);

$inventoryCreateEntity = new InventoryCreateRequestEntity();
$inventoryCreateEntity->setSku('AWSM1')
    ->setStock(15);

$externalId = '';// retrieve it from your persistent storage
$inventoryCreateEntity->setExternalId($externalId);

try {
    $inventoryApiClient->createInventory($inventoryCreateEntity);
} catch (Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\Inventory\InventoryApiClient;
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryChangedRequestEntity;

$inventoryApiClient = new InventoryApiClient($clientConfiguration);

$inventoryChangeEntity = new InventoryChangedRequestEntity();
$inventoryChangeEntity->setSku('AWSM1')
    ->setQuantity(5); // read as: +5 to existing value

$externalId = '';// retrieve it from your persistent storage
$inventoryChangeEntity->setExternalId($externalId);

try {
    $inventoryApiClient->addInventory($inventoryChangeEntity);
} catch (Exception $exception) {
    echo $exception->getMessage();
}

use Payever\ExternalIntegration\Inventory\InventoryApiClient;
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryChangedRequestEntity;

$inventoryApiClient = new InventoryApiClient($clientConfiguration);

$inventoryChangeEntity = new InventoryChangedRequestEntity();
$inventoryChangeEntity->setSku('AWSM1')
    ->setQuantity(3); // read as: -3 to existing value

$externalId = '';// retrieve it from your persistent storage
$inventoryChangeEntity->setExternalId($externalId);

try {
    $inventoryApiClient->subtractInventory($inventoryChangeEntity);
} catch (Exception $exception) {
    echo $exception->getMessage();
}