Showing posts with label Stripe. Show all posts
Showing posts with label Stripe. Show all posts

Saturday, October 7, 2017

Stripe Payment API: Create Charge Or Payment Using Java Code

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 


package com.pkm;

import common.HttpJavaClient;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author PRITOM
 */
public class StripeCreatePayment {
    static final String API_KEY = "sk_test_eXF5nNyXat29WjWrqHK92rcj";
    static final String TOKEN_URL = "https://api.stripe.com/v1/tokens";
    static final String PAYMENT_URL = "https://api.stripe.com/v1/charges";
    
    public static void main(String[] args) {        
        Map params = new HashMap();
        params.put("amount", "210");
        params.put("currency", "aud");
        params.put("source", createToken());
        params.put("description", (new Date()).toString());
        String paramString = HttpJavaClient.buildParameters(params);
        
        Map headers = new HashMap();
        headers.put("Authorization", "Bearer " + API_KEY);
        headers.put("tls", "TLSv1.2");
        
        HttpJavaClient.Response response = HttpJavaClient.doPost(PAYMENT_URL, paramString, HttpJavaClient.Type.URL_ENCODED, headers);
        HttpJavaClient.println(response);
    }
    
    static String createToken() {
        Map params = new HashMap();
        params.put("card[name]", "Pritom Kumar");
        params.put("card[number]", "4242424242424242");
        params.put("card[exp_month]", "12");
        params.put("card[exp_year]", "19");
        params.put("card[cvc]", "123");
        String paramString = HttpJavaClient.buildParameters(params);
        
        Map headers = new HashMap();
        headers.put("Authorization", "Bearer " + API_KEY);
        headers.put("tls", "TLSv1.2");
        
        HttpJavaClient.Response response = HttpJavaClient.doPost(TOKEN_URL, paramString, HttpJavaClient.Type.URL_ENCODED, headers);
        if (response.getCode() == 200) {
            String token = response.getBody().substring(response.getBody().indexOf("\"id\": \"") + 7);
            return token.substring(0, token.indexOf("\""));
        }
        return null;
    }
}

And output would be for successful transaction line below:


  "id": "ch_1BACCFFIwfarG3vBdzS2A09q",
  "object": "charge",
  "amount": 210,
  "amount_refunded": 0,
  "application": null,
  "application_fee": null,
  "balance_transaction": "txn_1BACCFFIwfarG3vBWUtFsX4N",
  "captured": true,
  "created": 1507359243,
  "currency": "aud",
  "customer": null,
  "description": "Sat Oct 07 12:54:06 BDT 2017",
  "destination": null,
  "dispute": null,
  "failure_code": null,
  "failure_message": null,
  "fraud_details": {},
  "invoice": null,
  "livemode": false,
  "metadata": {},
  "on_behalf_of": null,
  "order": null,
  "outcome": {
    "network_status": "approved_by_network",
    "reason": null,
    "risk_level": "normal",
    "seller_message": "Payment complete.",
    "type": "authorized"
  },
  "paid": true,
  "receipt_email": null,
  "receipt_number": null,
  "refunded": false,
  "refunds": {
    "object": "list",
    "data": [],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/charges/ch_1BACCFFIwfarG3vBdzS2A09q/refunds"
  },
  "review": null,
  "shipping": null,
  "source": {
    "id": "card_1BACCDFIwfarG3vBAnQYrCBR",
    "object": "card",
    "address_city": null,
    "address_country": null,
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": null,
    "address_zip_check": null,
    "brand": "Visa",
    "country": "US",
    "customer": null,
    "cvc_check": "pass",
    "dynamic_last4": null,
    "exp_month": 12,
    "exp_year": 2019,
    "fingerprint": "CjZNbbCtG5QSnuIS",
    "funding": "credit",
    "last4": "4242",
    "metadata": {},
    "name": "Pritom Kumar",
    "tokenization_method": null
  },
  "source_transfer": null,
  "statement_descriptor": null,
  "status": "succeeded",
  "transfer_group": null
}

Wednesday, September 27, 2017

Stripe Payment GateWay | Refund Stripe VIA API | Stripe API Refund Payment

Stripe Payment GateWay | Refund Stripe VIA API | Stripe API Refund Payment



<?php
include_once "curl.php";
$key = "sk_test_eXF4nNy........rcHK95rwj";

$params = array(
    "charge" => "ch_1B3bkMFIwfarG3vBaBs0hodp",
    "amount" => 10,
    "metadata" => array(
        "Refund-reason" => "Some reason to fail",
        "Refund-by" => "Pritom Kumaar"
    )
);

$url = "https://api.stripe.com/v1/refunds";

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

$response = CurlExecutor::execute($url, "POST", $params, null, $headers);
$response["response"] = json_decode($response["response"]);
CurlExecutor::prettyPrint($response);

And output would be like below:

Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => re_1B6h5DFIwfarG3vBvFwcbNl9
            [object] => refund
            [amount] => 10
            [balance_transaction] => txn_1B6h5DFIwfarG3vBALLuMSUw
            [charge] => ch_1B3bkMFIwfarG3vBaBs0hodp
            [created] => 1506524659
            [currency] => aud
            [metadata] => stdClass Object
                (
                    [Refund-reason] => Some reason to fail
                    [Refund-by] => Pritom Kumaar
                )

            [reason] => 
            [receipt_number] => 
            [status] => succeeded
        )

)

Monday, September 18, 2017

Stripe Using Connect with Deferred Standard Accounts | Stripe Create Deferred Standard Account | Stripe Create Connected Account Using API Call

1 Login to your stripe account and navigate to https://dashboard.stripe.com/account/applications/settings and follow below steps:









2. Now have to create Deferred (standard) account using below code snippet (API call). Later account holder will login to their account and complete their verification process:



<?php
include_once "curl.php";

$api_key = "sk_test_eXF..........jWrcHK95rwj";
$url = "https://api.stripe.com/v1/accounts";

$params = array(
    "type" => "standard",
    "country" => "AU",
    "email" => "pritomkucse+stripe1@gmail.com"
);

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

$result = CurlExecutor::execute($url, "POST", null, $params, $headers);
$result["response"] = json_decode($result["response"]);
CurlExecutor::prettyPrint($result);

Successful output would be like below:


Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => acct_1B3......1R7ddx6
            [object] => account
            [business_logo] => 
            [business_name] => 
            [business_url] => 
            [charges_enabled] => 1
            [country] => AU
            [default_currency] => aud
            [details_submitted] => 
            [display_name] => 
            [email] => pritomkucse+stripe1@gmail.com
            [keys] => stdClass Object
                (
                    [secret] => sk_test_wel..........5fXfyBsmhI6
                    [publishable] => pk_test_mlPoU..........zBUbHBFof
                )

            [metadata] => stdClass Object
                (
                )

            [payouts_enabled] => 
            [statement_descriptor] => 
            [support_email] => 
            [support_phone] => 
            [timezone] => Etc/UTC
            [type] => standard
        )

)

Use "keys/secret" (actually it is "Secret key" for API), you can use this Key to process all your transactions.

And list of your connected account is below:




Stripe Using Connect with Standard Accounts | Stripe Create Standard Account

1 Login to your stripe account and navigate to https://dashboard.stripe.com/account/applications/settings and follow below steps:








2. Copy Client ID and follow below PHP script (which will redirect target users to Stripe application page to fill up their personal details to create and account with Stripe):



<?php
$client_id = "ca_BPnCyPD...........ODApFdHyTXHbwQ";
$auth_url = "https://connect.stripe.com/oauth/authorize?".
    "response_type=code&client_id=$client_id&scope=read_write".
    "&redirect_uri=".rawurlencode("http://localhost/ci/dragon71/stripe/callback.php").
    "&state=".md5(trim(true));
header("Refresh:0; url=$auth_url");

3. After completing procedures there, Stripe will redirect back to our target redirect URL as below:


http://localhost/ci/dragon71/stripe/callback.php?state=c4ca4238a0b923820dcc509a6f75849b&scope=read_write&code=ac_BQGZyHtVkFbOFYhNPTlAdbZeZcMutzec

If fails then:

http://localhost/ci/dragon71/stripe/callback.php?state=c4ca4238a0b923820dcc509a6f75849b&error=access_denied&error_description=The+user+denied+your+request

4. One thing we forgot, have to collect our API Key to request API call, navigate to https://dashboard.stripe.com/account/apikeys to collect your API Key's.




5. After successful redirect back, it's time to collect access token to procedure further. 


<?php
include_once "curl.php";

$api_key = "sk_test_eXF4.........jWrcHK95rwj";
$url = "https://connect.stripe.com/oauth/token";

$params = array(
    "code" => $_GET["code"],
    "grant_type" => "authorization_code"
);

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

$result = CurlExecutor::execute($url, "POST", null, $params, $headers);
$result["response"] = json_decode($result["response"]);
CurlExecutor::prettyPrint($result);

Successful output would be like below:


Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [access_token] => sk_test_oWtb.........xb4tLBXqZmh
            [livemode] => 
            [refresh_token] => rt_BQGsyumxyAoDH....................aPMt5MvpprSnpwD
            [token_type] => bearer
            [stripe_publishable_key] => pk_test_xHhgw.........zfo0QYltBB
            [stripe_user_id] => acct_1B3........3aeEh
            [scope] => read_write
        )

)

Use "access_token" (actually it is "Secret key" for API), you can use this Key to process all your transactions.

And list of your connected account is below:




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] =>
        )

)

Friday, May 26, 2017

Stripe Payment API: Create Get Edit Delete Customer

You can create Customer in Stripe using API. You can profile source when you create the Customer. Here source is payment source, Such you can create a Credit Card Token and provide reference as source. To create Credit Card Token follow below link:

http://pritomkumar.blogspot.com/2017/05/stripe-payment-api-create-token-using.html

And below link for API details:
https://stripe.com/docs/api#create_customer

Below is a PHP Script that will Create Get And Delete Customer From Stripe.



<?php
$params = array(
    "email" => "customer_3@pritom.com",
    "source" => "token_id_using_above_link" 
 );
$create = StripeCustomer::create($params);
StripeCustomer::prettyPrint($create);

if ($create["code"] == 200) {
    $get = StripeCustomer::get($create["response"]->id);
    StripeCustomer::prettyPrint($get);

    $delete = StripeCustomer::delete($create["response"]->id);
    StripeCustomer::prettyPrint($delete);
}

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

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

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

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

    static function get($id)
    {
        $url = "https://api.stripe.com/v1/customers/$id";

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

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

    static function delete($id)
    {
        $url = "https://api.stripe.com/v1/customers/$id";

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

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

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

Below is output of the Script above:



CREATE REQUEST RESPONSE:
Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => cus_Aj4vdApn2NQIbB
            [object] => customer
            [account_balance] => 0
            [created] => 1495783280
            [currency] => 
            [default_source] => card_1ANcktFIwfarG3vBDsNU6Bg9
            [delinquent] => 
            [description] => 
            [discount] => 
            [email] => customer_8@pritom.com
            [livemode] => 
            [metadata] => stdClass Object
                (
                )

            [shipping] => 
            [sources] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => card_1ANcktFIwfarG3vBDsNU6Bg9
                                    [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] => cus_Aj4vdApn2NQIbB
                                    [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] => 
                                )

                        )

                    [has_more] => 
                    [total_count] => 1
                    [url] => /v1/customers/cus_Aj4vdApn2NQIbB/sources
                )

            [subscriptions] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                        )

                    [has_more] => 
                    [total_count] => 0
                    [url] => /v1/customers/cus_Aj4vdApn2NQIbB/subscriptions
                )

        )

)
GET REQUEST RESPONSE
Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => cus_Aj4kNrfMs3ivE8
            [object] => customer
            [account_balance] => 0
            [created] => 1495782666
            [currency] =>
            [default_source] =>
            [delinquent] =>
            [description] =>
            [discount] =>
            [email] => customer_3@pritom.com
            [livemode] =>
            [metadata] => stdClass Object
                (
                )

            [shipping] =>
            [sources] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                        )

                    [has_more] =>
                    [total_count] => 0
                    [url] => /v1/customers/cus_Aj4kNrfMs3ivE8/sources
                )

            [subscriptions] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                        )

                    [has_more] =>
                    [total_count] => 0
                    [url] => /v1/customers/cus_Aj4kNrfMs3ivE8/subscriptions
                )

        )

)

DELETE REQUEST RESULT
Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [deleted] => 1
            [id] => cus_Aj4kNrfMs3ivE8
        )

)