Saturday, May 27, 2017

Stripe Payment API: Create Charge Or Payment

You need a Token (Saved credit card instance) to create a Payment which is known as Charge in Stripe API. Creating Charge in Stripe is equal to creating a Payment. 

Payment API documentation:
https://stripe.com/docs/api#charges

<?php
function createCharge()
{
    $token = CreateToken::create();

    if ($token["code"] != 200) {
        StripeCharge::prettyPrint($token["response"]);
        exit;
    }

    $params = array(
        "amount" => "200",
        "currency" => "aud",
        "source" => $token["response"]->id,
        "description" => "Some description against charge/payment"
    );
    $create = StripeCharge::create($params);
    StripeCharge::prettyPrint($create);
}
createCharge();

class StripeCharge {
    private static $key = "sk_test_...";

    static function create($params)
    {
        $url = "https://api.stripe.com/v1/charges";

        $headers[] = "Authorization: Bearer " . self::$key;
        $headers[] = "Content-Type: application/x-www-form-urlencoded";

        return makeCurlCall($url, "POST", null, $params, $headers);
    }

    static function prettyPrint($data)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }
}

Output below:



Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => ch_1ANxxAFIwfarG3vBDqR8ROkg
            [object] => charge
            [amount] => 200
            [amount_refunded] => 0
            [application] =>
            [application_fee] =>
            [balance_transaction] => txn_1ANxxAFIwfarG3vB3wrLUKgn
            [captured] => 1
            [created] => 1495864748
            [currency] => aud
            [customer] =>
            [description] => Some description against charge/payment
            [destination] =>
            [dispute] =>
            [failure_code] =>
            [failure_message] =>
            [fraud_details] => stdClass Object
                (
                )

            [invoice] =>
            [livemode] =>
            [metadata] => stdClass Object
                (
                )

            [on_behalf_of] =>
            [order] =>
            [outcome] => stdClass Object
                (
                    [network_status] => approved_by_network
                    [reason] =>
                    [risk_level] => normal
                    [seller_message] => Payment complete.
                    [type] => authorized
                )

            [paid] => 1
            [receipt_email] =>
            [receipt_number] =>
            [refunded] =>
            [refunds] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                        )

                    [has_more] =>
                    [total_count] => 0
                    [url] => /v1/charges/ch_1ANxxAFIwfarG3vBDqR8ROkg/refunds
                )

            [review] =>
            [shipping] =>
            [source] => stdClass Object
                (
                    [id] => card_1ANxx8FIwfarG3vBa0fmLGhT
                    [object] => card
                    [address_city] =>
                    [address_country] =>
                    [address_line1] =>
                    [address_line1_check] =>
                    [address_line2] =>
                    [address_state] =>
                    [address_zip] =>
                    [address_zip_check] =>
                    [brand] => Visa
                    [country] => US
                    [customer] =>
                    [cvc_check] => pass
                    [dynamic_last4] =>
                    [exp_month] => 12
                    [exp_year] => 2019
                    [fingerprint] => CjZNbbCtG5QSnuIS
                    [funding] => credit
                    [last4] => 4242
                    [metadata] => stdClass Object
                        (
                        )

                    [name] => Card Name
                    [tokenization_method] =>
                )

            [source_transfer] =>
            [statement_descriptor] =>
            [status] => succeeded
            [transfer_group] =>
        )

)

No comments:

Post a Comment