dalholm/omnipay-klarna

Klarna Checkout 网关,用于 Omnipay 支付处理库

1.2.3 2023-12-21 12:54 UTC

This package is auto-updated.

Last update: 2024-09-21 14:46:18 UTC


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 存储库。

一般流程

  1. 创建 Klarna 订单
  2. 更新交易(如果需要)
  3. 渲染 Iframe
  4. 响应重定向到 checkoutUrlconfirmation_url
  5. 响应 结账回调
  6. 响应对 push_url 的请求(指示客户端已完成订单)并通过 承认
  7. 扩展授权(如果需要)
  8. 更新商家地址(如果需要)
  9. 执行一个或多个 捕获退款取消 操作

授权

要创建新订单,请使用 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 片段。

API 文档

渲染 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();

响应将包含更新后的订单数据。

API 文档

更新商家参考

在订单未被客户端授权(完成)之前,可以更新它

$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)

API 文档

扩展授权

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']
    );
}

API 文档

捕获

$success = $gateway->capture([
    'transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab',
    'amount' => '995',
])->send()
->isSuccessful();

API 文档

获取

初始时,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'] ?? [];

API 文档 | 结账 | 订单管理

确认

确认完成的订单

$success = $gateway->acknowledge(['transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab'])
    ->send()
    ->isSuccessful();

API 文档

退款

$success = $gateway->refund([
    'transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab',
    'amount' => '995',
])->send()
->isSuccessful();

API 文档

作废

您可以释放剩余的已授权金额。无法指定特定金额。

$success = $gateway->void(['transactionReference' => 'a5bec272-d68d-4df9-9fdd-8e35e51f92ab'])
    ->send()
    ->isSuccessful();

API 文档

更新客户地址

这可以在订单授权后更新客户地址详情时使用。此操作的执行成功将受 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();

API 文档

单位

Klarna 如下所述使用小数位表示金额 此处