PHP 7 Amazon Pay API Payment Gateway Integration SDK Example to Collect Payments in Website Using HTML5 Form in Javascript

You are currently viewing PHP 7 Amazon Pay API Payment Gateway Integration SDK Example to Collect Payments in Website Using HTML5 Form in Javascript

PHP 7 Amazon Pay API Payment Gateway Integration SDK Example to Collect Payments in Website Using HTML5 Form in Javascript

    composer require amzn/amazon-pay-api-sdk-php
 

    <span class="pl-ent"><?php</span>
        <span class="pl-k">include</span> <span class="pl-s">'vendor/autoload.php'</span>;
        <span class="pl-k">echo</span> <span class="pl-s">"SDK_VERSION="</span> . <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>::<span class="pl-c1">SDK_VERSION</span> . <span class="pl-s">"\n"</span>;
    <span class="pl-ent">?></span>

ssh-keygen -t rsa -b 2048 -f private.pem
ssh-keygen -f private.pem -e -m PKCS8 > public.pub
 

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout > public.pub
 

    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'ABC123DEF456XYZ'</span>,  <span class="pl-c">// RSA Public Key ID (this is not the Merchant or Seller ID)</span>
        <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>, <span class="pl-c">// Path to RSA Private Key (or a string representation)</span>
        <span class="pl-s">'sandbox'</span>       => <span class="pl-c1">true</span>,               <span class="pl-c">// true (Sandbox) or false (Production) boolean</span>
        <span class="pl-s">'region'</span>        => <span class="pl-s">'us'</span>                <span class="pl-c">// Must be one of: 'us', 'eu', 'jp' </span>
    );

    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
    <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'MY_PUBLIC_KEY_ID'</span>,  <span class="pl-c">// LIVE-XXXXX or SANDBOX-XXXXX</span>
    <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>, <span class="pl-c">// Path to RSA Private Key (or a string representation)</span>
    <span class="pl-s">'region'</span>        => <span class="pl-s">'us'</span> <span class="pl-c">// Must be one of: 'us', 'eu', 'jp'         </span>
);

    <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span> = <span class="pl-en">array</span>(<span class="pl-s">'x-amz-pay-idempotency-key'</span> => <span class="pl-en">uniqid</span>());

    <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);

    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-s">'{"scanData":"UKhrmatMeKdlfY6b","scanReferenceId":"0b8fb271-2ae2-49a5-b35d7","merchantCOE":"US","ledgerCurrency":"USD","chargeTotal":{"currencyCode":"USD","amount":"2.00"},"metadata":{"merchantNote":"Merchant Name","communicationContext":{"merchantStoreName":"Store Name","merchantOrderId":"789123"}}}'</span>;

     <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">instoreMerchantScan</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>);

    <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span> = <span class="pl-en">json_decode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'response'</span>], <span class="pl-c1">true</span>);
    <span class="pl-s1"><span class="pl-c1">
lt;/span>id</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'chargePermissionId'</span>];

    <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span> = <span class="pl-en">array</span>(<span class="pl-s">'x-amz-pay-authtoken'</span> => <span class="pl-s">'other_merchant_super_secret_token'</span>);
    <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">instoreMerchantScan</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span>);

    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'scanData'</span> => <span class="pl-s">'UKhrmatMeKdlfY6b'</span>,
        <span class="pl-s">'scanReferenceId'</span> => <span class="pl-en">uniqid</span>(),
        <span class="pl-s">'merchantCOE'</span> => <span class="pl-s">'US'</span>,
        <span class="pl-s">'ledgerCurrency'</span> => <span class="pl-s">'USD'</span>,
        <span class="pl-s">'chargeTotal'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'currencyCode'</span> => <span class="pl-s">'USD'</span>,
            <span class="pl-s">'amount'</span> => <span class="pl-s">'2.00'</span>
        ),
        <span class="pl-s">'metadata'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'merchantNote'</span> => <span class="pl-s">'Merchant Name'</span>,
            <span class="pl-s">'communicationContext'</span> => <span class="pl-en">array</span>(
                <span class="pl-s">'merchantStoreName'</span> => <span class="pl-s">'Store Name'</span>,
                <span class="pl-s">'merchantOrderId'</span> => <span class="pl-s">'789123'</span>
            )
        )
    );
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">json_encode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>);

    <span class="pl-ent"><?php</span>
    <span class="pl-k">include</span> <span class="pl-s">'vendor/autoload.php'</span>;
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'MY_PUBLIC_KEY_ID'</span>,
        <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>,
        <span class="pl-s">'region'</span>        => <span class="pl-s">'US'</span>,
        <span class="pl-s">'sandbox'</span>       => <span class="pl-c1">false</span>
    );
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'amazonOrderReferenceId'</span> => <span class="pl-s">'P01-0000000-0000000'</span>,
        <span class="pl-s">'deliveryDetails'</span> => <span class="pl-en">array</span>(<span class="pl-en">array</span>(
            <span class="pl-s">'trackingNumber'</span> => <span class="pl-s">'01234567890'</span>,
            <span class="pl-s">'carrierCode'</span> => <span class="pl-s">'FEDEX'</span>
        ))
    );
    <span class="pl-k">try</span> {
        <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
        <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">deliveryTrackers</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>);
        <span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'status'</span>] === <span class="pl-c1">200</span>) {
            <span class="pl-c">// success</span>
            <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'response'</span>] . <span class="pl-s">"\n"</span>;
        } <span class="pl-k">else</span> {
            <span class="pl-c">// check the error</span>
            <span class="pl-k">echo</span> <span class="pl-s">'status="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"status'</span>] . <span class="pl-s">'; response="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"response'</span>] . <span class="pl-s">"\n"</span>;
        }
    } <span class="pl-k">catch</span> (\<span class="pl-smi">Exception</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span>) {
        <span class="pl-c">// handle the exception</span>
        <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span> . <span class="pl-s">"\n"</span>;
    }
    <span class="pl-ent">?></span>

    <span class="pl-ent"><?php</span>
    <span class="pl-en">session_start</span>();
 
    <span class="pl-k">include</span> <span class="pl-s">'vendor/autoload.php'</span>;
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'MY_PUBLIC_KEY_ID'</span>,
        <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>,
        <span class="pl-s">'region'</span>        => <span class="pl-s">'US'</span>,
        <span class="pl-s">'sandbox'</span>       => <span class="pl-c1">true</span>
    );
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'webCheckoutDetails'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'checkoutReviewReturnUrl'</span> => <span class="pl-s">'https://localhost/store/checkout_review'</span>,
            <span class="pl-s">'checkoutResultReturnUrl'</span> => <span class="pl-s">'https://localhost/store/checkout_result'</span>
        ),
        <span class="pl-s">'storeId'</span> => <span class="pl-s">'amzn1.application-oa2-client.000000000000000000000000000000000'</span>
    );
    <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span> = <span class="pl-en">array</span>(<span class="pl-s">'x-amz-pay-Idempotency-Key'</span> => <span class="pl-en">uniqid</span>());
    <span class="pl-k">try</span> {
        <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
        <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">createCheckoutSession</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span>);
 
        <span class="pl-en">header</span>(<span class="pl-s">"Content-type:application/json; charset=utf-8"</span>);
        <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'response'</span>];
        <span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'status'</span>] !== <span class="pl-c1">201</span>) {
            <span class="pl-en">http_response_code</span>(<span class="pl-c1">500</span>);
        }
 
    } <span class="pl-k">catch</span> (\<span class="pl-smi">Exception</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span>) {
        <span class="pl-c">// handle the exception</span>
        <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span> . <span class="pl-s">"\n"</span>;
        <span class="pl-en">http_response_code</span>(<span class="pl-c1">500</span>);
    }
    <span class="pl-ent">?></span>

    <span class="pl-ent"><?php</span>
    <span class="pl-k">include</span> <span class="pl-s">'vendor/autoload.php'</span>;
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'MY_PUBLIC_KEY_ID'</span>,
        <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>,
        <span class="pl-s">'region'</span>        => <span class="pl-s">'US'</span>,
        <span class="pl-s">'sandbox'</span>       => <span class="pl-c1">true</span>
    );
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'webCheckoutDetails'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'checkoutReviewReturnUrl'</span> => <span class="pl-s">'https://localhost/store/checkout_review'</span>,
            <span class="pl-s">'checkoutResultReturnUrl'</span> => <span class="pl-s">'https://localhost/store/checkout_result'</span>
        ),
        <span class="pl-s">'storeId'</span> => <span class="pl-s">'amzn1.application-oa2-client.000000000000000000000000000000000'</span>
    );
    <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span> = <span class="pl-en">array</span>(<span class="pl-s">'x-amz-pay-Idempotency-Key'</span> => <span class="pl-en">uniqid</span>());
    <span class="pl-k">try</span> {
        <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
        <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">createCheckoutSession</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span>);
        <span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'status'</span>] === <span class="pl-c1">201</span>) {
            <span class="pl-c">// created</span>
            <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span> = <span class="pl-en">json_decode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'response'</span>], <span class="pl-c1">true</span>);
            <span class="pl-s1"><span class="pl-c1">
lt;/span>checkoutSessionId</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'checkoutSessionId'</span>];
            <span class="pl-k">echo</span> <span class="pl-s">"checkoutSessionId=$checkoutSessionId\n"</span>;
        } <span class="pl-k">else</span> {
            <span class="pl-c">// check the error</span>
            <span class="pl-k">echo</span> <span class="pl-s">'status="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"status'</span>] . <span class="pl-s">'; response="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"response'</span>] . <span class="pl-s">"\n"</span>;
        }
    } <span class="pl-k">catch</span> (\<span class="pl-smi">Exception</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span>) {
        <span class="pl-c">// handle the exception</span>
        <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span> . <span class="pl-s">"\n"</span>;
    }
    <span class="pl-ent">?></span>

    <span class="pl-ent"><?php</span>
    <span class="pl-k">include</span> <span class="pl-s">'vendor/autoload.php'</span>;
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'MY_PUBLIC_KEY_ID'</span>,
        <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>,
        <span class="pl-s">'region'</span>        => <span class="pl-s">'US'</span>,
        <span class="pl-s">'sandbox'</span>       => <span class="pl-c1">true</span>
    );
 
    <span class="pl-k">try</span> {
        <span class="pl-s1"><span class="pl-c1">
lt;/span>checkoutSessionId</span> = <span class="pl-s">'00000000-0000-0000-0000-000000000000'</span>;
        <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
        <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">getCheckoutSession</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>checkoutSessionId</span>);
        <span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'status'</span>] === <span class="pl-c1">200</span>) {
            <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span> = <span class="pl-en">json_decode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'response'</span>], <span class="pl-c1">true</span>);
            <span class="pl-s1"><span class="pl-c1">
lt;/span>checkoutSessionState</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'statusDetails'</span>][<span class="pl-s">'state'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>chargeId</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'chargeId'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>chargePermissionId</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'chargePermissionId'</span>];
 
            <span class="pl-c">// NOTE: Once Checkout Session moves to a "Completed" state, buyer and shipping</span>
            <span class="pl-c">// details must be obtained from the getCharges() function call instead</span>
            <span class="pl-s1"><span class="pl-c1">
lt;/span>buyerName</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'buyer'</span>][<span class="pl-s">'name'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>buyerEmail</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'buyer'</span>][<span class="pl-s">'email'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>shipName</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'shippingAddress'</span>][<span class="pl-s">'name'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>shipAddrLine1</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'shippingAddress'</span>][<span class="pl-s">'addressLine1'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>shipCity</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'shippingAddress'</span>][<span class="pl-s">'city'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>shipState</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'shippingAddress'</span>][<span class="pl-s">'stateOrRegion'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>shipZip</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'shippingAddress'</span>][<span class="pl-s">'postalCode'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>shipCounty</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'shippingAddress'</span>][<span class="pl-s">'countryCode'</span>];
 
            <span class="pl-k">echo</span> <span class="pl-s">"checkoutSessionState=$checkoutSessionState\n"</span>;
            <span class="pl-k">echo</span> <span class="pl-s">"chargeId=$chargeId; chargePermissionId=$chargePermissionId\n"</span>;
            <span class="pl-k">echo</span> <span class="pl-s">"buyer=$buyerName ($buyerEmail)\n"</span>;
            <span class="pl-k">echo</span> <span class="pl-s">"shipName=$shipName\n"</span>;
            <span class="pl-k">echo</span> <span class="pl-s">"address=$shipAddrLine1; $shipCity $shipState $shipZip ($shipCounty)\n"</span>;
        } <span class="pl-k">else</span> {
            <span class="pl-c">// check the error</span>
            <span class="pl-k">echo</span> <span class="pl-s">'status="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"status'</span>] . <span class="pl-s">'; response="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"response'</span>] . <span class="pl-s">"\n"</span>;
        }
    } <span class="pl-k">catch</span> (\<span class="pl-smi">Exception</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span>) {
        <span class="pl-c">// handle the exception</span>
        <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span> . <span class="pl-s">"\n"</span>;
    }
    <span class="pl-ent">?></span>

    <span class="pl-ent"><?php</span>
    <span class="pl-k">include</span> <span class="pl-s">'vendor/autoload.php'</span>;
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'MY_PUBLIC_KEY_ID'</span>,
        <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>,
        <span class="pl-s">'region'</span>        => <span class="pl-s">'US'</span>,
        <span class="pl-s">'sandbox'</span>       => <span class="pl-c1">true</span>
    );
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">array</span>(
       <span class="pl-s">'paymentDetails'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'paymentIntent'</span> => <span class="pl-s">'Authorize'</span>,
            <span class="pl-s">'canHandlePendingAuthorization'</span> => <span class="pl-c1">false</span>,
            <span class="pl-s">'chargeAmount'</span> => <span class="pl-en">array</span>(
                <span class="pl-s">'amount'</span> => <span class="pl-s">'1.23'</span>,
                <span class="pl-s">'currencyCode'</span> => <span class="pl-s">'USD'</span>
            ),
        ),
        <span class="pl-s">'merchantMetadata'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'merchantReferenceId'</span> => <span class="pl-s">'2020-00000001'</span>,
            <span class="pl-s">'merchantStoreName'</span> => <span class="pl-s">'Store Name'</span>,
            <span class="pl-s">'noteToBuyer'</span> => <span class="pl-s">'Thank you for your order!'</span>
        )
    );
 
    <span class="pl-k">try</span> {
        <span class="pl-s1"><span class="pl-c1">
lt;/span>checkoutSessionId</span> = <span class="pl-s">'00000000-0000-0000-0000-000000000000'</span>;
        <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
        <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">updateCheckoutSession</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>checkoutSessionId</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>);
        <span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'status'</span>] === <span class="pl-c1">200</span>) {
            <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span> = <span class="pl-en">json_decode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'response'</span>], <span class="pl-c1">true</span>);
            <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonPayRedirectUrl</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'webCheckoutDetails'</span>][<span class="pl-s">'amazonPayRedirectUrl'</span>];
            <span class="pl-k">echo</span> <span class="pl-s">"amazonPayRedirectUrl=$amazonPayRedirectUrl\n"</span>;
        } <span class="pl-k">else</span> {
            <span class="pl-c">// check the error</span>
            <span class="pl-k">echo</span> <span class="pl-s">'status="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"status'</span>] . <span class="pl-s">'; response="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"response'</span>] . <span class="pl-s">"\n"</span>;
        }
    } <span class="pl-k">catch</span> (\<span class="pl-smi">Exception</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span>) {
        <span class="pl-c">// handle the exception</span>
        <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span> . <span class="pl-s">"\n"</span>;
    }
    <span class="pl-ent">?></span>

    <span class="pl-ent"><?php</span>
    <span class="pl-k">include</span> <span class="pl-s">'vendor/autoload.php'</span>;
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'MY_PUBLIC_KEY_ID'</span>,
        <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>,
        <span class="pl-s">'region'</span>        => <span class="pl-s">'US'</span>,
        <span class="pl-s">'sandbox'</span>       => <span class="pl-c1">true</span>
    );
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'captureAmount'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'amount'</span> => <span class="pl-s">'1.23'</span>,
            <span class="pl-s">'currencyCode'</span> => <span class="pl-s">'USD'</span>
        ),
        <span class="pl-s">'softDescriptor'</span> => <span class="pl-s">'For CC Statement'</span>
    );
 
    <span class="pl-k">try</span> {
        <span class="pl-s1"><span class="pl-c1">
lt;/span>chargeId</span> = <span class="pl-s">'S01-0000000-0000000-C000000'</span>;
        <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span> = <span class="pl-en">array</span>(<span class="pl-s">'x-amz-pay-Idempotency-Key'</span> => <span class="pl-en">uniqid</span>());
        <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
        <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">captureCharge</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>chargeId</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>headers</span>);
 
        <span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'status'</span>] === <span class="pl-c1">200</span>) {
            <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span> = <span class="pl-en">json_decode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">'response'</span>], <span class="pl-c1">true</span>);
            <span class="pl-s1"><span class="pl-c1">
lt;/span>state</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'statusDetails'</span>][<span class="pl-s">'state'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>reasonCode</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'statusDetails'</span>][<span class="pl-s">'reasonCode'</span>];
            <span class="pl-s1"><span class="pl-c1">
lt;/span>reasonDescription</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>response</span>[<span class="pl-s">'statusDetails'</span>][<span class="pl-s">'reasonDescription'</span>];
            <span class="pl-k">echo</span> <span class="pl-s">"state=$state; reasonCode=$reasonCode; reasonDescription=$reasonDescription\n"</span>;
        } <span class="pl-k">else</span> {
            <span class="pl-c">// check the error</span>
            <span class="pl-k">echo</span> <span class="pl-s">'status="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"status'</span>] . <span class="pl-s">'; response="</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>result</span>[<span class="pl-s">"response'</span>] . <span class="pl-s">"\n"</span>;
        }
    } <span class="pl-k">catch</span> (\<span class="pl-smi">Exception</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span>) {
        <span class="pl-c">// handle the exception</span>
        <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>e</span> . <span class="pl-s">"\n"</span>;
    }
    <span class="pl-ent">?></span>

    <span class="pl-ent"><?php</span>
    <span class="pl-k">include</span> <span class="pl-s">'vendor/autoload.php'</span>;
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'public_key_id'</span> => <span class="pl-s">'MY_PUBLIC_KEY_ID'</span>,
        <span class="pl-s">'private_key'</span>   => <span class="pl-s">'keys/private.pem'</span>,
        <span class="pl-s">'region'</span>        => <span class="pl-s">'US'</span>,
        <span class="pl-s">'sandbox'</span>       => <span class="pl-c1">true</span>
    );
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-s">'{"storeId":"amzn1.application-oa2-client.xxxxx","webCheckoutDetails":{"checkoutReviewReturnUrl":"https://localhost/test/CheckoutReview.php","checkoutResultReturnUrl":"https://localhost/test/CheckoutResult.php"}}'</span>;
    <span class="pl-s1"><span class="pl-c1">
lt;/span>signature</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">generateButtonSignature</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>);
    <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">
lt;/span>signature</span> . <span class="pl-s">"\n"</span>;
    <span class="pl-ent">?></span>

    <span class="pl-c">/*  getPostSignedHeaders convenience – Takes values for canonical request sorts and parses it and</span>
<span class="pl-c">     *  returns a signature for the request being sent</span>
<span class="pl-c">     *  @param $http_request_method [String]</span>
<span class="pl-c">     *  @param $request_uri [String]</span>
<span class="pl-c">     *  @param $request_parameters [array()]</span>
<span class="pl-c">     *  @param $request_payload [string]</span>
<span class="pl-c">     */</span>

    <span class="pl-s1"><span class="pl-c1">
lt;/span>method</span> = <span class="pl-s">'POST'</span>;
 
    <span class="pl-c">// API Merchant Scan</span>
    <span class="pl-s1"><span class="pl-c1">
lt;/span>url</span> = <span class="pl-s">'https://pay-api.amazon.com/sandbox/'</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>versiom</span> . <span class="pl-s">'/in-store/merchantScan'</span>;
    
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'scanData'</span> => <span class="pl-s">'UKhrmatMeKdlfY6b'</span>,
        <span class="pl-s">'scanReferenceId'</span> => <span class="pl-s">'0b8fb271-2ae2-49a5-b35d4'</span>,
        <span class="pl-s">'merchantCOE'</span> => <span class="pl-s">'US'</span>,
        <span class="pl-s">'ledgerCurrency'</span> => <span class="pl-s">'USD'</span>,
        <span class="pl-s">'chargeTotal'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'currencyCode'</span> => <span class="pl-s">'USD'</span>,
            <span class="pl-s">'amount'</span> => <span class="pl-s">'2.00'</span>
        ),
        <span class="pl-s">'metadata'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'merchantNote'</span> => <span class="pl-s">'Ice Cream'</span>,
            <span class="pl-s">'customInformation'</span> => <span class="pl-s">'In-store Ice Cream'</span>,
            <span class="pl-s">'communicationContext'</span> => <span class="pl-en">array</span>(
                <span class="pl-s">'merchantStoreName'</span> => <span class="pl-s">'Store Name'</span>,
                <span class="pl-s">'merchantOrderId'</span> => <span class="pl-s">'789123'</span>
            )
        )
    );
 
    <span class="pl-c">// Convert to json string</span>
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">json_encode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>);
    
    <span class="pl-s1"><span class="pl-c1">
lt;/span>requestParameters</span> = <span class="pl-en">array</span>();
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>postSignedHeaders</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">getPostSignedHeaders</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>method</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>url</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>requestParameters</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>);

  <span class="pl-c">/*    createSignature convenience – Takes values for canonical request sorts and parses it and</span>
<span class="pl-c">   *    returns a signature for the request being sent</span>
<span class="pl-c">   *    @param $http_request_method [String]</span>
<span class="pl-c">   *    @param $request_uri [String]</span>
<span class="pl-c">   *    @param $request_parameters [Array()]</span>
<span class="pl-c">   *    @param $pre_signed_headers [Array()]</span>
<span class="pl-c">   *    @param $request_payload [String]</span>
<span class="pl-c">   *    @param $timeStamp [String]</span>
<span class="pl-c">   */</span>
 
    <span class="pl-c">// Example request method:</span>
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>method</span> = <span class="pl-s">'POST'</span>;
 
    <span class="pl-c">// API Merchant Scan</span>
    <span class="pl-s1"><span class="pl-c1">
lt;/span>url</span> = <span class="pl-s">'https://pay-api.amazon.com/sandbox/in-store/'</span> . <span class="pl-s1"><span class="pl-c1">
lt;/span>version</span> . <span class="pl-s">'/merchantScan'</span>;
    
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">array</span>(
        <span class="pl-s">'scanData'</span> => <span class="pl-s">'ScanData'</span>,
        <span class="pl-s">'scanReferenceId'</span> => <span class="pl-s">'0b8fb271-2ae2-49a5-b35d4'</span>,
        <span class="pl-s">'merchantCOE'</span> => <span class="pl-s">'US'</span>,
        <span class="pl-s">'ledgerCurrency'</span> => <span class="pl-s">'USD'</span>,
        <span class="pl-s">'chargeTotal'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'currencyCode'</span> => <span class="pl-s">'USD'</span>,
            <span class="pl-s">'amount'</span> => <span class="pl-s">'2.00'</span>
        ),
        <span class="pl-s">'metadata'</span> => <span class="pl-en">array</span>(
            <span class="pl-s">'merchantNote'</span> => <span class="pl-s">'Ice Cream'</span>,
            <span class="pl-s">'customInformation'</span> => <span class="pl-s">'In-store Ice Cream'</span>,
            <span class="pl-s">'communicationContext'</span> => <span class="pl-en">array</span>(
                <span class="pl-s">'merchantStoreName'</span> => <span class="pl-s">'Store Name'</span>,
                <span class="pl-s">'merchantOrderId'</span> => <span class="pl-s">'789123'</span>
            )
        )
    );
 
    <span class="pl-c">// Convert to json string</span>
    <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span> = <span class="pl-en">json_encode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>);
    
    <span class="pl-s1"><span class="pl-c1">
lt;/span>requestParameters</span> = <span class="pl-en">array</span>();
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Amazon</span>\<span class="pl-v">Pay</span>\<span class="pl-c1">API</span>\<span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
 
    <span class="pl-c">// Create an array that will contain the parameters for the charge API call</span>
    <span class="pl-s1"><span class="pl-c1">
lt;/span>pre_signed_headers</span> = <span class="pl-en">array</span>();
    <span class="pl-s1"><span class="pl-c1">
lt;/span>pre_signed_headers</span>[<span class="pl-s">'Accept'</span>] = <span class="pl-s">'application/json'</span>;
    <span class="pl-s1"><span class="pl-c1">
lt;/span>pre_signed_headers</span>[<span class="pl-s">'Content-Type'</span>] = <span class="pl-s">'application/json'</span>;
    <span class="pl-s1"><span class="pl-c1">
lt;/span>pre_signed_headers</span>[<span class="pl-s">'X-Amz-Pay-Region'</span>] = <span class="pl-s">'na'</span>;
 
    <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span> = <span class="pl-k">new</span> <span class="pl-v">Client</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>amazonpay_config</span>);
    <span class="pl-s1"><span class="pl-c1">
lt;/span>signedInput</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>client</span>-><span class="pl-en">createSignature</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>method</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>url</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>requestParameters</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>pre_signed_headers</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>payload</span>, <span class="pl-s">'20180326T203730Z'</span>);

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

This Post Has 3 Comments

  1. Balack Adam

    most of your projects, there a lots of errors, it can’t run as you said, why can’t you do a clean project and instructions on how to run the projects on localhost as well as how it works online before posting.

    note: your side is not good as usual, because ads is too much.

      1. Balack Adam

        Thank you! the ads that disrupt people in your site have been remove, but I have not test any one of your projects yet, especially PHP. whenever I test one of it. I will notify you. bye.

        Have a great day! you are a wonderful developer.

Leave a Reply