fredrik01 / swish-php
Swish api的PHP包装器
Requires
- php: >=5.5.9
- ext-curl: *
- ext-openssl: *
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- phpunit/phpunit: ^5.7
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-09-26 18:58:46 UTC
README
这是对 helmutschneider/swish-php 的分支,支持Docker。
Swish-PHP是为swish商户API创建的一个小型包装器。更多信息请访问 https://www.getswish.se/handel/。
依赖项
- PHP 5.5.9或更高版本
- curl
- openssl
开发依赖项
- 只需Docker和Docker Compose
安装
git clone https://github.com/Fredrik01/swish-php.git docker-compose run --rm swish composer install
获取测试证书
Swish文档(截至2018-06-27):https://developer.getswish.se/content/uploads/2017/04/MerchantsAPI_Getswish_180517_v1.91.pdf 测试证书包(截至2018-06-27):https://developer.getswish.se/content/uploads/2017/04/Merchants_Test-1.zip
首先,获取Swish所需的SSL证书。Swish服务器本身使用自签名的根证书,因此需要CA-bundle来验证其来源。您还需要一个客户端证书和相应的私钥,以便Swish服务器可以识别您。
截至2018-06-27,测试证书名称为 Swish Merchant Test Certificate 1231181189.key、Swish Merchant Test Certificate 1231181189.pem 和 Swish TLS Root CA.pem。**您必须将 Swish Merchant Test Certificate 1231181189.key 和 Swish 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\Util; // 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 中。
docker-compose run --rm swish ./extract.sh bundle.p12 swish swish
将生成的证书放在 tests/_data 中,并运行测试。
docker-compose run --rm swish composer install
docker-compose run --rm swish phpunit