apility / nets-easy
Nets Easy 库
v2.3.0
2022-11-24 16:50 UTC
Requires
- php: ^7.3|^7.4|^8.0
- illuminate/support: >=5.5 <9.0
- nesbot/carbon: >=1.26 <3.0
Requires (Dev)
- vlucas/phpdotenv: >=4.1 <5
This package is auto-updated.
Last update: 2024-09-24 20:45:16 UTC
README
NETS Easy PHP 库提供从 PHP 编写的应用程序中方便地访问 NETS Easy API。它包含一组预定义的类,用于初始化 API 资源,这些类会从 API 响应中动态初始化。
要求
PHP 7.3.0 及更高版本。
Composer
您可以通过 Composer 安装绑定。运行以下命令
composer require apility/nets-easy
要使用绑定,请使用 Composer 的 自动加载
require_once('vendor/autoload.php');
入门
简单用法如下
后端集成
<?php use Nets\Easy; use Nets\Easy\Payment; Easy::setup([ 'secret_key' => '00000000000000000000000000000000', 'checkout_key' => '00000000000000000000000000000000', 'merchant_id' => '000000000' ]); $payment = Payment::create([ 'checkout' => [ 'url' => 'https://domain.tld/checkout', // The exact URL where your checkout is hosted (except query parameters and fragments) 'termsUrl' => 'https://domain.tld/terms', // Your terms ], 'order' => [ 'currency' => 'NOK', 'reference' => '1', // A unique reference for this specific order 'amount' => 10000, // Total order amount in cents 'items' => [ [ 'reference' => '1', // A unique reference for this specific item 'name' => 'Test', 'quantity' => 1, 'unit' => 'pcs', 'unitPrice' => 8000, // Price per unit except tax in cents 'taxRate' => 25000 // Taxrate (e.g 25.0 in this case), 'taxAmount' => 2000 // The total tax amount for this item in cents, 'grossTotalAmount' => 10000 // Total for this item with tax in cents, 'netTotalAmount' => 8000 // Total for this item without tax in cents ] ] ] ]);
前端集成
在前端集成结账表单的最简单方法是使用 Payment 对象上的 form
辅助方法。
<?php use Nets\Easy; use Nets\Easy\Payment; Easy::setCredentials(...); $payment = isset($_GET['paymentId'] ? Payment::retrieve($_GET['paymentId']) : Payment::create(...); $form = $payment->form([ 'redirect' => 'https://domain.tld/checkout/complete', // URL to redirect to on payment completion (paymentId query parameter is appended) 'theme' => [ // Optional 'textColor' => 'blue', 'linkColor' => '#bada55', 'buttonRadius' => '50px' ] ]); ?> <div id="easy-complete-checkout"></div> <?php echo $form; ?>
或者,如果您需要完全控制结账流程,可以手动实现它
<html> <head> <title>Example checkout</title> </head> <body> <div id="easy-complete-checkout"></div> <script> document.addEventListener('DOMContentLoaded', () => { const checkout = new Dibs.Checkout({ checkoutKey: "00000000000000000000000000000000", // Checkout Key paymentId: "00000000000000000000000000000000", // Payment ID created in the backend integration partnerMerchantNumber: "000000000", // Merchant ID containerId: "easy-complete-checkout", // Container element where the checkout form should be created }) checkout.on('payment-completed', ({ paymentId }) => { // Payment is completed, do what you need to do here }) }) </script> <script src="https://test.checkout.dibspayment.eu/v1/checkout.js?v=1"></script> <!-- or if in production: --> <script src="https://checkout.dibspayment.eu/v1/checkout.js?v=1"></script> </body> </html>
文档
请参阅 官方 API 文档。