rapidwebltd/simplestripe

SimpleStripe 使得将 Stripe 驱动的支付集成到网站中变得前所未有的简单。只需少量代码,你就可以准备一个支付表单,开始向客户收费。

v1.0.6 2018-01-30 19:36 UTC

This package is auto-updated.

Last update: 2024-09-06 09:31:59 UTC


README

这个超级简单的 Stripe 集成包允许你通过极少的代码将 Stripe 驱动的支付表单集成到网站中,并开始向客户收费。

安装与依赖

可以使用 composer 安装此包及其依赖项。

只需将包添加到 composer.json 文件中,如下所示,然后运行 composer update

{
  "require": {
       "rapidwebltd/simplestripe": "1.*"
   }
}

如果你的框架没有为你这样做,请记住包含由 composer 生成的自动加载文件,如下所示。

require_once 'vendor/autoload.php';

设置

要使用 SimpleStripe,你必须首先实例化 SimpleStripe 对象。为此,你需要 Stripe API 密钥以及你希望接受的支付货币。

// Setup SimpleStripe using Stripe API keys and currency 
$simpleStripe = \RapidWeb\SimpleStripe\Factories\SimpleStripeFactory::create('PUBLISHABLE_KEY', 'SECRET_KEY', 'GBP');

你可以在 https://dashboard.stripe.com/account/apikeys 找到你的 Stripe API 密钥。你需要秘密密钥和可发布密钥。

货币必须以 ISO 4217 格式呈现,例如 GBP、USD、EUR。

显示支付表单

以下代码将显示一个简单的支付表单,适用于所有常见的借记卡和信用卡支付。

// Display a simple payment form
echo $simpleStripe->paymentForm();

此代码还将包含处理客户端与 Stripe 通信和显示验证错误的必要 JavaScript 代码。表单将回传到显示它的同一 URL。

向客户收费

向客户收费很简单。以下代码示例展示了你可以如何

  1. 处理支付表单的回传
  2. 尝试向客户收费
  3. 处理成功或失败
// If payment form has been submitted
if (isset($_POST['stripeToken'])) {

    // Get the amount to charge (in the currency's lowest denomination)
    $amount = 500; // Five hundred pence = Five pounds (5 GBP)

    // Charge the customer
    $charge = $simpleStripe->charge($amount, $_POST['stripeToken']);

    if ($charge->succeeded) {
        
        // If charge succeeded, display success messsage, or perhaps redirect the user to a success page
        echo "Success!";
        exit;

    } elseif ($charge->problemType=='Card') {

        // If there was a problem with the card, display details of the problem
        echo $charge->problem;

    } else {

        // Else, display a generic failure message
        echo "Sorry, there was a problem processing your payment.";
    }
  
}

你应该在包含你的支付表单的页面顶部包含类似此处的代码。

示例

关于 SimpleStripe 的完整实现,请参阅 src/Example.php

许可

此库受 GNU 通用公共许可证版本 3 许可。