如何在 CodeIgniter 4 中访问 Stripe

How to access Stripe in CodeIgniter 4

我正在尝试将 Stripe PHP API(通过 Composer 安装)加载到我的 CodeIgniter 4 应用程序中。这是我的文件的样子:

Composer.json

{
    "description": "The CodeIgniter framework",
    "name": "codeigniter/framework",
    "type": "project",
    "homepage": "https://codeigniter.com",
    "license": "MIT",
    "support": {
        "forum": "http://forum.codeigniter.com/",
        "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
        "slack": "https://codeigniterchat.slack.com",
        "source": "https://github.com/bcit-ci/CodeIgniter"
    },
    "require": {
        "php": ">=5.3.7",
        "stripe/stripe-php": "^6.7"
    },
    "suggest": {
        "paragonie/random_compat": "Provides better randomness in PHP 5.x"
    },
    "require-dev": {
        "mikey179/vfsStream": "1.1.*",
        "phpunit/phpunit": "4.* || 5.*"
    }
}

Autoload.php(只有重要的代码)

...
$classmap = ['Stripe' => '../../vendor/stripe/stripe-php/lib/Stripe.php'];
...

MyController.php

public function construct() {
    parent::__construct($request, $response, $logger = null);
    $this->db = \Config\Services::db();
    $this->stripe = Stripe();
}

运行 这段代码给我:调用未定义的函数 App\Controllers\Base\Stripe()。我很确定我已正确设置所有内容,只是没有从我的控制器正确调用 Stripe。我查看了 CodeIgniter 4 文档,但找不到任何帮助,而且由于 CI4 太新了,所以在线资料不多。非常感谢任何帮助!

您必须在控制器中包含 autoload.php。

include("path-to-autoload.php-in-vendor");

自动加载后,无需将其传递到变量中,只需使用即可。

所以 Stripe 可以通过

\Stripe\[function]

发件人:https://stripe.com/docs/charges 有额外评论

// set your API key
\Stripe\Stripe::setApiKey("sk_test_your_key");

// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];

// you set a variable when expecting a result/return
$charge = \Stripe\Charge::create([
   'amount' => 999,
   'currency' => 'usd',
   'description' => 'Example charge',
   'source' => $token,
]);