helmutschneider / swish-php
Swish api 的 PHP 包装器
Requires
- php: >=7.3
- ext-curl: *
- ext-openssl: *
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpunit/phpunit: ^9.6
This package is not auto-updated.
Last update: 2024-09-15 12:29:24 UTC
README
Swish-PHP 是 Swish 商户 api 的小型包装器。更多信息请参阅 https://www.getswish.se/handel/。
依赖项
- php 7.3 或更高版本,支持 curl 和 openssl
- composer
升级说明
3.0.0
- 现在函数
createPaymentRequest
返回一个包含两个属性id
和paymentRequestToken
的对象。在早期版本中,id 直接返回。
通过 git 安装
git clone https://github.com/helmutschneider/swish-php.git composer install
通过 composer 安装
composer require helmutschneider/swish-php:^3.0
获取测试证书
截至 2022-05-29,可以通过访问以下链接找到测试证书包(mss_test_1.9.2.zip):https://developer.swish.nu/documentation/environments#certificates
首先获取 Swish 所需的 SSL 证书。Swish 服务器本身使用自签名的根证书,因此需要一个 CA-bundle 来验证其来源。您还需要一个客户端证书和相应的私钥,以便 Swish 服务器可以识别您。
截至 2022-05-29,测试证书的名称为 Swish_Merchant_TestCertificate_1234679304.key
、Swish Merchant Test Certificate 1231181189.pem
和 Swish_TLS_RootCA.pem
。**您必须将 Swish_Merchant_TestCertificate_1234679304.key
和 Swish Merchant Test Certificate 1231181189.pem
合并在一起,否则它们将无法与 cURL 一起工作。此包是您的客户端证书**。
用法
客户端紧密模仿 swish api
class Client { /** * @param PaymentRequest $request * @return CreatePaymentRequestResponse * @throws \GuzzleHttp\Exception\GuzzleException * @throws ValidationException */ public function createPaymentRequest(PaymentRequest $request): CreatePaymentRequestResponse; /** * @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']; // Create a client for the production environment. To connect to the test environment // instead, you must pass the constant Client::SWISH_TEST_URL as the third parameter. $client = Client::make($rootCert, $clientCert); $pr = new PaymentRequest([ 'callbackUrl' => 'https:///swish', 'payeePaymentReference' => '12345', 'payerAlias' => '4671234768', 'payeeAlias' => '1231181189', 'amount' => '100', ]) $res = $client->createPaymentRequest($pr); var_dump($res->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