steffenz / php-nets-sdk
非官方的PHP SDK,用于轻松与Nets(北欧支付提供商)API交互。
dev-master
2019-05-23 12:08 UTC
Requires
- php: >5.3.0
- guzzlehttp/guzzle: ^6.3@dev
This package is auto-updated.
Last update: 2024-09-24 00:09:35 UTC
README
这是一个超级非官方(但官方上很棒)的SDK,让支付变得像从婴儿那里偷糖果一样简单。坦白说,我们都是想要糖果的孩子 🍭
本计划是将其作为composer包发布,所以请密切关注这一进展 🚚
有什么问题,或者不符合预期的工作?请随时贡献并提交PR 🥰
入门
创建新交易
在您能捕获一些珍贵的金钱之前,您需要创建并注册一个交易。在此示例中,我们不会涵盖错误处理,但您应该用try/catch块包裹此代码(有很多事情可能出错)。
use NetsSdk\Merchant; use NetsSdk\Currencies; use NetsSdk\Price; use NetsSdk\Request; use NetsSdk\Transaction; /* Create a new merchant object with your credentials. */ $merchant = new Merchant(); $merchant->setMerchantId("") ->setAccessToken(""); /* Create a new price object with your desired currency. */ $price = new Price(); $price->setAmount(13.37) ->setCurrency(Currencies::NorwegianKrone) /* Create new request object */ $request = new Request(); $request->setOrderNumber(1337) ->setPrice($price) ->setCustomerFirstName("Nitrus") ->setCustomerLastName("Brio") ->setCustomerEmail("nitrus.cheezedoodles91@hotmail.com") ->setOrderDescription("Equipment for cortex vortex.") ->setRedirectUrl("https:///cash4life"); /* Create and register new transaction */ $transaction = new Transaction(); $transaction->setMerchant($merchant) ->setRequest($requestObj) ->setIsTestEnvironment(true) // Uses Nets test enviroment. ->register(); // Posts to Nets API. /* Retrives the transaction ID */ $transactionId = $transaction->getTransactionId(); /* URL to terminal */ $terminalUrl = $transaction->getTerminalUrl();
授权和捕获支付
最激动人心的部分。在上一个步骤中,我们只是购买了门票;现在是我们抓取一些现金(或者至少一些比特和字节)的时候了。我们假设您“丢失”了上一个块中的对象,并在需要时从API中简单查询它。
同样,您应该用try/catch包裹它。我们双重检查交易是否已授权和/或捕获,以避免不断向API发送请求,这反过来会记录这些尝试。
use NetsSdk\Merchant; use NetsSdk\Transaction; /* Passing to the constructor, just to make it less readable */ $merchant = new Merchant("merchantId", "accessToken"); /* We don't need a request this time */ $request = false; /* Still testing, aren't we? */ $isTestEnvironment = true; /* Creating a new transaction with a known ID */ $transaction = new Transaction($merchant, false, $transactionId, $isTestEnvironment); $isAuthorized = $transaction->isAuthorized(); /* Authorize transaction if it hasn't been done already */ if(!$transaction->isAuthorized()){ $transaction->authorize(); } /* If nothing is captured - we'll capture too */ if($transaction->getCapturedAmount() < 1){ $transaction->capture(); } /* Fetch the full transaction object */ $transaction = $transaction->query();
有用的资源
https://www.youtube.com/watch?v=ewRjZoRtu0Y&t=56
https://blog.jgrossi.com/2013/creating-your-first-composer-packagist-package/