sunaoka/amazon-pay-api-sdk-php-laravel

Amazon Pay API SDK (PHP) for Laravel

v1.3.1 2024-03-29 00:38 UTC

This package is auto-updated.

Last update: 2024-11-03 02:09:03 UTC


README

Latest Stable Version License PHP from Packagist Laravel Test codecov

This library is The Laravel Framework binding for amazon-pay-api-sdk-php.

Installation

composer require sunaoka/amazon-pay-api-sdk-php-laravel

Configurations

php artisan vendor:publish --tag=amazon-pay-config

The settings can be found in the generated config/amazon-pay.php configuration file.

<?php

return [
    'sandbox'              => (bool)env('AMAZON_PAY_SANDBOX', true),
    'merchant_id'          => env('AMAZON_PAY_MERCHANT_ID'),
    'store_id'             => env('AMAZON_PAY_STORE_ID'),
    'public_key_id'        => env('AMAZON_PAY_PUBLIC_KEY_ID'),
    'private_key'          => env('AMAZON_PAY_PRIVATE_KEY'),
    'region'               => env('AMAZON_PAY_REGION'),
    'language'             => env('AMAZON_PAY_LANGUAGE'),
    'currency_code'        => env('AMAZON_PAY_CURRENCY_CODE'),
    'algorithm'            => env('AMAZON_PAY_ALGORITHM'),
    'override_service_url' => env('AMAZON_PAY_OVERRIDE_SERVICE_URL'),
    'proxy'                => !empty(env('AMAZON_PAY_PROXY_HOST')) ? [
        'host'     => env('AMAZON_PAY_PROXY_HOST'),
        'port'     => env('AMAZON_PAY_PROXY_PORT'),
        'username' => env('AMAZON_PAY_PROXY_USERNAME'),
        'password' => env('AMAZON_PAY_PROXY_PASSWORD'),
    ] : null,
];

Usage

# .env
AMAZON_PAY_SANDBOX=true
AMAZON_PAY_MERCHANT_ID=ABC123DEF456XY
AMAZON_PAY_STORE_ID=amzn1.application-oa2-client.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AMAZON_PAY_PUBLIC_KEY_ID=ABC123DEF456XYZ789IJK000
AMAZON_PAY_PRIVATE_KEY=keys/private.pem
AMAZON_PAY_REGION=us
AMAZON_PAY_LANGUAGE=en_US
AMAZON_PAY_CURRENCY_CODE=USD
<?php

$payload = [
    'storeId'            => config('amazon-pay.store_id'),
    'webCheckoutDetails' => [
        'checkoutReviewReturnUrl' => 'http://localhost/checkout/review',
        'checkoutResultReturnUrl' => 'http://localhost/checkout/result',
        'checkoutCancelUrl'       => 'http://localhost/checkout/cancel',
    ],
];

$signature = \AmazonPay::generateButtonSignature($payload);

$button = [
    'merchantId'                  => config('amazon-pay.merchant_id'),
    'ledgerCurrency'              => config('amazon-pay.currency_code'),
    'sandbox'                     => config('amazon-pay.sandbox'),
    'checkoutLanguage'            => config('amazon-pay.language'),
    'productType'                 => 'PayAndShip',
    'placement'                   => 'Cart',
    'createCheckoutSessionConfig' => [
        'payloadJSON' => $payload,
        'signature'   => $signature,
        'publicKeyId' => config('amazon-pay.public_key_id'),
        'algorithm'   => config('amazon-pay.algorithm'),
    ],
];
<body>
    <div id="AmazonPayButton"></div>

    <script src="{{ \AmazonPay::getAmazonPayScript() }}"></script>
    <script type="text/javascript" charset="utf-8">
        amazon.Pay.renderButton('#AmazonPayButton', @json($button));
    </script>
</body>

Testing

You may use the AmazonPay facade's fake method to apply the mock response.

<?php

\AmazonPay::fake([
    'refundId'           => 'S01-5105180-3221187-R022311',
    'chargeId'           => 'S01-5105180-3221187-C056351',
    'refundAmount'       => [
        'amount'       => '14.00',
        'currencyCode' => 'USD'
    ],
    'softDescriptor'     => 'Descriptor',
    'creationTimestamp'  => '20190714T155300Z',
    'statusDetails'      => [
        'state'                => 'RefundInitiated',
        'reasonCode'           => null,
        'reasonDescription'    => null,
        'lastUpdatedTimestamp' => '20190714T155300Z'
    ],
    'releaseEnvironment' => 'Sandbox',
]);

$payload = [
    'chargeId'       => 'S01-5105180-3221187-C056351',
    'refundAmount'   => [
        'amount'       => '14.00',
        'currencyCode' => 'USD',
    ],
    'softDescriptor' => 'Descriptor',
];

$response = \AmazonPay::createRefund($payload, []);
$result = json_decode($response['response'], true);

echo $result['refundId']; // S01-5105180-3221187-R022311