PHP code example of erikwang2013 / stripe-php-erik
1. Go to this page and download the library: Download erikwang2013/stripe-php-erik library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
erikwang2013 / stripe-php-erik example snippets
use Erikwang2013\Stripe\StripeClient;
class PaymentController
{
public function charge(StripeClient $stripe)
{
$charge = $stripe->charges->create([
'amount' => 2000,
'currency' => 'usd',
'source' => 'tok_visa',
]);
return response()->json($charge);
}
}
use Erikwang2013\Stripe\Laravel\Facade\Stripe;
$customer = Stripe::customers()->create([
'email' => '[email protected]',
]);
use Erikwang2013\Stripe\ThinkPHP\StripeHelper;
class AppService extends Service
{
public function boot()
{
StripeHelper::init();
}
}
use Erikwang2013\Stripe\ThinkPHP\StripeHelper;
use Erikwang2013\Stripe\StripeClient;
// Via helper · 通过辅助类
$stripe = StripeHelper::client();
$customer = $stripe->customers->create([
'email' => '[email protected]',
]);
// Via container (when StripeService is registered) · 通过容器(StripeService 注册后)
$stripe = app('stripe');
// Or via DI · 或依赖注入
// public function __construct(StripeClient $stripe) { ... }
// Or directly · 或直接使用
$stripe = new StripeClient(config('stripe.api_key'));
// set up your tweaked Curl client · 配置自定义 Curl 客户端
$curl = new \Erikwang2013\Stripe\HttpClient\CurlClient();
$curl->setTimeout(10); // default is \Erikwang2013\Stripe\HttpClient\CurlClient::DEFAULT_TIMEOUT
$curl->setConnectTimeout(5); // default is \Erikwang2013\Stripe\HttpClient\CurlClient::DEFAULT_CONNECT_TIMEOUT
echo $curl->getTimeout(); // 10
echo $curl->getConnectTimeout(); // 5
// tell Stripe to use the tweaked client · 让 Stripe 使用自定义客户端
\Erikwang2013\Stripe\ApiRequestor::setHttpClient($curl);
// use the Stripe API client as you normally would · 正常使用即可
// set up your tweaked Curl client · 配置带代理的 Curl 客户端
$curl = new \Erikwang2013\Stripe\HttpClient\CurlClient([CURLOPT_PROXY => 'proxy.local:80']);
// tell Stripe to use the tweaked client · 让 Stripe 使用此客户端
\Erikwang2013\Stripe\ApiRequestor::setHttpClient($curl);
$stripe = new \Erikwang2013\Stripe\StripeClient('sk_test_xyz');
$response = $stripe->rawRequest('post', '/v1/beta_endpoint', [
"caveat": "emptor"
], [
"stripe_version" => "2022-11_15",
]);
// $response->body is a string, you can call $stripe->deserialize to get a \Erikwang2013\Stripe\StripeObject.
$obj = $stripe->deserialize($response->body);
// For GET requests, the params argument must be null, and you should write the query string explicitly.
$get_response = $stripe->rawRequest('get', '/v1/beta_endpoint?caveat=emptor', null, [
"stripe_version" => "2022-11_15",
]);