pigmentab/omnipay-klarna

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

1.2.1 2022-08-05 12:55 UTC

This package is auto-updated.

Last update: 2024-09-05 17:32:21 UTC


README

本软件包是omnipay-klarna-checkout的修改版本,由nyehandelIntflow提供。使用本软件包,您可以在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. 响应checkout回调
  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天)。然后可以通过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']
    );
}

API文档

捕获

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

API文档

获取

克雷纳订单最初通过结账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文档

更新客户地址

这可能用于在订单授权后更新客户地址详情 之后。此操作的执行成功取决于克雷纳的风险评估。两个地址都是必需的参数。

$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文档

单位

克雷纳在这里描述了以小单位表达金额 此处