GithubHelp home page GithubHelp logo

lib-php's Introduction

Stancer PHP library

Requirement

We only support current PHP version, so this library needs at least PHP 8.1.

Installation

To keep it simple, we uses Composer. You only need to run the following commmand :

composer require stancer/stancer

The library uses cURL internaly to connect to the API. You need the php extension phpX.Y-curl, where X.Y correspond to your PHP version, to be installed on your server.

You may also use Guzzle or every PSR7 compatible client to replace the internal cURL instance. You only need to provider the client instance to the API config.

<?php

$config = Stancer\Config::init($key);
$config->setHttpClient($guzzle);

Usage

All you need is a valid API key.

<?php

$key = 'my_api_key_kSp7hBH3hyDQ36izsyKR';

Stancer\Config::init($key);

$card = new Stancer\Card();
$card->setNumber('4111111111111111');
$card->setExpMonth(12);
$card->setExpYear(2022);
$card->setCvc('999');
$card->setName('John Doe');

$payment = new Stancer\Payment();
$payment->setCard($card);
$payment->setAmount(1000); // You must put an integer, here we wanted USD$10.00
$payment->setCurrency('USD');
$payment->description('My first payment');
$payment->send();

Retrieve an object

To retrieve an object, just put an identifier when creating a new instance.

<?php

$payment = new Stancer\Payment('paym_KIVaaHi7G8QAYMQpQOYBrUQE');

Creating an object

To create an object, like a payment, use the send method.

<?php

$payment = new Stancer\Payment();

$payment->setCard($card); // Pay with a card
// or
$payment->setSepa($sepa); // Pay with a bank account (SEPA, uses BIC and IBAN)

// Do not forget to complete your payment informations

$payment->send();

Refund a payment

The easiest way to do it, use Payment::refund() method. There is a simple example :

<?php

$payment = new Stancer\Payment('paym_KIVaaHi7G8QAYMQpQOYBrUQE');

$payment->refund(); // To refund the full payment
// or
$payment->refund($amount); // To refund a particular amount


$payment->getRefunds(); // Will return all refunds made on a payment

Exceptions

Every exceptions thrown by this project extend from Stancer\Exceptions\Exception.

Some exceptions are related to the connection with the API :

  • Stancer\Exceptions\TooManyRedirectsException on 310 HTTP errors
  • Stancer\Exceptions\BadRequestException on 400 HTTP errors
  • Stancer\Exceptions\NotAuthorizedException on 401 HTTP errors (basically bad credential)
  • Stancer\Exceptions\NotFoundException on 404 HTTP errors
  • Stancer\Exceptions\ConflictException on 409 HTTP errors

Or with less specificity :

  • Stancer\Exceptions\RedirectionException on 3** HTTP errors (technically only Too many redirection)
  • Stancer\Exceptions\ClientException on 4** HTTP errors
  • Stancer\Exceptions\ServerException on 5** HTTP errors

You can see other exceptions, not related to HTTP traffic :

  • Stancer\Exceptions\BadMethodCallException when you are calling an unknown method
  • Stancer\Exceptions\InvalidArgumentException when you are using a method without the right arguments

For these two exceptions, you need to check you code, they should never appear in real world.

To see every available exceptions, simply take a look in src/Exceptions folder.

Logger

You can add a PSR3 compatible logger.

We suggeset monolog but anything implementing log interfaces can be use.

<?php
$log = new Monolog\Logger('Stancer');
$log->pushHandler(new Monolog\Handler\StreamHandler('path/to/your.log', Monolog\Logger::INFO));

$config->setLogger($logger);

Security

  • Never, never, NEVER register a card or a bank account number in your database.

  • Always uses HTTPS in card/SEPA in communication.

  • Our API will never give you a complete card/SEPA number, only the last four digits. If you need to keep track, use these last four digit.

Contribute

We can contribute and modify everything, you just need to follow some rules :

  • Fork the project
  • Make a topic branch (aka one branch for one feature or one bug)
  • Test locally
  • Complete the Unreleased part of the changelog
  • Make a merge/pull request

Every MR/PR MUST have unit test and CHANGELOG entries to be approved.

Our unit testing framework is atoum. It is fast and easy to learn. Go see the documentation.

lib-php's People

Contributors

jdslv avatar jgaychet-stancer avatar aeris avatar cqoicebordel avatar

Stargazers

Camille Bouvat avatar Pierre avatar Eytan avatar Aurélien Pohu avatar  avatar Gabriel Bugeaud avatar Arnaud Ligny avatar Matthieu Vion avatar  avatar

lib-php's Issues

Laravel 10 et psr/http-message && psr/log

Bonjour,
Possible de rendre compatible avec
"psr/http-message": "^2.0",
"psr/log": "^3.0"
Car impossible de l'installer simplement avec composer lorsqu'on utilise Laravel 10.

add api "hello" "ping" or something else

Hello,
just for step by step API i'm looking for a entry point to something like "hello world" or "welcome onboard" just to check if auth is ok.

Here is part of doc:

<?php
$config = Stancer\Config::init(['<your_public_keys>', '<your_secret_keys>']);

$config->setMode(Stancer\Config::TEST_MODE); # Not needed, TEST mode is set by default
                                           # But you need to do it to go on production ;)
?>

ok but after that, how could i check if there is no auth problem ? maybe a request to get list of my "customers" ? -> no ...

Little misspelling in readme.md

Hello Stancer team,

You should :

  • modify $payment->setAmmount(1000) with $payment->setAmount(1000),
  • and update $payment->save() with $payment->send()

Have a nice day :-) !

getCustomer exception ? could be better if returns null (i think)

Il some race conditions there is a problem with php lib, for example when stancer server returns that sort of data:

    {
      "amount": 100,
      "auth": null,
      "capture": true,
      "country": "FR",
      "created": 1681876445,
      "currency": "eur",
      "date_bank": 1681855200,
      "date_paym": 1681768800,
      "description": null,
      "fee": 2,
      "id": "paym_sqxxxxxxxxxxxxxxxxxxx",
      "method": "card",
      "order_id": null,
      "response": "00",
      "status": "captured",
      "unique_id": "xxxxxxxxxxx"
    },

Then, if i call $payment->getCustomer() result is "Stancer\Exceptions\Exception exception occurs : Missing card details" but the documentation says that function will return Stancer\Customer or null

api to get / search user by email

how could a get a user if i don't store (or loose) the customer_id ?

here is the exception when i try to re-create the same customer:

"Customer already exists, you may want to update it instead creating a new one (cust_xxxxx)"

do i have to parse that string to get cust_id ?

payout again ?

Hello
i'm sorry but payment.php seems to be not clear, why is there disputes,payments,refunds into hydrate function ?

    public function hydrate(array $data): self
    {
        $data['details'] = [];
        $items = [
            'disputes',
            'payments',
            'refunds',
        ];

        foreach ($items as $item) {
            if (array_key_exists($item, $data)) {
                $data['details'][$item] = $data[$item];
                $data['details'][$item]['currency'] = $data['currency'];
                $data['details'][$item]['parent'] = $this;
                $data['details'][$item]['type'] = $item;
            }
        }

        return parent::hydrate($data);
    }

Installation impossible

Bonjour,
composer require Stancer/lib-php
renvoie :

In PackageDiscoveryTrait.php line 364:
                                                                                                                                                                          
  Could not find a matching version of package Stancer/lib-php. Check the package spelling, your version constraint and that the package is available in a stability whi  
  ch matches your minimum-stability (stable).

Idem avec une minimum-stability définie en tant que "dev".

A quand une librairy javascript

J'utilise Node en back end. pas de perl, pas de php, pas de python.
Avez vous prévu de pouvoir interagir an javascript ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.