wucdbm / epay
epay.bg 的库
v1.0.2
2017-08-24 22:39 UTC
Requires
- php: ~5.5|~7.0
- symfony/options-resolver: ~2.8|~3.0
This package is auto-updated.
Last update: 2024-09-16 07:43:33 UTC
README
用法
- 创建一个
Wucdbm\Component\Epay\Client\ClientOptions
实例,该实例负责提供Wucdbm\Component\Epay\Client\Client
所需的所有选项。其构造函数需要 epay.bg 提供的商户 ID 和密钥。记得根据是否测试,将构造函数的第三个参数设置为 true 或 false。 - 创建一个实现了
Wucdbm\Component\Epay\Client\PaymentHandlerInterface
接口的处理器。该接口包含客户端调用的方法 - 创建一个
Wucdbm\Component\Epay\Client\Client
的实例,并传递上述两个 - 要创建支付表单,调用
$client->getEpayForm($id, $amount, $description, $expiry, $formId, $okUrl, $cancelUrl)
- 要创建 EasyPay 支付 ID,调用
$response = $this->client->getEasyPayIdn($id, $amount, $description, $expiry)
。这将返回一个Wucdbm\Component\Epay\Response\EasyPayResponse
响应。调用$response->getIdn()
获取 ID。请注意,此方法会抛出Wucdbm\Component\Epay\Exception\EasyPayGetIdnError
异常。
WucdbmEpayBundle https://packagist.org.cn/packages/wucdbm/epay-bundle 使用了此库,前往该链接查看更多示例。
$options = new \Wucdbm\Component\Epay\Client\ClientOptions($merchantId, $merchantSecret, $isDebug);
// $options->set... to alter any options
$handler = new \My\Project\Payments\Epay\PaymentHandler(LogManager $some, PaymentManager $dependencies);
$client = new \Wucdbm\Component\Epay\Client\Client($options, $handler);
获取支付表单的步骤如下
$uniqId = uniqid(); // this is to make autosubmitting easy - $('#formId').submit();
$expiry = new \DateTime('today + 2 days');
// ... other parameters
$form = $client->getEpayForm($id, $amount, $description, $expiry, $formId, $okUrl, $cancelUrl);
// Display the form and optionally auto submit it. Another option is to alter the submit button through the options and let the user do that
获取 EasyPay IDN
$id = $payment->getId();
$amount = $payment->getAmount();
$description = 'Payment #' . $payment->getId();
$expiry = $payment->getExpiryDate();
try {
$response = $this->client->getEasyPayIdn($id, $amount, $description, $expiry);
$idn = $response->getIdn();
// display idn to the user, do some logging.
// the response body is available at $response->getBody();
return $idn;
} catch (\Wucdbm\Component\Epay\Exception\EasyPayGetIdnError $ex) {
$this->logEasyPayRequest($payment, $ex->getBody(), true);
throw $ex;
}
接收支付
$response = $client->receiveResponse($post);
// the client will call your handler, which must deal with the payments received
// $response is an \Wucdbm\Component\Epay\Response\ReceiveResponseInterface instance
// exceptions are caught internally and transformed into responses, the no data exception and checksum mismatch exceptions in particular generate a global error response for epay
echo $response->toString();
exit();
// alternatively, if you use Symfony's HttpFoundation component
$response = \Symfony\Component\HttpFoundation\Response($response->toString());