gnurlan / gpwebpay
GP WepPay支付网关,用于标准卡支付
5.0.1
2020-08-27 15:42 UTC
Requires
- php: >=7.1
- ext-intl: *
- alcohol/iso4217: ^3.1
- granam/float: >=5.0
- granam/integer: >=6.1
- granam/strict-object: >=3.0
- granam/string: ^3.0
- phpgt/dom: ^2.0
Requires (Dev)
- ext-curl: *
- granam/exceptions-hierarchy: >=4.0
- granam/tools: >=3.0
- mockery/mockery: ~1.0
- phpunit/phpunit: ~7.0
- roave/security-advisories: dev-master
- symfony/yaml: ^3.2
Suggests
- granam/gpwebpay-flat: Let's check accepted payments via GPWebPay report on daily basis
This package is auto-updated.
Last update: 2024-09-29 00:18:46 UTC
README
GPWebPay是一个PHP库,通过GPWebPay服务进行在线支付
如果您使用的是Nette框架,您可能希望使用Pixidos/GPWebPay Nette扩展。
快速入门
设置与使用
<?php namespace App\Http\Controllers; use Auth; use App; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Granam\GpWebPay\Settings as GpSettings; use Granam\GpWebPay\DigestSigner; use Granam\GpWebPay\CardPayResponse; use Granam\GpWebPay\Codes\CurrencyCodes; use Alcohol\ISO4217 as IsoCurrencies; use Granam\GpWebPay\CardPayRequestValues; use Granam\GpWebPay\CardPayRequest; use Granam\GpWebPay\Exceptions\GpWebPayErrorByCustomerResponse; use Granam\GpWebPay\Exceptions\GpWebPayErrorResponse; use Granam\GpWebPay\Exceptions\ResponseDigestCanNotBeVerified; use Granam\GpWebPay\Exceptions\Exception as GpWebPayException; class PayController extends Controller { const MERCHANT_NUMBER = '123456789'; // your 'merchant number' given to you by GP WebPay const GP_PRIVATE_KEY = 'your_private_key_downloaded_from_gp_web_pay.pem'; const GP_PRIVATE_KEY_PASS = 'TopSecretPasswordForPrivateKey'; const GP_PUBLIC_KEY = 'gp_web_pay_server_public_key_also_downloaded_from_gp_web_pay.pem'; protected $digestSigner; protected $settings; public function __construct() { $this->settings = GpSettings::createForProduction( storage_path(self::GP_PRIVATE_KEY), self::GP_PRIVATE_KEY_PASS, storage_path(self::GP_PUBLIC_KEY), self::MERCHANT_NUMBER // without explicit URL for response the current will be used - INCLUDING query string ); $this->digestSigner = new DigestSigner($this->settings); } public function request() { $currencyCodes = new CurrencyCodes(new IsoCurrencies()); try { $cardPayRequestValues = CardPayRequestValues::createFromArray($_GET, $currencyCodes); $cardPayRequest = new CardPayRequest($cardPayRequestValues, $this->settings, $this->digestSigner); } catch (GpWebPayException $exception) { /* show an apology to the customer * like "we are sorry, our payment gateway is temporarily unavailable" and log it, solve it */ exit(); } print '<html><body><form method="post" action="' . $cardPayRequest->getRequestUrl() . '">'; foreach ($cardPayRequest as $name => $value) { print '<input type="hidden" name="' . $name . '" value="' . $value . '">'; } print '<input type="submit" value="Confirm order"></form></body></html>'; } public function response() { try { $response = CardPayResponse::createFromArray($_POST, $this->settings, $this->digestSigner); } catch(GpWebPayErrorByCustomerResponse $gpWebPayErrorByCustomerResponse) { // some pretty error box for customer information about HIS mistake like invalid card number /** * WARNING: do not rely blindly on this detection - for example if YOU (developer) are sending * card number in a hidden field, because the customer provided it to its account before and * does not need to enter it again, but the card number has been refused by GP WebPay, * you will show to the customer confusing message about an invalid card number, * although he does not enter it. * For full list of auto-detected customer * mistakes @see GpWebPayErrorByCustomerResponse::isErrorCausedByCustomer */ echo $gpWebPayErrorByCustomerResponse->getLocalizedMessage(); } catch(GpWebPayErrorResponse $gpWebPayErrorResponse) { /* GP WebPay refuses request by OUR (developer) mistake like duplicate order number * - show an apology to the customer and log this, solve this */ } catch(ResponseDigestCanNotBeVerified $responseDigestCanNotBeVerified) { /* values in response have been changed(!), * show an apology (or a warning?) to the customer and probably log this for evidence */ } catch(GpWebPayException $gpWebPayException) { // EVERY exception share this interface /* some generic error like processing error on GP WebPay server, * show an apology to the customer and log this, solve this */ } /** * its OK, lets process $response->getParametersForDigest(); * @see \Granam\GpWebPay\CardPayResponse::getParametersForDigest */ } }
故障排除
几乎所有可能的错误情况都被许多异常清楚地覆盖,但有些太复杂,不能被覆盖
- 在向GP WebPay发送请求后,您只看到一个标志,HTTP响应代码为401
- 可能是您提供给GP WebPay的URL参数中的URL在GP WebPay看来无效
- 请确保URL存在且没有任何重定向,例如从https://www.github.com到https://github.com/(不要相信浏览器地址栏中的内容,末尾的反斜杠经常被隐藏)
- 可能是您提供给GP WebPay的URL参数中的URL在GP WebPay看来无效
对于测试测试支付网关,您可以使用支付卡
- 卡号:
4056070000000008
- 有效期:
12/2020
- CVC2:
200
- 3D Secure密码:
password
安装
composer require gnurlan/gpwebpay
(需要PHP 7.0+)
致谢
这个库源自Pixidos/GPWebPay库,该库具有相同的功能,但只能作为Nette框架扩展使用。所有荣誉都属于Pixidos的作者Ondra Votava。
尽管如此,我非常感谢他公开分享这个库。请有更多这样的人。