emanuelecoppola/satispay-php-sdk

v1.0.0 2024-07-07 16:52 UTC

This package is auto-updated.

Last update: 2024-09-26 18:08:49 UTC


README

License PHP Version

这是集成Satispay API最完整的PHP SDK。

它提供了一个全面的解决方案,支持所有Satispay API功能,允许无缝地将支付功能集成到您的PHP应用程序中。

目前由以下人员维护此软件

目录

入门

要求: PHP 5.4+, ext-curl, ext-mbstring, ext-json

首先,通过Composer包管理器安装SDK

composer require emanuelecoppola/satispay-php-sdk

如果您正在使用5.x版本的PHP,请确保使用具有composer-runtime-api:^2的Composer 2.x版本。
根据Composer系统要求

最新版本的Composer需要PHP 7.2.5才能运行。长期支持版本(2.2.x)仍然为PHP 5.3.2+提供支持,以防您陷入旧版本的PHP。还需要一些敏感的php设置和编译标志,但在使用安装程序时,您将收到任何不兼容性的警告。

同时确保允许php-http/discoveryComposer插件运行。
这将允许自动发现PSR-18 HTTP客户端。

如果您的项目中没有可用的PSR-18 HTTP客户端实现,您可以手动安装一个客户端

composer require guzzlehttp/guzzle

然后,您就可以开始与Satispay API交互了

$yourPublicKey = getenv('SATISPAY_PUBLIC_KEY');
$yourPrivateKey = getenv('SATISPAY_PRIVATE_KEY');
$yourKeyId = getenv('SATISPAY_KEY_ID');

$satispayGBusinessClient = new SatispayGBusinessClient([

    // authentication
    // set these three keys only if you are already authenticated
    // if you're not, then follow the authentication example
    'public_key' => $yourPublicKey,
    'private_key' => $yourPrivateKey,
    'key_id' => $yourKeyId,

    'sandbox' => true
]);

$payment = $satispayGBusinessClient->payments->create([
    'flow' => 'MATCH_CODE',
    'currency' => 'EUR',
    'amount_unit' => 12.99 * 100, // 12,99€
]);

echo $payment->toJson();

如果需要,可以指定不同的PSR-18客户端实现。

use Psr\Http\Client\ClientInterface;

$yourPublicKey = getenv('SATISPAY_PUBLIC_KEY');
$yourPrivateKey = getenv('SATISPAY_PRIVATE_KEY');
$yourKeyId = getenv('SATISPAY_KEY_ID');

$satispayGBusinessClient = new SatispayGBusinessClient([

    // authentication
    // set these three keys only if you are already authenticated
    // if you're not, then follow the authentication example
    'public_key' => $yourPublicKey,
    'private_key' => $yourPrivateKey,
    'key_id' => $yourKeyId,

    'psr' => [
        ClientInterface::class => new \GuzzleHttp\Client([])
    ],

    'sandbox' => true
]);

$payment = $satispayGBusinessClient->payments->create([
    'flow' => 'MATCH_CODE',
    'currency' => 'EUR',
    'amount_unit' => 12.99 * 100, // 12,99€
]);

echo $payment->toJson();

支持的API

此SDK支持以下API

使用方法

客户端实例化

use EmanueleCoppola\Satispay\SatispayGAgentClient;
use EmanueleCoppola\Satispay\SatispayGBusinessClient;

use Psr\Http\Client\ClientInterface;

use EmanueleCoppola\Satispay\Services\RSAService\OpenSSL_RSAService;
use EmanueleCoppola\Satispay\Services\RSAService\SeclibRSAService;

use EmanueleCoppola\Satispay\SatispayHeaders;

$yourPublicKey = getenv('SATISPAY_PUBLIC_KEY');
$yourPrivateKey = getenv('SATISPAY_PRIVATE_KEY');
$yourKeyId = getenv('SATISPAY_KEY_ID');

// you could either instantiate a SatispayGBusinessClient instance
// or a SatispayGAgentClient instance based on your needs
// the constructor is the same in both classes
$satispayGBusinessClient = new SatispayGBusinessClient([
 
    // authentication
    // set these three keys only if you are already authenticated
    // if you're not, then follow the authentication example
    'public_key' => $yourPublicKey,
    'private_key' => $yourPrivateKey,
    'key_id' => $yourKeyId,

    // here you can specify the passphrase set in the RSA keys
    'passphrase' => null,

    // here you can tell which environment to use
    // true for sandbox
    // false for production
    'sandbox' => false,

    // RSA encryption strategy
    // here you can pass an array of RSAServiceContract implementations
    // by default are like this:
    'rsa_service' => [
        OpenSSL_RSAService::class,
        SeclibRSAService::class
    ],

    // custom PSR interfaces
    // here you can specify all the custom PSR interfaces that you want to user
    // by default each instance will be resolved from the dependencies
    'psr' => [
        ClientInterface::class => new \GuzzleHttp\Client([])
    ],

    // headers
    // here you can specify more details about your integration
    // to better help Satispay technician to investigate in your issues
    'headers' => [

        // the two OS_ headers have a default value set by reading your PHP installation
        SatispayHeaders::OS => 'your os',
        SatispayHeaders::OS_VERSION => 'your os versione',

        // the following headers are highly suggested
        SatispayHeaders::APP_SOFTWARE_HOUSE => 'your software house name',
        SatispayHeaders::APP_VERSION  => 'your app version',
        SatispayHeaders::APP_NAME  => 'your app name',

        SatispayHeaders::DEVICE_TYPE  => 'your device type (e.g. POS)',
    ]
]);

客户端认证

要认证您的应用程序,您需要使用Satispay提供的6位激活码。
您可以在此处了解更多信息: https://developers.satispay.com/docs/credentials

为了认证,您可以使用以下代码

use EmanueleCoppola\Satispay\SatispayGAgentClient;
use EmanueleCoppola\Satispay\SatispayGBusinessClient;

// for the authentication
// you could either instantiate a SatispayGBusinessClient instance
// or a SatispayGAgentClient instance based on your needs
$satispayGBusinessClient = new SatispayGBusinessClient([
    // this is optional, if you use the password in the first authentication
    // you must use it in every instance that you will create with the same credentials
    // 'passphrase' => 'my-passphrase',

    'sandbox' => true
]);

$satispayGBusinessClient->authentication->authenticate('QHPYXD');

$publicKey = $satispayGBusinessClient->authentication->publicKey;
$privateKey = $satispayGBusinessClient->authentication->privateKey;
$keyId = $satispayGBusinessClient->authentication->keyId;

if ($satispayGBusinessClient->authentication->ready()) {
    // store the $publicKey, $privateKey and the $keyId
}

// once done, just reuse them in every client instance to correctly authenticate to the Satispay APIs
// you can find an example below

$newSatispayGBusinessClient = new SatispayGBusinessClient([
    'public_key' => $publicKey,
    'private_key' => $privateKey,
    'key_id' => $keyId,

    // this is optional, if you use the password in the first authentication
    // you must use it in every instance that you will create with the same credentials
    // 'passphrase' => 'my-passphrase',

    'sandbox' => true
]);

SatispayGBusinessClient 付款

官方文档和代码示例

use EmanueleCoppola\Satispay\SatispayGBusinessClient;
use EmanueleCoppola\Satispay\SatispayHeaders;

$satispayGBusinessClient = new SatispayGBusinessClient([...]);

// Create payment
$satispayPayment = $satispayGBusinessClient->payments->create(
    [
        'flow' => 'MATCH_CODE',
        'currency' => 'EUR',
        'amount_unit' => 12.99 * 100, // 12,99€
    ],
    [
        // list of headers to be sent
        SatispayHeaders::IDEMPOTENCY_KEY => rand(10, 1000)
    ]
);

// Get payment
$satispayPayment = $satispayGBusinessClient->payments->get('7b5g32m5-3166-4c01-4617-edrb41558ce7');

// Get payment list
$satispayPayments = $satispayGBusinessClient->payments->all([
    'status' => 'ACCEPTED'
]);

// Update payment
$satispayPayment = $satispayGBusinessClient->payments->update(
    '7b5g32m5-3166-4c01-4617-edrb41558ce7',
    [
        'action' => 'ACCEPT',
    ]
);

SatispayGBusinessClient 预授权

官方文档和代码示例

use EmanueleCoppola\Satispay\SatispayGBusinessClient;

$satispayGBusinessClient = new SatispayGBusinessClient([...]);

// Create pre-authorization
$satispayPreAuthorization = $satispayGBusinessClient->preAuthorizations->create([
    'reason' => 'Monthly Payments',
    'callback_url' => 'https://myServer.com/myCallbackUrl?payment_id={uuid}',
    'redirect_url' => 'https://myServer.com/myRedirectUrl'
]);

// Get pre-authorization
$satispayPreAuthorization = $satispayGBusinessClient->preAuthorizations->get('9b89c251-6151-4561-93cc-c027f4d7f034');

// Update pre-authorization
$satispayPreAuthorization = $satispayGBusinessClient->preAuthorizations->update(
    '9b89c251-6151-4561-93cc-c027f4d7f034',
    [
        'status' => 'CANCELLED',
    ]
);

SatispayGBusinessClient 每日结算

官方文档和代码示例

use EmanueleCoppola\Satispay\SatispayGBusinessClient;

$satispayGBusinessClient = new SatispayGBusinessClient([...]);

// Get daily closure
$satispayConsumer = $satispayGBusinessClient->dailyClosures->get('20230119', ['generate_pdf' => true]);

SatispayGBusinessClient 消费者

官方文档和代码示例

use EmanueleCoppola\Satispay\SatispayGBusinessClient;

$satispayGBusinessClient = new SatispayGBusinessClient([...]);

// Get consumer
$satispayConsumer = $satispayGBusinessClient->consumers->get('+393337777888');

SatispayGBusinessClient 个人资料

官方文档和代码示例

use EmanueleCoppola\Satispay\SatispayGBusinessClient;

$satispayGBusinessClient = new SatispayGBusinessClient([...]);

// Get profile
$satispayProfile = $satispayGBusinessClient->profile->me();