Thursday, May 25, 2017

Stripe Connect Account with Laravel 5.4

Stripe Connect Account with Laravel 5.4:

In laravel 5.4, I want to use Stripe Connect & payment to this connected account from an another user's card.

For this, firstly connect Stripe Account:

    public function getConnect() {
        try {
            if (isset($_GET['code']) && isset($_GET['state'])) {
                $userId = UtilitiesModels::getUserId($_GET['state']);
                if ($userId == config('constant.NUMERIC_ZERO')) {
                    return Utilities::responseError(__('messages.UNAUTHORISED'), config('constant.FOUR_HUNDRED'));
                }

                $token_request_body = array(
                    'grant_type' => 'authorization_code',
                    'client_id' => env('STRIPE_CLIENT_ID'),
                    'code' => $_GET['code'],
                    'client_secret' => env('STRIPE_SECRET_KEY')
                );
                $req = curl_init(env('STRIPE_TOKEN_URI'));
                curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($req, CURLOPT_POST, true);
                curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($token_request_body));
                curl_getinfo($req, CURLINFO_HTTP_CODE);
                $stripe = json_decode(curl_exec($req), true);
                curl_close($req);

                if (isset($stripe['stripe_user_id']) && !empty($stripe['stripe_user_id'])) {
                    UtilitiesModels::updateSellerStripeAcc($userId, $stripe['stripe_user_id']);
                    $response = redirect()->away(config('constant.HTTP_SUCCESS'));
                } else {
                    $response = redirect()->away(config('constant.HTTP_ERROR'));
                }
            } else {
                $response = redirect()->away(config('constant.HTTP_ERROR'));
            }
        } catch (Exception $e) {
            Log::error(__METHOD__ . ' ' . $e->getMessage());
            $response = redirect()->away(config('constant.HTTP_ERROR'));
        }
        return $response;
    }


Now i want to charge amount from first stripe token (which we get from card details) & then transfer to this connected stripe account

Stripe::setApiKey(env('STRIPE_SECRET_KEY'));
            $charge = Charge::create(array(
                        'source' => $request->buyerStripeToken,
                        'amount' => $request->amount * config('constant.HUNDRED'),
                        'currency' => 'aud',
                        'description' => __('messages.PAYMENT_FROM_BUYER'),
                        'destination' => array(
                            "amount" => $sellerEntity->price * config('constant.HUNDRED'),
                            "account" => $user->seller_stripe_account
                        ),
            ));


Here:
$_GET['code'] is the code which we get from Stripe after Stripe Auth.
$_GET['state'] is access token which we get from front end for getting user id.
env('STRIPE_CLIENT_ID') is stripe client id.
env('STRIPE_SECRET_KEY') is stripe secret key.
env('STRIPE_TOKEN_URI') is https://connect.stripe.com/oauth/token
config('constant.HTTP_SUCCESS') is http://success
config('constant.HTTP_ERROR') is http://error
$request->buyerStripeToken is Stripe token which comes from stripe card
$request->amount is amount which we deduct
$sellerEntity->price is amount which we transfer to Stripe Account
$user->seller_stripe_account is connected Stripe Account

Thanks

1 comment:

  1. If you're trying to lose fat then you certainly need to try this totally brand new tailor-made keto diet.

    To produce this keto diet, certified nutritionists, fitness trainers, and professional cooks joined together to provide keto meal plans that are powerful, suitable, price-efficient, and fun.

    Since their launch in January 2019, 1000's of people have already remodeled their body and well-being with the benefits a professional keto diet can provide.

    Speaking of benefits: clicking this link, you'll discover eight scientifically-tested ones offered by the keto diet.

    ReplyDelete