Giter VIP home page Giter VIP logo

alma-php-client's Introduction

Alma PHP API client

This is the official PHP API client for Alma.

This PHP API Client is being used in production on thousands of e-commerce websites and provides the necessary endpoints to build a full-fledge integration. It does not, however, implement the full Alma API as documented here yet. If you find yourself needing to use some endpoints that are not yet implemented, feel free to reach out! (or even better, submit a PR :))

Installation

The Alma PHP API Client library is tested against all recently supported PHP versions. A modern, supported PHP version is highly recommended.

Composer

You would normally install this package via Composer:

composer require alma/alma-php-client

Without Composer

  • Head over to the releases and grab the alma-php-client.zip file of the latest published library version.
  • Unzip the library into your vendors directory.
  • Require the included Composer's autoload file:
require_once "path/to/alma-php-client/vendor/autoload.php";
  • You should then be able to use Alma as if it was installed with Composer.

Typical usage

An example of using the API client. (check API documentation for further information)

1. instanciate client in test mode

$alma = new Alma\API\Client($apiKey, ['mode' => Alma\API\Client::TEST_MODE]);

2. check eligibility

// ...
$amountInCents = 150000; // 1500 euros
$customerBillingCountry = ""; // can be an empty string but NOT null
$customerShippingCountry = "FR"; // billing_address has priority over shipping_address (if not empty)
try {
    $eligibilities = $alma->payments->eligibility(
        [
            'purchase_amount' => $amountInCents,
            'billing_address' => [ // (optional) useful to check eligibility for a specific billing country
                'country' => $customerBillingCountry // can be an empty string but not null
            ],
            'shipping_address' => [ // (optional) useful to check eligibility for a specific shipping country
                'country' => $customerShippingCountry
            ],
            'queries'         =>
                [
                    [
                        'installments_count' => 1,
                        'deferred_days'      => 30,
                    ],
                    [
                        'installments_count' => 2,
                    ],
                    [
                        'installments_count' => 3,
                    ],
                    [
                        'installments_count' => 4,
                    ],
                    [
                        'installments_count' => 10,
                    ],
                ],
        ],
        $raiseOnError = true // throws an exception on 4xx or 5xx http return code
                             // instead of just returning an Eligibility Object with isEligible() === false
    );
} catch (Alma\API\RequestError $error) {
    header("HTTP/1.1 500 Internal Server Error");
    die($error->getMessage());
}

foreach($eligibilities as $eligibility) {
    if (!$eligibility->isEligible()) {
        die('cart is not eligible');
    }
}
// ...

3. check available fee plans and build payment form

// ...
echo "<form>";
echo "<h2>Available feePlans are:</h2>";
foreach($alma->merchants->feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "all", $includeDeferred = true) as $feePlan) {
    if (!$feePlan->allowed) {
        continue;
    }
    printf('<label for="%s">Pay in %s by %s installments count</label>', $feePlan->getPlanKey(), $feePlan->getDeferredDays(), $feePlan->getInstallmentsCount());
    printf('<input id="radio-%s" type="radio" name="fee-plan" value="%s">', $feePlan->getPlanKey(), $feePlan->getPlanKey());
}
echo "<button type=\"submit\">Submit</button>";
echo "</form>";
// ...

You can prefer use eligibilities to do this work but this part of code allow you to get more familiar with feePlans definitions.

4. build a payment plan

// ...
function formatMoney(int $amount) {
    return sprintf("%.2f %s", round(intval($amount) / 100, 2), "");
}
function formatPercent(int $amount) {
    return sprintf("%.2f %s", round(intval($amount) / 100, 2), "%");
}
// ...
foreach($eligibilities as $eligibility) {
    // display following payment plan (or not eligible message) on feePlan selection using javascript.
    printf('<div id="table-%s">', $eligibility->getPlanKey());
    if (!$eligibility->isEligible()) {
        echo "This fee plan is not eligible!";
        echo "</div>";
        continue;
    }
    if (!$paymentPlans = $eligibility->getPaymentPlan()) {
        echo "No payment plan found for current eligibility! (that should not happen)";
        echo "</div>";
        continue;
    }
    echo "<ul>";
    foreach ($paymentPlans as $paymentPlan) {
        $planDefinition     = sprintf(
            "<li>You will pay %s on %s including %s fees & %s of interest</li>",
            formatMoney($paymentPlan['total_amount']),
            (new DateTime())->setTimestamp($paymentPlan['due_date'])->format('Y-m-d'),
            formatMoney($paymentPlan['customer_fee']),
            formatMoney($paymentPlan['customer_interest'])
        );
    }
    echo "</ul>";
    echo "    <div>";
    echo "    Annual Interest Rate:" . formatPercent($eligibility->getAnnualInterestRate()) . "<br>";
    echo "    Order Amount:" . formatMoney($amountInCents);
    echo "    Total Cost Amount:" . formatMoney($eligibility->getCustomerTotalCostAmount());
    echo "    </div>";
    echo "</div>";
}
// ...

5. create a payment and redirecting a customer to the payment page

// ...
$payment = $alma->payments->createPayment(
    [
        'origin'   => 'online',
        'payment'  =>
            [
                'return_url'         => '<where_the_customer_will_be_redirect_after_alma_checkout>',
                'ipn_callback_url'   => '<your_ipn_callback_url>',
                'purchase_amount'    => 150000,
                'installments_count' => 4,
                'custom_data'        =>
                    [
                        'my_very_important_key' => '<the_context_custom_value>',
                    ],
                'locale'             => 'fr',
                'billing_address'    =>
                    [
                        'first_name'  => 'John',
                        'last_name'   => 'Doe',
                        'email'       => '[email protected]',
                        'line1'       => '1 rue de Rome',
                        'postal_code' => '75001',
                        'city'        => 'Paris',
                        'country'     => 'FR',
                    ],
                'shipping_address'   =>
                    [
                        'first_name'  => 'John',
                        'last_name'   => 'Doe',
                        'email'       => '[email protected]',
                        'line1'       => '1 rue de Rome',
                        'postal_code' => '75001',
                        'city'        => 'Paris',
                        'country'     => 'FR',
                    ],
            ],
        'customer' =>
            [
                'first_name' => 'John',
                'last_name'  => 'Doe',
                'email'      => '[email protected]',
                'phone'      => '06 12 34 56 78',
                'addresses'  =>
                    [
                        [
                            'first_name' => 'John',
                            'last_name'  => 'Doe',
                            'email'      => '[email protected]',
                            'phone'      => '06 12 34 56 78',
                        ],
                    ],
            ],
    ]
);

// store $payment->id and link it to the customer order here ;)

header('Location: ' . $payment->url);
exit();
// ...

6. receive notification about payment validation by IPN

(can be given on payment creation or statically defined in your Alma Dashboard)

// ...
if (!isset($_GET['pid']) || empty($_GET['pid'])) {
     header("HTTP/1.1 400 Bad Request");
     die();
}
// retrieve your local order by payment id
$order = getOrderByPaymentId($_GET['pid'])
if (!$order) {
     header("HTTP/1.1 404 Not Found");
     die();
}

// check $payment->state & do the order / customer stuff you want here :D

header("HTTP/1.1 200");
exit();
// ...

7. retrieve payment information and display status

// ...
$payment = $alma->payments->fetch($paymentId);
switch($payment->state) {
    case Alma\API\Entities\Payment::STATE_IN_PROGRESS: break;
    case Alma\API\Entities\Payment::STATE_PAID: break;
}
// ...

License

Alma PHP Client is distributed under the MIT license.

alma-php-client's People

Contributors

alma-renovate-bot[bot] avatar arnaudrolland avatar benjamin-freoua-alma avatar camillefljt avatar carine-bonnafous avatar clairealma avatar dependabot[bot] avatar francois-gomis avatar gdraynz avatar jir4 avatar joyet-simon avatar lionelquellery avatar natim avatar olance avatar paulrouss3l avatar raharima avatar remic-alma avatar syjust-alma avatar villfa avatar webaaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

alma-php-client's Issues

Fatal Error: Psr\Log\AbstractLogger::emergency

I am currently developing an Alma checkout module for my company eCommerce website, and when it gets initialized I get this error (php 8.1):

Declaration of Psr\Log\AbstractLogger::emergency(Stringable|string $message, array $context = []): void must be compatible with Psr\Log\LoggerInterface::emergency($message, array $context = [])

I am sure this is an issue related to alma-php-client because it is the only dependent to psr/log. Any way to fix/workaround this ?

PSR 4 Warning Test Classes

Just installed with composer, and it seems some test classes are not PS4 valid

  Class Alma\API\Tests\Integration\PaymentsTest located in ./vendor/alma/alma-php-client/tests/integration/Endpoints/PaymentTest.php does not comply with psr-4 autoloading standard. Skipping.
  Class Alma\API\Tests\Unit\PaymentsTest located in ./vendor/alma/alma-php-client/tests/unit/Endpoints/PaymentsTest.php does not comply with psr-4 autoloading standard. Skipping.
  Class Alma\API\Tests\ClientContextTest located in ./vendor/alma/alma-php-client/tests/unit/ClientContextTest.php does not comply with psr-4 autoloading standard. Skipping.
  Class Alma\API\Tests\ArrayUtilsTest located in ./vendor/alma/alma-php-client/tests/unit/ArrayUtilsTest.php does not comply with psr-4 autoloading standard. Skipping.
  Class Alma\API\Tests\ClientOptionsValidatorTest located in ./vendor/alma/alma-php-client/tests/unit/ClientOptionsValidatorTest.php does not comply with psr-4 autoloading standard. Skipping.

Add support for Psr/Log v2/3

I recently developped the commerce_alma module for Drupal Commerce. I'd like to make it available for Drupal 10, however, as D10 now requires Psr/Log v3, the Alma API client install fails.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

composer
composer.json
  • php ^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1 || ~8.2 || ~8.3
  • psr/log ^1 || ^2 || ^3
  • dealerdirect/phpcodesniffer-composer-installer ^1.0.0
  • phpcompatibility/php-compatibility ^9.0
  • phpunit/phpunit ^5 || ^6 || ^7 || ^8 || ^9 || ^10 || ^11
  • squizlabs/php_codesniffer ^3.3
  • mockery/mockery ^1.3
github-actions
.github/workflows/aqua.yml
  • actions/checkout v4
  • ubuntu 22.04
.github/workflows/backport-pull-request.yml
  • actions/checkout v4
  • peter-evans/create-pull-request v7
  • ubuntu 22.04
.github/workflows/ci.yml
  • actions/checkout v4
  • shivammathur/setup-php v2
  • ubuntu 22.04
.github/workflows/hotfix-pull-request.yml
  • actions/checkout v4
  • release-drafter/release-drafter v6
  • actions/github-script v7
  • stefanzweifel/changelog-updater-action v1
  • peter-evans/create-pull-request v7
  • ubuntu 22.04
.github/workflows/pr-labeler.yml
  • TimonVS/pr-labeler-action v5
  • ubuntu 22.04
.github/workflows/release-publish.yml
  • actions/checkout v4
  • arduino/setup-task v2
  • actions/github-script v7
  • LoveToKnow/slackify-markdown-action v1.1.1
  • slackapi/slack-github-action v1.27.0
  • ubuntu 22.04
.github/workflows/release-pull-request.yml
  • actions/checkout v4
  • release-drafter/release-drafter v6
  • stefanzweifel/changelog-updater-action v1
  • actions/create-github-app-token v1
  • peter-evans/create-pull-request v7
  • ubuntu 22.04
pre-commit
.pre-commit-config.yaml
  • pre-commit/pre-commit-hooks v4.6.0
  • returntocorp/semgrep v1.27.0
  • alma/pre-commit-hooks 1.1.2

Error messages are not showing up

If there is an error during an API call, error messages are empty.
While trying to call $alma->payments->create() (and not $alma->payments->createPayment() as it is suggested in your README), I got an error thrown at me, but without a message. I had to dig to the exec() function in your Request class to find the error message.

Setupt timeout on curl

It would be a nice feature to have a customizable way to set up a specific timeout on curl in order to avoid errors

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.