myonlinestore / omnipay-klarna-checkout
Klarna Checkout网关用于Omnipay支付处理库
Requires
- php: ^7.4 | ^8.0
- ext-json: *
- omnipay/common: ^3.1
Requires (Dev)
- myonlinestore/coding-standard: ^3.1
- myonlinestore/php-devtools: ^0.2
- omnipay/tests: ^4.1
- vimeo/psalm: ^4.18
- dev-master
- 5.0
- 4.0
- 3.1
- 3.0.12
- v3.0.11
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.1
- v2.0.0
- v1.0.31
- v1.0.30
- v1.0.29
- v1.0.28
- v1.0.27
- v1.0.26
- v1.0.25
- v1.0.24
- v1.0.23
- v1.0.22
- v1.0.21
- v1.0.20
- v1.0.19
- v1.0.18
- v1.0.17
- v1.0.16
- v1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
- dev-v2-authentication
- dev-release-2.0
- dev-purchase_country-to-string
This package is auto-updated.
Last update: 2024-03-01 13:01:17 UTC
README
此仓库不再维护。
Omnipay: Klarna Checkout
简介
Omnipay 是一个与框架无关的、多网关支付处理库,适用于 PHP 5.6+。本包实现了 Omnipay 的 Klarna Checkout 支持。
安装
要安装,只需将其添加到您的 composer.json
文件中
$ composer require myonlinestore/omnipay-klarna-checkout
初始化
首先,创建 Omnipay 网关
$gateway = Omnipay\Omnipay::create('\MyOnlineStore\Omnipay\KlarnaCheckout\Gateway'); // or $gateway = new MyOnlineStore\Omnipay\KlarnaCheckout\Gateway(/* $httpClient, $httpRequest */);
然后,使用正确的凭据初始化它
$gateway->initialize([ 'username' => $username, 'secret' => $secret, 'api_region' => $region, // Optional, may be Gateway::API_VERSION_EUROPE (default) or Gateway::API_VERSION_NORTH_AMERICA 'testMode' => false // Optional, default: true ]); // or $gateway->setUsername($username); $gateway->setSecret($secret); $gateway->setApiRegion($region);
用法
有关一般用法说明,请参阅主要的 Omnipay 仓库。
一般流程
- 创建 Klarna 订单
- 更新交易(如有必要)
- 渲染 Iframe
- 响应重定向到
checkoutUrl
或confirmation_url
- 响应 结账回调
- 响应对
push_url
的请求(表明客户已完成订单)并返回一个 确认 - 扩展授权(如有必要)
- 更新商家地址(如有必要)
- 执行一个或多个 捕获、退款 或 取消 操作
授权
要创建新订单,请使用 authorize
方法
$data = [ 'amount' => 100, 'tax_amount' => 20, 'currency' => 'SEK', 'locale' => 'SE', 'purchase_country' => 'SE', 'notify_url' => '', // https://developers.klarna.com/api/#checkout-api__ordermerchant_urls__validation 'return_url' => '', // https://developers.klarna.com/api/#checkout-api__ordermerchant_urls__checkout 'terms_url' => '', // https://developers.klarna.com/api/#checkout-api__ordermerchant_urls__terms 'validation_url' => '', // https://developers.klarna.com/api/#checkout-api__ordermerchant_urls__validation 'items' => [ [ 'type' => 'physical', 'name' => 'Shirt', 'quantity' => 1, 'tax_rate' => 25, 'price' => 100, 'unit_price' => 100, 'total_tax_amount' => 20, ], ], ]; $response = $gateway->authorize($data)->send()->getData();
这将返回订单详情以及要在您的网站上渲染的结账 HTML 片段。
渲染 Iframe
Klarna Checkout 在授权支付时需要渲染 iframe
$response = $gateway->fetchTransaction(['transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab']) ->send(); echo $response->getData()['checkout']['html_snippet'];
在 iframe 内提交表单后,Klarna 将将客户端重定向到提供的 confirmation_url
(成功)或 checkout_url
(失败)。
更新交易
在客户未授权(完成)订单之前,可以更新订单
$response = $gateway->updateTransaction([ 'transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab', 'amount' => 200, 'tax_amount' => 40, 'currency' => 'SEK', 'locale' => 'SE', 'purchase_country' => 'SE', 'items' => [/*...*/], ])->send();
响应将包含更新的订单数据。
扩展授权
Klarna 订单授权有效至特定日期,并且可以延长(最长可达 180 天)。可以使用 fetch 请求检索更新的过期日期。
if ($gateway->extendAuthorization(['transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab'])->send()->isSuccessful()) { $expiration = new \DateTimeImmutable( $gateway->fetchTransaction(['transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab']) ->send() ->getData()['management']['expires_at'] ); }
捕获
$success = $gateway->capture([ 'transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab', 'amount' => '995', ])->send() ->isSuccessful();
获取
一个 Klarna 订单最初可通过结账 API 获取。在它被授权后,它将通过订单管理 API(一段时间后,将不再在结账 API 中可用)获取。此获取请求将首先检查订单是否存在于结账 API 中。如果不存在,或者状态指示订单已完成,它还将从订单管理 API 中获取订单。
$response = $gateway->fetchTransaction(['transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab']) ->send(); $success = $response->isSuccessful(); $checkoutData = $response->getData()['checkout'] ?? []; $managementData = $response->getData()['management'] ?? [];
确认
确认完成的订单
$success = $gateway->acknowledge(['transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab']) ->send() ->isSuccessful();
退款
$success = $gateway->refund([ 'transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab', 'amount' => '995', ])->send() ->isSuccessful();
作废
您可以释放剩余的授权金额。指定特定金额是不可能的。
$success = $gateway->void(['transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab']) ->send() ->isSuccessful();
更新客户地址
这可以在订单授权后更新客户地址详情时使用。此操作的成功将受 Klarna 的风险评估。两个地址都是必需的参数。
$success = $gateway->refund([ 'transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab', 'shipping_address' => [ 'given_name'=> 'Klara', 'family_name'=> 'Joyce', 'title'=> 'Mrs', 'street_address'=> 'Apartment 10', 'street_address2'=> '1 Safeway', 'postal_code'=> '12345', 'city'=> 'Knoxville', 'region'=> 'TN', 'country'=> 'us', 'email'=> 'klara.joyce@klarna.com', 'phone'=> '1-555-555-5555' ], 'billing_address' => [ 'given_name'=> 'Klara', 'family_name'=> 'Joyce', 'title'=> 'Mrs', 'street_address'=> 'Apartment 10', 'street_address2'=> '1 Safeway', 'postal_code'=> '12345', 'city'=> 'Knoxville', 'region'=> 'TN', 'country'=> 'us', 'email'=> 'klara.joyce@klarna.com', 'phone'=> '1-555-555-5555' ], ])->send() ->isSuccessful();
更新商户参考
如果订单已被客户授权,其商户参考可以更新
$response = $gateway->updateMerchantReferences([ 'merchant_reference1' => 'foo', 'merchant_reference2' => 'bar', ])->send();
单位
Klarna 以如此处所述的小单位表示金额。