dalholm / omnipay-klarna
Klarna Checkout 网关,用于 Omnipay 支付处理库
Requires
- omnipay/common: ^2.0||^3.0
Requires (Dev)
- omnipay/tests: ^2.0||^3.0
README
此包是 nyehandel 和 Intflow 修改的 omnipay-klarna-checkout 版本的包。使用此包,您可以在 Laravel 8 和 PHP 8 上运行。此包还将包含比原始包更多的功能。
简介
Omnipay 是一个针对 PHP 5.6+ 的无框架、多网关支付处理库。此包实现了 Omnipay 的 Klarna Checkout 支持。
此包支持 PHP 8
安装
要安装,只需将其添加到您的 composer.json 文件中
$ composer require dalholm/omnipay-klarna
初始化
首先,创建 Omnipay 网关
$gateway = Omnipay\Omnipay::create('\Dalholm\Omnipay\KlarnaCheckout\Gateway'); // or $gateway = new Dalholm\Omnipay\Klarna\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' => 2500, 'price' => 1000, 'unit_price' => 1000, 'total_tax_amount' => 200, ], ], ]; $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();
响应将包含更新后的订单数据。
更新商家参考
在订单未被客户端授权(完成)之前,可以更新它
$response = $gateway->updateMerchantReferences([ 'transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab', 'merchant_reference1' => (String) 'Your reference or order id', 'merchant_reference2' => (String) 'Another reference or order id', ])->send();
响应将是一个空的响应(204)
扩展授权
Klarna 订单授权有效至特定日期,并且可以延长(最长可达 180 天)。然后可以通过 获取 请求检索更新的到期日期
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();
单位
Klarna 如下所述使用小数位表示金额 此处。