dimanche 22 mai 2016

Use Stripe to post to external API

I'm adding payment to my app using Stripe. My app is divided into two parts, a front-end Angular app (http://ift.tt/1WJWIzJ), and a separate Laravel API. They live on two servers.

I'm using Stripe Checkout embed form and Laravel Cashier. Stripe is supposed to hijack my POST request and send the Credit Card data to their servers, then return a credit card token to me, to which I then post to my server.

Their form action assumes my app's API and ClientSide is all within the same app: <form action="" method="POST">

So, to post the token to my API endpoint, doIt, I added:

<form action="http://ift.tt/1qE1bpR" method="POST">
    <script
            src="http://ift.tt/1doUtf9" class="stripe-button"
            data-key="pk_test_UAgGwte0zwi0vCHVSKjAooqk"
            data-amount="999"
            data-name="Demo Site"
            data-description="Widget"
            data-image="/img/documentation/checkout/marketplace.png"
            data-locale="auto">
    </script>
</form>

Which should go here: $router->post('/doIt', 'Resources\Subscriptions@createSubscription');

And I'm trying to die out this info in the Controller:

public function createSubscription()
{

    $token = Input::get('stripeToken');

    $user = User::find(1);

    dd($token);

    $user->subscription('monthly')->create($token);

    $user->trial_ends_at = Carbon::now()->addDays(14);

    $user->save();
}

When I click the Checkout client button, the request goes as expected to Stripe, but it simply routes me to my API. How can I post the credit card token to my API and route as necessary on my client side? (Like, "payment successful!")


Edit: I'm using Angular, so I was thinking of capturing the hidden CC token field in my form controller, then posting from there. But the hidden field is generated on the fly and therefore doesn't exist.



via Chebli Mohamed

TokenMismatchException for API in Laravel 5.2.31

What Am I trying?

I already have a website and I am trying Token based authentication and below is the start for sample authentication code

I created a controller below is the code.

class AccountController extends \App\Http\Controllers\Controller
{
    public function apilogin($UserData) {
        return json_decode($UserData);
    }
}

My route config is below.

Route::group(['prefix' => 'api/v1'], function () {
    Route::post('/apilogin', 'AccountController@apilogin');
});

Then from the Postman Chrome Extension, I have below info for Headers

Cache-Control →no-cache, private
Connection →close
Content-Type →text/html; charset=UTF-8
Date →Mon, 23 May 2016 05:20:56 GMT
Server →Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.14
Transfer-Encoding →chunked
X-Powered-By →PHP/5.6.14

Body info is like below

{"UserName": "Pankaj"}

When I press Send: I get below Error

TokenMismatchException in VerifyCsrfToken.php line 67:

Am I missing something?



via Chebli Mohamed

Laravel fetch latest record based on created_at datetime field

In my E-Commerce web application, I am trying to implement International Shipping Feature. The relationship is like:

One international shipping zone hasMany Countries and a Country belongsTo a shippig zone.

One international shipping zone hasMany Shipping rate applied and one Shipping Rate belongsTo one Shipping Zone.

Now what I want to trying to do is to fetch the all the shipping rates that are created in descending order. Uptil now, I can fetch it correctly without any issue. Now here comes the twist. If there is one shipping rate having the same shipping rate code in the table, fetch the latest record and ignore the previous record, based on the country that will be selected by the user on the front end.

The code that I have tried:

$shippingCountryId = session()->get('new_shipping_country');
$shippingCountryAmount = session()->get('new_shipping_amount');

if ($shippingCountryId !== null) {
    $shippingAll = ShippingCountry::find($shippingCountryId)
                                    ->zone->rates()
                                    ->latest()->get();
} else {
    $shippingAll = ShippingCountry::where('name', 'India')->first()
                                    ->zone->rates()
                                    ->latest()->get();
}

The above code fetches all the records from the rates table for that shipping zone when the user selects the shipping country, which I don't want. I want only the latest one to be fetched if there is more than one record found with the same code.

Collection methods didn't work for me:

unique(), contains(), search(), filter()

I know I am pretty sure that I must be making some silly mistake, but I cannot find out what mistake and where.

Kindly help me out with this. I am trying to solve this since 2 days, but could not succeed yet.

Any help is highly appreciated. Thank you in advance.



via Chebli Mohamed

Laravel 5.1 - Foreach data in form (Blade)

i have a problem to show colors avaible of my products, i tryed to show them with blade foreach, but it doesnt work. My resource controller:

public function show($id){

        $colors = Color::where('product_id', $id)->orderBy('id', 'asc');

        $product = Product::where('id', $id)->first();

        return view('store.show', compact('product','colors')); 

    }

This is my table color, i added correctly the relations enter image description here Product Model:

namespace dixard;

use Illuminate\Database\Eloquent\Model;

use dixard\User;

use dixard\Category;

use dixard\Gender;

use dixard\OrderItem;

use dixard\Color;

class Product extends Model
{

    protected $table = 'products';

    protected $fillable = 

    [
    'name',
    'slug',
    'description',
    'extract',
    'image',
    'visible',
    'price',
    'category_id',
    'gender_id',
    'user_id'

    ];



    // Colleghiamo OGNI prodotto ha un utente
    public function user() {
            return $this->belongsTo('dixard\User');



    }

    // Colleghiamo OGNI prodotto ha una categoria
    public function category() {
            return $this->belongsTo('dixard\Category');



    }

    public function gender() {
            return $this->belongsTo('dixard\Gender');



    }

    // prodotto è contenuto in TANTI order item
    public function OrderItem() {
            return $this->belongsTo('dixard\OrderItem');

    }

    // prodotto è contenuto in TANTI order item
    public function Color() {
            return $this->belongsTo('dixard\Color');

    }

}

Color Model

namespace dixard;

use Illuminate\Database\Eloquent\Model;

class Color extends Model
{
    protected $table = 'colors';


    // gli dico che voglio scrivere questo campi
    protected $fillable = [

    'color',
    'product_id',



    ];
    public $timestamps = false;

    // Ogni color HA tanti prodotti. // Ogni prodotto ha tanti colori
    public function products() {

        return $this->hasMany('dixard\Product');

    }
}

I'm trying to show the color avaible for my product so:

    <label for="p_color">Color</label>


     @foreach($colors as $color)
 <td></td>
     @endforeach

This is only test! I would like show a select option, i tryed to use BLADE but it doesnt work,

-get all colors where product_id = $id work fine.

-get the product where id = $id work fine.

-I think the problem is the code blade(foreach) to show all colors avaible for my product.

how can i resolve it? Thank your help!



via Chebli Mohamed

Can not install laravel

I can not install Laravel in this way:

php composer.phar create-project laravel/laravel blog --prefer-dist ~5.0
php composer.phar create-project laravel/laravel blog --prefer-dist 5.0
php composer.phar create-project laravel/laravel blog --prefer-dist
php composer.phar create-project laravel/laravel blog

Response in console:

Could not find package laravel\laravel with stability stable.

or

Could not find package laravel\laravel with version ~5.0.

I'm trying do this in another way like that:

 php laravel new blog

Project doest not exist after crafting project or I have info in console:

cURL error 7: Failed to connect to 192.241.224.13 port 80: Timed out

What is wrong with this stupid composer ? :(



via Chebli Mohamed

Making a visitor tracker in laravel 5.1

I've currently installed a plugin (http://ift.tt/1oa6jQo)

Wich does the tracking thing, only I want to get unique visitors. I reall don't know how to extract those from this package. Or how I should make one by my own.

I want them to return as a json object per month.

If someone could help me out with this?

I tried it using the tracker_sessions table, but that doesn't work well.

Route::get('admin/api', function(){

        $stats = DB::table('tracker_sessions')
          ->groupBy('created_at')
          ->orderBy('created_at', 'ASC')
          ->get([
            DB::raw('created_at as y'),
            DB::raw('COUNT(*) as b')
          ]);

          return json_encode($stats);
    });

That returns something like this:

[{"y":"2016-05-22 21:17:17","b":1},{"y":"2016-05-22 21:17:27","b":1},{"y":"2016-05-22 21:17:28","b":2},{"y":"2016-05-22 21:17:29","b":1},{"y":"2016-05-22 21:17:31","b":1},{"y":"2016-05-22 21:17:33","b":1},{"y":"2016-05-22 21:18:10","b":1},{"y":"2016-05-22 21:18:11","b":2},{"y":"2016-05-22 21:18:13","b":1}]

Wich is not good at all...

Can someone please help me out?

Thanks!



via Chebli Mohamed

laravel 5.1 route direct to public folder

this is my route to access index.php ....http://localhost/abc/public/ if i write http://localhost/abc/ it does pick public bydefault

i want to write .htaccess file to direct http://localhost/abc call to http://localhost/abc/public/ so my url will not hurt..

in short i want http://localhost/abc/ and by dont even change directory struture of laravel.



via Chebli Mohamed