Thursday, May 25, 2017

How to set Deep Linking in Laravel 5.4

How to set Deep Linking in Laravel 5.4:

For this

    public function redirectDeepLink(Request $request) {
        try {
            $device = $this->isMobileDevice();
            $id = $request->input('itemId');
            $app = config('constant.DEEPLINKING.APP') . $id;

            $data = array();
            if ($device == 'iPhone') {
                $data['primaryRedirection'] = $app;
                $data['secndaryRedirection'] = config('constant.DEEPLINKING.APPSTORE');
            } else {
                $redirect = config('constant.DEEPLINKING.WEBSITE');
                return redirect($redirect);
            }
            return View('deep-linking', $data);
        } catch (Exception $e) {
            Log::error(__METHOD__ . ' ' . $e->getMessage());
            return Utilities::responseError(__('messages.SOMETHING_WENT_WRONG') . $e->getMessage());
        }
    }

    private function isMobileDevice() {
        $aMobileUA = array(
            '/iphone/i' => 'iPhone',
            '/ipod/i' => 'iPod',
            '/ipad/i' => 'iPad',
            '/android/i' => 'Android',
            '/blackberry/i' => 'BlackBerry',
            '/webos/i' => 'Mobile'
        );
        //Return true if Mobile User Agent is detected
        foreach ($aMobileUA as $sMobileKey => $sMobileOS) {
            if (preg_match($sMobileKey, $_SERVER['HTTP_USER_AGENT'])) {
                return $sMobileOS;
            }
        }
        //Otherwise return false..
        return false;
    }


Here:
config('constant.DEEPLINKING.APP') is Path of App
config('constant.DEEPLINKING.APPSTORE') is Path of App in Appstore
config('constant.DEEPLINKING.WEBSITE') is website domain

in deep-linking.blade.php add this code

<html>
<head>
</head>
<body>
<script>

  window.location = "{{$primaryRedirection}}"; // will result in error message if app not installed
  setTimeout(function() {
     // Link to the App Store should go here -- only fires if deep link fails
     window.location = "{{$secndaryRedirection}}";
  }, 500);

</script>
</body>
</html>


Thanks

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