Monday, October 16, 2017

sdf


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

Saturday, February 25, 2017

Convert to camel case and snake case in php

Convert to camel case and snake case in php:

In php, if we want to convert camel case to snake case and vise versa then we have to use following methods:

public static function convertToCamelCase($array) {
        $converted_array = [];
        foreach ($array as $old_key => $value) {
            if (is_array($value)) {
                $value = static::convertToCamelCase($value);
            } else if (is_object($value)) {
                if (method_exists($value, 'toArray')) {
                    $value = $value->toArray();
                } else {
                    $value = (array) $value;
                }
                $value = static::convertToCamelCase($value);
            }
            $converted_array[camel_case($old_key)] = $value;
        }
        return $converted_array;
    }


public static function convertToSnakeCase($array) {
        $converted_array = [];
        foreach ($array as $old_key => $value) {
            if (is_array($value)) {
                $value = static::convertToSnakeCase($value);
            } else if (is_object($value)) {
                if (method_exists($value, 'toArray')) {
                    $value = $value->toArray();
                } else {
                    $value = (array) $value;
                }
                $value = static::convertToSnakeCase($value);
            }
            $new_key = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', $old_key)), '_');
            $converted_array[$new_key] = $value;
        }
        return $converted_array;
    }



This will help for this.

Laravel 5.4 validation for age lesser than 18

Laravel 5.4 validation for age lesser than 18:

In laravel 5.4 validation we have only min & max value , which counts the number.

If in such case we have to validate age then this will not help.

For this we have to use regex like this:

            'age' => ['required','numeric','regex:/^(?:[1-9]\d{2,}+|[2-9]\d|1[89])$/i'],

It will not allow age lesser than 18 

Thanks

Laravel multi select box get old value

Laravel multi select box get old value:

Hi, Some time we want to get old values in laravel (when there is some validation apply on a form.).

In this case, genrally we are unable to get old value for a multiple select box.

For this use this code:

<select name="tribe[]" id="tags" class="form-control" multiple>
                                    @if (is_array(old('tribe')))
                                    @foreach ($tribes as $tribe)
                                    <option value="{{ $tribe->id }}" <?php if(in_array($tribe->id, old('tribe'))) {echo 'selected';} ?> >{{ $tribe->name }}</option>
                                    @endforeach
                                    @else
                                    @foreach ($tribes as $tribe)
                                    <option value="{{ $tribe->id }}" >{{ $tribe->name }}</option>
                                    @endforeach
                                    @endif
                                </select>


This will help for get old values in multiple select box.

Thanks