简体   繁体   中英

Code example using PHP library to retrieve payments from stripe account

I am not sure how to include the PHP stripe api library into my php file in order to get a list of transactions from my stripe account. I have downloaded the lib source and extracted the contents to a folder into which I saved a file called getStripeTransList.php with the following code inside:-

The error I get is

Class Stripe not found

I have adjusted the code as follows:-

<?php

require "Stripe.php";
require "Charge.php";

Stripe\Stripe::setApiKey("sk_test_...");
$transList = Stripe\Charge::all(array("limit" => 3));
var_dump($transList);

The error I now get is 'Stripe\\ApiResource' not found in Charge.php. Charge.php looks like this:-

<?php

namespace Stripe;

class Charge extends ApiResource

{
    /**
     * @param string $id The ID of the charge to retrieve.

with class APIResource declared in APIResource.php. I get the feeling that I have not installed or configured the stripe PHP API library correctly with all these dependencies appearing? How should the library be installed. I try not to use Composer, but will if that is the only way.

You're not including the Stripe PHP bindings correctly.

If you're using Composer, you'd simply include Composer's autoload.php file:

require_once("vendor/autoload.php");

If you installed the library manually, you'd need to include the init.php file:

require_once("/path/to/stripe-php/init.php");

Once the library has been included, you will be able to list all charges like this:

$charges = \Stripe\Charge::all();
foreach ($charges->data as $charge) {
    // Do something with $charge
}

Note that all "list" API calls only return a finite number of resources. To retrieve the entire list, you might need to issue several API calls with pagination parameters to pick up where the previous call left off.

If you're using the latest versions of the bindings (3.9.0), you can also use the new auto-pagination feature:

$charges = \Stripe\Charge::all();
foreach ($charges->autoPagingIterator() as $charge) {
    // Do something with $charge
}

This will automatically iterate over all charges, querying new pages as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM