granam/gpwebpay

GP WepPay支付网关,用于标准卡支付

5.1.0 2021-03-24 09:03 UTC

README

Build Status Test Coverage Latest Stable Version License

GPWebPay是一个PHP库,用于通过GPWebPay服务进行在线支付

如果您正在使用Nette框架,您可能希望使用Pixidos/GPWebPay Nette扩展。

快速入门

设置与使用

<?php
namespace Foo\Bar;

use Granam\GpWebPay\Settings;
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;

// SET UP
$settings = Settings::createForProduction(
    __DIR__ . '/foo/bar/your_private_key_downloaded_from_gp_web_pay.pem',
    'TopSecretPasswordForPrivateKey',
    __DIR__ . '/foo/bar/gp_web_pay_server_public_key_also_downloaded_from_gp_web_pay.pem',
    '123456789' // your 'merchant number' given to you by GP WebPay
    null // without explicit URL for response the current will be used - INCLUDING query string
);
$digestSigner = new DigestSigner($settings);

// RESPONSE
if (count($_POST) > 0) {
    try {
        $response = CardPayResponse::createFromArray($_POST, $settings, $digestSigner);
    } catch (GpWebPayErrorByCustomerResponse $gpWebPayErrorByCustomerResponse) {
        // some pretty error box for customer information about HIS mistake (for example "an 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 YOUR (developer) mistake (for example "a 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 an 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 (or just try again later, it happens) */
    }
    /**
     * it is OK, lets process $response->getParametersForDigest();
     * @see \Granam\GpWebPay\CardPayResponse::getParametersForDigest
     */
} else { // REQUEST
    $currencyCodes = new CurrencyCodes(new IsoCurrencies());
    try {
        $cardPayRequestValues = CardPayRequestValues::createFromArray($_POST, $currencyCodes);
        $cardPayRequest = new CardPayRequest($cardPayRequestValues, $settings, $digestSigner);
    } catch (GpWebPayException $exception) {
        /* show an apology to the customer
         * (for example "we are sorry, our payment gateway is temporarily unavailable") and log it, solve it */
        exit(); // exit in the middle of a script is not a good idea - this is just a fast-and-dirty example
    } ?>
    <html>
    <body>
        <!-- some pretty recapitulation of the order -->
        <form method="post" action="<?= $cardPayRequest->getRequestUrl() ?>">
            <?php foreach ($cardPayRequest as $name => $value) {
                ?><input type="hidden" name="<?= $name ?>" value="<?= $value ?>"
            <?php } ?>
            <input type="submit" value="Confirm order">
       </form>
    </body>
    </html>
<?php } ?>

故障排除

几乎所有可能的错误情况都通过许多异常进行了明确处理,但有些错误非常糟糕,无法涵盖

  • 在向GP WebPay发送请求后,您只看到一个标志,HTTP响应码是401
    • 可能您提供给GP WebPay的URL参数中的URL对GP WebPay来说不有效

对于测试测试支付网关,您可以使用支付卡

  • 卡号: 4056070000000008
  • 卡有效期: 12/2020
  • CVC2: 200
  • 3D Secure密码: password

安装

composer require granam/gpwebpay

致谢

此库源自Pixidos/GPWebPay,它具有相同的功能,但只能作为Nette框架扩展使用。所有荣誉都属于原作者Ondra Votava,来自Pixidos。

尽管如此,我对他公开共享那个库表示感谢。请有更多的人这样做!