PHP code example of spare-technologies / php-sdk

1. Go to this page and download the library: Download spare-technologies/php-sdk 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/ */

    

spare-technologies / php-sdk example snippets




use Helpers\Crypto\SpCrypto;

public class MyClass {
    $keys = SpCrypto::GenerateKeyPair();
    
    echo "Private key \n" . nl2br($keys->getPrivateKey());
    echo "\n\n";
    echo "Public key \n" . nl2br($keys->getPublicKey())
}

use Helpers\Security\DigitalSignature\EccSignatureManager;
use Helpers\Serialization\SpSerializer;
use Payment\Client\SpPaymentClient;

public class MyClass {

    // Business ECC private key
    public static string $privateKey = "";

    // Spare ECC public key
    public static string $serverPublicKey = "";

    public static function createPayment(): void {

        // Configure client
        $clientOptions = new SpPaymentClientOptions(
        "https://payment.tryspare.com",
        "Your app id",
        "Your API key"
        );

        $client = new SpPaymentClient($clientOptions);

        // Initialize payment
        $paymentRequest = new SpPaymentRequest();
        $paymentRequest->setAmount(10.0);
        $paymentRequest->setDescription("Test payment");
        $paymentRequest->setOrderId("8jwaQ");

        // Sign the payment 
        $signature = EccSignatureManager::Sign($paymentRequest->toJonsString(), $privateKey);

        // Create payment
        $paymentResponse = $this->paymentClient->CreateDomesticPayment($paymentRequest, $signature);

        // To verify signature of the created payment 
        if (EccSignatureManager::Verify($serverPublicKey, $paymentResponse->getPayment()->toJonsString(), createPayment->getSignature())) {
            // signature verified
        }
    }
}
bash
composer