altitudeevents/swish-php

Swish api 的 PHP 包装器

dev-master 2020-11-29 14:47 UTC

This package is auto-updated.

Last update: 2024-09-29 06:14:23 UTC


README

Swish-PHP 是一个针对 Swish 商户 API 的小型包装器。更多详情请见 https://www.getswish.se/handel/

Guzzle 7 支持

这个分支增加了对 Guzzle 7 的支持。此外,它还支持新的 V2 PUT 端点,用于支付和退款请求。

支付请求支持

这个分支增加了对支付请求的支持。

依赖关系

  • php 5.5.9 或更高版本,需有 curl 和 openssl
  • composer

通过 git 安装

git clone https://github.com/altitudeevents/swish-php.git
composer install

通过 composer 安装

将此 git 添加为自定义存储库到您的 composer.json

{
  "require": {
    "helmutschneider/swish-php": "dev-master"
  },
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/altitudeevents/swish-php.git"
    }
  ]
}

现在您可以像往常一样执行 composer update

获取测试证书

Swish 文档(截至 2018-06-27): https://developer.getswish.se/content/uploads/2017/04/MerchantsAPI_Getswish_180517_v1.91.pdf
测试证书包(截至 2018-10-01)
https://developer.getswish.se/content/uploads/2018/10/Merchants_Test.zip

首先,获取 Swish 需要的 SSL 证书。Swish 服务器本身使用自签名根证书,因此需要 CA-bundle 来验证其来源。您还需要客户端证书及其对应的私钥,以便 Swish 服务器能够识别您。

截至 2018-06-27,测试证书被称为 Swish Merchant Test Certificate 1231181189.keySwish Merchant Test Certificate 1231181189.pemSwish TLS Root CA.pem您必须将 Swish Merchant Test Certificate 1231181189.keySwish Merchant Test Certificate 1231181189.pem 连接在一起,否则它们将无法与 cURL 一起使用。此包是您的客户端证书。

使用方法

客户端紧密模仿 swish api

class Client
{

    /**
     * @param PaymentRequest $request
     * @return string payment request id
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws ValidationException
     */
    public function createPaymentRequest(PaymentRequest $request): string;
    
    /**
     * @param string $id Payment request id
     * @return PaymentRequest
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getPaymentRequest(string $id): PaymentRequest;
    
    /**
     * @param Refund $refund
     * @return string refund id
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws ValidationException
     */
    public function createRefund(Refund $refund): string;
    
    /**
     * @param string $id Refund id
     * @return Refund
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getRefund(string $id): Refund;
}

当您准备好 SSL 证书时,您可以实例化客户端

use HelmutSchneider\Swish\Client;
use HelmutSchneider\Swish\PaymentRequest;

// Swish CA root cert
$rootCert = 'path/to/swish-root.crt'; // forwarded to guzzle's "verify" option

// .pem-bundle containing your client cert and it's corresponding private key. forwarded to guzzle's "cert" option
// you may use an empty string for "password" if you are using the test certificates.
$clientCert = ['path/to/client-cert.pem', 'password'];

$client = Client::make($rootCert, $clientCert);

$pr = new PaymentRequest([
    'callbackUrl' => 'https:///swish',
    'payeePaymentReference' => '12345',
    'payerAlias' => '4671234768',
    'payeeAlias' => '1231181189',
    'amount' => '100',
])

$id = $client->createPaymentRequest($pr);

var_dump($id);

//
//  string(32) "0D3AD8F1AE484A57B82A87FAB8C602EB"
//

OSX 的注意事项

OSX 10.12 及更早版本的捆绑 PHP 与上述转发 SSL 证书的方法不兼容。您必须获取一个编译有 cURL 链接 OpenSSL 或类似的 PHP 版本。

运行测试

要运行测试,您需要 Swish 测试服务器的证书。将根证书放在 tests/_data/root.pem 中,将客户端证书放在 tests/_data/client.pem 中。

vendor/bin/phpunit

Altitude Events

我们是一家旅游公司,主要在瑞典为青少年安排旅行。我们喜欢在老行业创造奇迹。如果您对与我们合作您的下一次企业之旅感兴趣,请联系 Tommie(tommie [at] altitudeevents.se)。