
Content
- Laravel E-Commerce with Alipay Dualfun Develope Part I - Creating Database Objects
- Laravel E-Commerce with Alipay Dualfun Develope Part II - Creating Models
- Laravel E-Commerce with Alipay Dualfun Develope Part III - Views and Routes
- Laravel E-Commerce with Alipay Dualfun Develope Part IV - Controllers
- Laravel E-Commerce with Alipay Dualfun Develope Part V - Alipay Payment API
This file should be saved as app/controllers/ProductController.php
<?php
class ProductController extends BaseResource
{
/**
* Resource view directory
* @var string
*/
protected $resourceView = 'account.product';
/**
* Model name of the resource, after initialization to a model instance
* @var string|Illuminate\Database\Eloquent\Model
*/
protected $model = 'Product';
/**
* Resource identification
* @var string
*/
protected $resource = 'myproduct';
/**
* Resource database tables
* @var string
*/
protected $resourceTable = 'products';
/**
* Resource name
* @var string
*/
protected $resourceName = 'Goods';
/**
* Custom validation message
* @var array
*/
protected $validatorMessages = array(
'title.required' => 'Please fill goods name',
'price.required' => 'Please fill goods price',
'price.numeric' => 'Price only be a number',
'quantity.required' => 'Please fill remaining quantity of goods',
'quantity.integer' => 'Remaining quantity of goods must be an integer',
'province.required' => 'Please select province and city',
'content.required' => 'Please fill content',
'category.exists' => 'Please choose goods category',
);
/**
* Resource list view
* GET /resource
* @return Response
*/
public function index()
{
// Get sort conditions
$orderColumn = Input::get('sort_up', Input::get('sort_down', 'created_at'));
$direction = Input::get('sort_up') ? 'asc' : 'desc' ;
// Get search conditions
switch (Input::get('target')) {
case 'title':
$title = Input::get('like');
break;
}
// Construct query statement
$query = $this->model->orderBy($orderColumn, $direction)->where('user_id', Auth::user()->id)->paginate(15);
isset($title) AND $query->where('title', 'like', "%{$title}%");
$datas = $query;
return View::make($this->resourceView.'.index')->with(compact('datas'));
}
/**
* Resource create view
* GET /resource/create
* @return Response
*/
public function create()
{
if( Auth::user()->alipay == NULL ){
return Redirect::route('account.settings')
->with('info', 'Notice: You need to set Alipay account before sale goods');
} else {
$categoryLists = ProductCategories::lists('name', 'id');
return View::make($this->resourceView.'.create')->with(compact('categoryLists'));
}
}
/**
* Resource create action
* POST /resource
* @return Response
*/
public function store()
{
// Get all form data.
$data = Input::all();
// Create validation rules
$unique = $this->unique();
$rules = array(
'title' => 'required|'.$unique,
'price' => 'required|numeric',
'quantity' => 'required|integer',
'content' => 'required',
'category' => 'exists:product_categories,id',
'province' => 'required',
);
$slug = Input::input('title');
$hashslug = date('H.i.s').'-'.md5($slug).'.html';
// Custom validation message
$messages = $this->validatorMessages;
// Begin verification
$validator = Validator::make($data, $rules, $messages);
if ($validator->passes()) {
// Verification success
// Add recource
$model = $this->model;
$model->user_id = Auth::user()->id;
$model->category_id = $data['category'];
$model->title = e($data['title']);
$model->province = e($data['province']);
$model->city = e($data['city']);
$model->price = e($data['price']);
$model->quantity = e($data['quantity']);
$model->slug = $hashslug;
$model->content = e($data['content']);
$model->meta_title = e($data['title']);
$model->meta_description = e($data['title']);
$model->meta_keywords = e($data['title']);
if ($model->save()) {
// Add success
return Redirect::back()
->with('success', '<strong>'.$this->resourceName.'post success</strong>');
} else {
// Add fail
return Redirect::back()
->withInput()
->with('error', '<strong>'.$this->resourceName.'add fail</strong>');
}
} else {
// Verification fail
return Redirect::back()->withInput()->withErrors($validator);
}
}
/**
* Resource edit view
* GET /resource/{id}/edit
* @param int $id
* @return Response
*/
public function edit($id)
{
$data = $this->model->find($id);
$categoryLists = ProductCategories::lists('name', 'id');
$product = Product::where('slug', $data->slug)->first();
return View::make($this->resourceView.'.edit')->with(compact('data', 'categoryLists', 'product'));
}
/**
* Resource edit action
* PUT/PATCH /resource/{id}
* @param int $id
* @return Response
*/
public function update($id)
{
// Get all form data.
$data = Input::all();
// Create validation rules
$rules = array(
'title' => 'required',
'content' => 'required',
'price' => 'required|numeric',
'quantity' => 'required|integer',
'category' => 'exists:product_categories,id',
'province' => 'required',
);
// Custom validation message
$messages = $this->validatorMessages;
// Begin verification
$validator = Validator::make($data, $rules, $messages);
if ($validator->passes()) {
// Verification success
// Update resource
$model = $this->model->find($id);
$model->user_id = Auth::user()->id;
$model->category_id = $data['category'];
$model->title = e($data['title']);
$model->province = e($data['province']);
$model->city = e($data['city']);
$model->price = e($data['price']);
$model->quantity = e($data['quantity']);
$model->content = e($data['content']);
$model->meta_title = e($data['title']);
$model->meta_description = e($data['title']);
$model->meta_keywords = e($data['title']);
if ($model->save()) {
// Update success
return Redirect::back()
->with('success', '<strong>'.$this->resourceName.'update success');
} else {
// Update fail
return Redirect::back()
->withInput()
->with('error', '<strong>'.$this->resourceName.'update fail</strong>');
}
} else {
// Verification fail
return Redirect::back()->withInput()->withErrors($validator);
}
}
/**
* Resource destory action
* DELETE /resource/{id}
* @param int $id
* @return Response
*/
public function destroy($id)
{
$data = $this->model->find($id);
if (is_null($data))
return Redirect::back()->with('error', 'Can\'t'.$this->resourceName);
elseif ($data)
{
$model = $this->model->find($id);
$thumbnails = $model->thumbnails;
File::delete(public_path('uploads/product_thumbnails/'.$thumbnails));
$data->delete();
return Redirect::back()->with('success', $this->resourceName.'delete success');
}
else
return Redirect::back()->with('warning', $this->resourceName.'delete fail');
}
/**
* Action: Add resource images
* @return Response
*/
public function postUpload($id)
{
$input = Input::all();
$rules = array(
'file' => 'image|max:3000',
);
$validation = Validator::make($input, $rules);
if ($validation->fails())
{
return Response::make($validation->errors->first(), 400);
}
$file = Input::file('file');
$destinationPath = 'uploads/products/';
$ext = $file->guessClientExtension(); // Get real extension according to mime type
$fullname = $file->getClientOriginalName(); // Client file name, including the extension of the client
$hashname = date('H.i.s').'-'.md5($fullname).'.'.$ext; // Hash processed file name, including the real extension
$picture = Image::make($file->getRealPath());
// crop the best fitting ratio and resize image
$picture->fit(1024, 683)->save(public_path($destinationPath.$hashname));
$picture->fit(585, 347)->save(public_path('uploads/product_thumbnails/'.$hashname));
$model = $this->model->find($id);
$oldThumbnails = $model->thumbnails;
$model->thumbnails = $hashname;
$model->save();
File::delete(public_path('uploads/product_thumbnails/'.$oldThumbnails));
$models = new ProductPictures;
$models->filename = $hashname;
$models->product_id = $id;
$models->user_id = Auth::user()->id;
$models->save();
if( $models->save() ) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
/**
* Action: Delete resource images
* @return Response
*/
public function deleteUpload($id)
{
// Only allows you to share pictures on the cover of the current resource being deleted
$filename = ProductPictures::where('id', $id)->where('user_id', Auth::user()->id)->first();
$oldImage = $filename->filename;
if (is_null($filename))
return Redirect::back()->with('error', 'Can\'t find picture');
elseif ($filename->delete()) {
File::delete(
public_path('uploads/products/'.$oldImage)
);
return Redirect::back()->with('success', 'Delete success');
}
else
return Redirect::back()->with('warning', 'Delete fail');
}
/**
* View: My comments
* @return Response
*/
public function comments()
{
$comments = ProductComment::where('user_id', Auth::user()->id)->paginate(15);
return View::make($this->resourceView.'.comments')->with(compact('comments'));
}
/**
* Action: Delete my comments
* @return Response
*/
public function deleteComment($id)
{
// Delete operations only allow comments to yourself
$comment = ProductComment::where('id', $id)->where('user_id', Auth::user()->id)->first();
if (is_null($comment))
return Redirect::back()->with('error', 'Can\'t find that comments');
elseif ($comment->delete())
return Redirect::back()->with('success', 'Delete success');
else
return Redirect::back()->with('warning', 'Delete fail');
}
/**
* View: Product
* @return Respanse
*/
public function getIndex()
{
$product = Product::orderBy('created_at', 'desc')->where('quantity', '>', '0')->paginate(12);
$categories = ProductCategories::orderBy('sort_order')->paginate(6);
return View::make('product.index')->with(compact('product', 'categories', 'data'));
}
/**
* Resource list
* @return Respanse
*/
public function category($category_id)
{
$product = Product::where('category_id', $category_id)->orderBy('created_at', 'desc')->paginate(6);
$categories = ProductCategories::orderBy('sort_order')->get();
$current_category = ProductCategories::where('id', $category_id)->first();
return View::make('product.category')->with(compact('product', 'categories', 'category_id', 'current_category'));
}
/**
* Resource show view
* @param string $slug Slug
* @return response
*/
public function show($slug)
{
$product = Product::where('slug', $slug)->first();
is_null($product) AND App::abort(404);
$categories = ProductCategories::orderBy('sort_order')->get();
if (Auth::check())
{
$inCart = ShoppingCart::where('buyer_id', Auth::user()->id)->where('product_id', $product->id)->first();
} else {
$inCart = false;
}
return View::make('product.show')->with(compact('product', 'categories', 'inCart'));
}
/**
* View: Customer shopping cart
* @return Response
*/
public function cart()
{
// Get sort conditions
$orderColumn = Input::get('sort_up', Input::get('sort_down', 'created_at'));
$direction = Input::get('sort_up') ? 'asc' : 'desc' ;
// Get search conditions
switch (Input::get('target')) {
case 'title':
$title = Input::get('like');
break;
}
// Construct query statement
$query = ShoppingCart::orderBy($orderColumn, $direction)->where('buyer_id', Auth::user()->id)->paginate(15);
isset($title) AND $query->where('title', 'like', "%{$title}%");
$datas = $query;
$payment = ShoppingCart::where('buyer_id', Auth::user()->id)->sum('payment');
$resource = 'myproduct';
$resourceName = 'Shopping cart';
return View::make($this->resourceView.'.cart')->with(compact('datas', 'resource', 'resourceName', 'payment'));
}
/**
* Action: Delete goods in customer shopping cart
* @return Response
*/
public function destroyGoods($id)
{
$data = ShoppingCart::find($id);
if (is_null($data))
return Redirect::back()->with('error', 'Can\'t find'.$this->resourceName);
elseif ($data)
{
$data->delete();
return Redirect::back()->with('success', $this->resourceName.'Delete success');
}
else
return Redirect::back()->with('warning', $this->resourceName.'Delete fail');
}
/**
* Action: Show page post action
* @return Response
*/
public function postAction($slug)
{
$postComment = e(Input::get('postComment'));
if($postComment)
{
// Get comment
$content = e(Input::get('content'));
// Check word
if (mb_strlen($content)<3)
return Redirect::back()->withInput()->withErrors($this->messages->add('content', 'Too short'));
// Find article
$product = Product::where('slug', $slug)->first();
// Create comment
$comment = new ProductComment;
$comment->content = $content;
$comment->product_id = $product->id;
$comment->user_id = Auth::user()->id;
if ($comment->save()) {
// Create success
// Updated comments
$product->comments_count = $product->comments->count();
$product->save();
// Return success
return Redirect::back()->with('success', 'Post success');
} else {
// Create fail
return Redirect::back()->withInput()->with('error', 'Post fail');
}
} else {
$data = Input::all();
$rules = array(
'quantity' => 'required|integer',
'product_id' => 'required',
'price' => 'required',
'seller_id' => 'required',
'inventory' => 'required',
);
if (e($data['inventory'])<e($data['quantity'])) {
return Redirect::back()
->with('error', '<strong>Please fill correct'.$this->resourceName.'quantity</strong>');
} elseif (Auth::user()->id==e($data['seller_id'])) {
return Redirect::back()
->with('error', '<strong>You can\'t by it, because yourself on sale</strong>');
} else {
// Custom validation message
$messages = $this->validatorMessages;
// Begin verification
$validator = Validator::make($data, $rules, $messages);
if ($validator->passes()) {
// Verification success
// Add recource
$model = new ShoppingCart;
$model->buyer_id = Auth::user()->id;
$model->quantity = e($data['quantity']);
$model->product_id = e($data['product_id']);
$model->price = e($data['price']);
$model->payment = e($data['quantity']) * e($data['price']);
$model->seller_id = e($data['seller_id']);
$model->save();
if ($model->save()) {
// Add success
return Redirect::back()
->with('success', '<strong>'.$this->resourceName.'add success</strong>');
} else {
// Add fail
return Redirect::back()
->withInput()
->with('error', '<strong>'.$this->resourceName.'add fail</strong>');
}
} else {
// Verification fail
return Redirect::back()->withInput()->withErrors($validator);
}
}
}
}
// ...
}
This file should be saved as app/controllers/ProductOrderController.php
<?php
class ProductOrderController extends BaseController
{
/**
* Resource view directory
* @var string
*/
protected $resourceView = 'account.order';
/**
* Model name of the resource, after initialization to a model instance
* @var string|Illuminate\Database\Eloquent\Model
*/
protected $model = 'ProductOrder';
/**
* Resource identification
* @var string
*/
protected $resource = 'order';
/**
* Resource database tables
* @var string
*/
protected $resourceTable = 'product_orders';
/**
* Resource name
* @var string
*/
protected $resourceName = 'Order';
protected $validatorMessages = array(
'customer_name.required' => 'Please fill your name',
'customer_address.required' => 'Please fill your address',
'customer_phone.required' => 'Please fill your mobile phone number',
'customer_phone.numeric' => 'Please fill correct mobile phone number',
);
/**
* View: Customer order index
* @return Response
*/
public function index()
{
// Get sort conditions
$orderColumn = Input::get('sort_up', Input::get('sort_down', 'created_at'));
$direction = Input::get('sort_up') ? 'asc' : 'desc' ;
// Get search conditions
switch (Input::get('target')) {
case 'title':
$title = Input::get('like');
break;
}
// Construct query statement
$unpayment_order = ProductOrder::orderBy($orderColumn, $direction)->where('customer_id', Auth::user()->id)->where('is_payment', 0)->paginate(15);
$payment_order = ProductOrder::orderBy($orderColumn, $direction)->where('customer_id', Auth::user()->id)->where('is_payment', 1)->where('is_checkout', 0)->paginate(15);
$checkout_order = ProductOrder::orderBy($orderColumn, $direction)->where('customer_id', Auth::user()->id)->where('is_checkout', 1)->paginate(15);
isset($title) AND $query->where('title', 'like', "%{$title}%");
$resourceName = 'Order';
$resource = 'order';
return View::make($this->resourceView.'.index')->with(compact('unpayment_order', 'payment_order', 'checkout_order', 'resourceName', 'resource'));
}
/**
* View: Customer order details
* @return Response
*/
public function customerOrderDetails($id)
{
$data = ProductOrder::where('customer_id', Auth::user()->id)->where('id', $id)->first();
$resourceName = 'Order';
$resource = 'order';
return View::make($this->resourceView.'.customerOrderDetails')->with(compact('data', 'resourceName', 'resource'));
}
/**
* View: Seller order details
* @return Response
*/
public function sellerOrderDetails($id)
{
$data = ProductOrder::where('seller_id', Auth::user()->id)->where('id', $id)->first();
$resourceName = 'Order';
$resource = 'order';
return View::make($this->resourceView.'.sellerOrderDetails')->with(compact('data', 'resourceName', 'resource'));
}
/**
* View: Generate order details
* @return Response
*/
public function order($id)
{
$data = ShoppingCart::where('buyer_id', Auth::user()->id)->where('id', $id)->first();
$resourceName = 'Order';
$resource = 'order';
$product_id = Input::input('product_id');
$product = Product::where('id', $data->product_id)->first()->title;
$seller = User::where('id', $data->seller_id)->first()->nickname;
return View::make($this->resourceView.'.order')->with(compact('data', 'resourceName', 'resource', 'product_id', 'product', 'seller'));
}
/**
* Action: Payment after add goods in shopping cart
* @return Response
*/
public function payment()
{
$resourceName = 'Order';
$resource = 'order';
// Get all form data.
$data = Input::all();
$rules = array(
'product_id' => 'required|',
'customer_name' => 'required',
'customer_address' => 'required',
'customer_phone' => 'required|numeric',
);
// Custom validation message
$messages = array(
'customer_name.required' => 'Please fill your name',
'customer_address.required' => 'Please fill your address',
'customer_phone.required' => 'Please fill your mobile phone number',
'customer_phone.numeric' => 'Please fill correct mobile phone number',
);
// Begin verification
$validator = Validator::make($data, $rules, $messages);
// Save user real name
if ( Auth::user()->username == NULL ) {
$user = Auth::user();
$user->username = Input::get('customer_name');
$user->save();
};
// Save user mobile phone number
if ( Auth::user()->phone == NULL ) {
$user = Auth::user();
$user->phone = Input::get('customer_phone');
$user->save();
};
// Vrification Success
if ($validator->passes()) {
$product_id = Input::input('product_id');
$product = Product::where('id', $product_id)->first();
$data = ShoppingCart::where('buyer_id', Auth::user()->id)->where('product_id', $product_id)->first();
$order_id = md5(date('his')).$product_id.Auth::user()->id;
$seller_id = $data->seller_id;
$seller_alipay = User::where('id', $seller_id)->first()->alipay;
$order_name = 'Payment:'.$product->title;
$payment = $data->payment;
$goods_show = 'http://www.example.com/product/'.$product->slug;
$customer_name = Input::input('customer_name');
$customer_address = Input::input('customer_address');
$customer_phone = Input::input('customer_phone');
// Create product order
$product_order = new ProductOrder;
$product_order->order_id = $order_id;
$product_order->seller_id = $seller_id;
$product_order->product_id = $product_id;
$product_order->customer_id = Auth::user()->id;
$product_order->customer_address = $customer_address;
$product_order->quantity = $data->quantity;
$product_order->price = $data->price;
$product_order->payment = $payment;
$product_order->save();
// Destroy goods in shopping cart
$data->delete();
// Alipay API
require_once( app_path('api/alipay/alipay.config.php' ));
require_once( app_path('api/alipay/lib/alipay_submit.class.php' ));
// Request parameters
$payment_type = "1"; // Payment type (required, don't modify)
$notify_url = route('order.tradeNotify'); // Server asynchronous notification page URL (start with http://, don't use http://localhost/ or add ?id=123)
$return_url = route('order.tradeReturn'); // Synchronization notification page URL (start with http://, don't use http://localhost/ or add ?id=123)
$seller_email = $seller_alipay; // Saller Alipay ID (required)
$out_trade_no = $order_id; // Order ID (required)
$subject = $order_name; // Order name (required)
$price = $payment; // Order payment (required)
$quantity = "1"; // Goods quantity (default is 1)
$logistics_fee = "0.00"; // Express payment (required)
$logistics_type = "EXPRESS"; // Express type: EXPRESS, POST or EMS
$logistics_payment = "SELLER_PAY"; // Express payment type (require:SELLER_PAY customer pay or BUYER_PAY saller pay)
$body = $goods_show; // Order describe
$show_url = $goods_show; // Goods show page (URL start with http://)
$receive_name = $customer_name; // Customer name
$receive_address = $customer_address; // Customer address
$receive_zip = NULL; // Customer zip (code such as:123456)
$receive_phone = NULL; // Custome telephone number (such as:0571-88158090)
$receive_mobile = $customer_phone; // Customer mobile phone numer (such as:13312341234)
// Constructs an array of arguments to request, no need to change
$parameter = array(
"service" => "trade_create_by_buyer",
"partner" => trim($alipay_config['partner']),
"payment_type" => $payment_type,
"notify_url" => $notify_url,
"return_url" => $return_url,
"seller_email" => $seller_email,
"out_trade_no" => $out_trade_no,
"subject" => $subject,
"price" => $price,
"quantity" => $quantity,
"logistics_fee" => $logistics_fee,
"logistics_type" => $logistics_type,
"logistics_payment" => $logistics_payment,
"body" => $body,
"show_url" => $show_url,
"receive_name" => $receive_name,
"receive_address" => $receive_address,
"receive_zip" => $receive_zip,
"receive_phone" => $receive_phone,
"receive_mobile" => $receive_mobile,
"_input_charset" => trim(strtolower($alipay_config['input_charset']))
);
// Establish require
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "Payment");
echo $html_text;
}
else{
return Redirect::back()->withInput()->withErrors($validator);
}
}
/**
* Action: Payment for order in customer order list
* @return Response
*/
public function rePayment()
{
$resourceName = 'Order';
$resource = 'order';
// Get all form data.
$data = ProductOrder::where('id', Input::get('order_id'))->first();
if ($data) {
$product_id = $data->product_id;
$product = Product::where('id', $product_id)->first();
$order_id = $data->order_id;
$seller_id = $data->seller_id;
$seller_alipay = User::where('id', $seller_id)->first()->alipay;
$order_name = 'Payment:'.$product->title;
$payment = $data->payment;
$goods_show = 'http://www.example.com/product/'.$product->slug;
$customer_name = Auth::user()->username;
$customer_address = $data->customer_address;
$customer_phone = Auth::user()->phone;
// Alipay API
require_once( app_path('api/alipay/alipay.config.php' ));
require_once( app_path('api/alipay/lib/alipay_submit.class.php' ));
// Request parameters
$payment_type = "1"; // Payment type (required, don't modify)
$notify_url = route('order.tradeNotify'); // Server asynchronous notification page URL (start with http://, don't use http://localhost/ or add ?id=123)
$return_url = route('order.tradeReturn'); // Synchronization notification page URL (start with http://, don't use http://localhost/ or add ?id=123)
$seller_email = $seller_alipay; // Saller Alipay ID (required)
$out_trade_no = $order_id; // Order ID (required)
$subject = $order_name; // Order name (required)
$price = $payment; // Order payment (required)
$quantity = "1"; // Goods quantity (default is 1)
$logistics_fee = "0.00"; // Express payment (required)
$logistics_type = "EXPRESS"; // Express type: EXPRESS, POST or EMS
$logistics_payment = "SELLER_PAY"; // Express payment type (require:SELLER_PAY customer pay or BUYER_PAY saller pay)
$body = $goods_show; // Order describe
$show_url = $goods_show; // Goods show page (URL start with http://)
$receive_name = $customer_name; // Customer name
$receive_address = $customer_address; // Customer address
$receive_zip = NULL; // Customer zip (code such as:123456)
$receive_phone = NULL; // Custome telephone number (such as:0571-88158090)
$receive_mobile = $customer_phone; // Customer mobile phone numer (such as:13312341234)
// Constructs an array of arguments to request, no need to change
$parameter = array(
"service" => "trade_create_by_buyer",
"partner" => trim($alipay_config['partner']),
"payment_type" => $payment_type,
"notify_url" => $notify_url,
"return_url" => $return_url,
"seller_email" => $seller_email,
"out_trade_no" => $out_trade_no,
"subject" => $subject,
"price" => $price,
"quantity" => $quantity,
"logistics_fee" => $logistics_fee,
"logistics_type" => $logistics_type,
"logistics_payment" => $logistics_payment,
"body" => $body,
"show_url" => $show_url,
"receive_name" => $receive_name,
"receive_address" => $receive_address,
"receive_zip" => $receive_zip,
"receive_phone" => $receive_phone,
"receive_mobile" => $receive_mobile,
"_input_charset" => trim(strtolower($alipay_config['input_charset']))
);
// Establish require
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "Payment");
echo $html_text;
}
else{
return Redirect::back()->with('error', 'Can\'t find'.$this->resourceName.'。');
}
}
/**
* Action: Delete customer order
* @return Response
*/
public function destroyOrder($id)
{
$data = ProductOrder::find($id);
if (is_null($data))
return Redirect::back()->with('error', 'Can\'t find'.$this->resourceName.'。');
elseif ($data)
{
$data->delete();
return Redirect::back()->with('success', $this->resourceName.'Delete success');
}
else
return Redirect::back()->with('warning', $this->resourceName.'Delete fail');
}
/**
* Action: Aplipay trade return
* @return Response
*/
public function tradeReturn()
{
// Alipay Dualfun API
require_once( app_path('api/alipay/alipay.config.php' ));
require_once( app_path('api/alipay/lib/alipay_notify.class.php' ));
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyReturn();
if($verify_result) {
$out_trade_no = $_GET['out_trade_no']; // Order ID
$trade_no = $_GET['trade_no']; // Alipay order ID
$trade_status = $_GET['trade_status']; // Alipay trade status
$product_order = ProductOrder::where('order_id', $out_trade_no)->first();
$product_order->is_payment = true;
$product_order->alipay_trade = $trade_no;
$product_order->save();
$product = Product::where('id', $product_order->product_id)->first();
$product->quantity = $product->quantity - $product_order->quantity;
$product->save();
return Redirect::route('order.customerOrderDetails', $product_order->id)->with('success', 'Payment success');
} else {
return Redirect::route('order.index')->with('error', 'Payment fail');
}
}
/**
* Action: Aplipay trade notify
* @return Response
*/
public function tradeNotify()
{
require_once( app_path('api/alipay/alipay.config.php' ));
require_once( app_path('api/alipay/lib/alipay_notify.class.php' ));
// Get verification result
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyNotify();
if($verify_result) {
$out_trade_no = $_GET['out_trade_no']; // Order ID
$trade_no = $_GET['trade_no']; // Alipay order ID
$trade_status = $_GET['trade_status']; // Alipay trade status
$product_order = ProductOrder::where('order_id', $out_trade_no)->first();
$product_order->is_payment = true;
$product_order->alipay_trade = $trade_no;
$product_order->save();
$product = Product::where('id', $product_order->product_id)->first();
$product->quantity = $product->quantity - $product_order->quantity;
$product->save();
} else {
// Verification fail
return Redirect::route('order.index')->with('error', 'Payment fail');
}
}
/**
* View: Seller order list
* @return Response
*/
public function seller()
{
// Get sort conditions
$orderColumn = Input::get('sort_up', Input::get('sort_down', 'created_at'));
$direction = Input::get('sort_up') ? 'asc' : 'desc' ;
// Get search conditions
switch (Input::get('target')) {
case 'title':
$title = Input::get('like');
break;
}
// Construct query statement
$trading_order = ProductOrder::orderBy($orderColumn, $direction)->where('seller_id', Auth::user()->id)->where('is_payment', 1)->where('is_checkout', 0)->paginate(15);
$checkout_order = ProductOrder::orderBy($orderColumn, $direction)->where('seller_id', Auth::user()->id)->where('is_checkout', 1)->paginate(15);
isset($title) AND $query->where('title', 'like', "%{$title}%");
$resourceName = 'Order';
$resource = 'order';
return View::make($this->resourceView.'.seller')->with(compact('trading_order', 'checkout_order', 'resourceName', 'resource'));
}
/**
* Action: Seller send goods with express
* @return Response
*/
public function sendGoods()
{
// Get all form data.
$data = Input::all();
$rules = array(
'id' => 'required|',
'express_name' => 'required',
'invoice_no' => 'required',
);
// Custom validation message
$messages = array(
'express_name.required' => 'Please fill express company name',
'invoice_no.required' => 'Please fill express billing number',
);
// Begin verification
$validator = Validator::make($data, $rules, $messages);
if ($validator->passes()) {
$product_order = ProductOrder::find(Input::get('id'));
$product_order->is_express = true;
$product_order->express_name = Input::get('express_name');
$product_order->invoice_no = Input::get('invoice_no');
$product_order->save();
// Alipay Dualfun API
require_once( app_path('api/alipay/alipay.config.php' ));
require_once( app_path('api/alipay/lib/alipay_submit.class.php' ));
$trade_no = ProductOrder::where('id', Input::get('id'))->first()->alipay_trade; // Alipay trade number (required)
$logistics_name = Input::get('express_name'); // Express company name (required)
$invoice_no = Input::get('invoice_no'); // Express billing number
$transport_type = "EXPRESS"; // Express type: POST, EXPRESS or EMS
// Constructs an array of arguments to request, no need to change
$parameter = array(
"service" => "send_goods_confirm_by_platform",
"partner" => trim($alipay_config['partner']),
"trade_no" => $trade_no,
"logistics_name" => $logistics_name,
"invoice_no" => $invoice_no,
"transport_type" => $transport_type,
"_input_charset" => trim(strtolower($alipay_config['input_charset']))
);
// Establish require
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestHttp($parameter);
$doc = new DOMDocument();
$doc->loadXML($html_text);
return Redirect::back()->with('success', '<strong>Send goods success</strong>');
} else {
return Redirect::back()->withInput()->withErrors($validator);
}
}
/**
* Action: Customer checkout order
* @return Response
*/
public function checkout()
{
if (Input::get('id')) {
$product_order = ProductOrder::find(Input::get('id'));
$product_order->is_checkout = true;;
$product_order->save();
return Redirect::back()->with('success', 'Success, enjoy it!');
} else {
return Redirect::back()->with('error', 'Fail');
}
}
// ...
}
This file should be saved as app/controllers/Admin/ProductCategoriesResource.php
<?php
class Admin_ProductResource extends BaseResource
{
/**
* Resource view directory
* @var string
*/
protected $resourceView = 'admin.product';
/**
* Model name of the resource, after initialization to a model instance
* @var string|Illuminate\Database\Eloquent\Model
*/
protected $model = 'Product';
/**
* Resource identification
* @var string
*/
protected $resource = 'product';
/**
* Resource database tables
* @var string
*/
protected $resourceTable = 'products';
/**
* Resource name
* @var string
*/
protected $resourceName = 'Goods';
/**
* Custom validation message
* @var array
*/
protected $validatorMessages = array(
'title.required' => 'Please fill goods name',
'price.required' => 'Please fill goods price',
'price.numeric' => 'Goods price must be a number',
'quantity.required' => 'Please fill quantity of goods',
'quantity.integer' => 'Quantity of good must be a integer',
'province.required' => 'Please select province and city',
'content.required' => 'Please fill content of goods',
'category.exists' => 'Please select a category of this goods',
);
/**
* Resource list view
* GET /resource
* @return Response
*/
public function index()
{
// Get sort conditions
$orderColumn = Input::get('sort_up', Input::get('sort_down', 'created_at'));
$direction = Input::get('sort_up') ? 'asc' : 'desc' ;
// Get search conditions
switch (Input::get('target')) {
case 'title':
$title = Input::get('like');
break;
}
// Construct query statement
$query = $this->model->orderBy($orderColumn, $direction);
isset($title) AND $query->where('title', 'like', "%{$title}%");
$datas = $query->paginate(15);
return View::make($this->resourceView.'.index')->with(compact('datas'));
}
/**
* Resource create view
* GET /resource/create
* @return Response
*/
public function create()
{
if(Auth::user()->alipay==NULL){
return Redirect::route('account.settings')
->with('info', 'Notice: you neet to set Alipay account befor salle goods at here');
} else {
$categoryLists = ProductCategories::lists('name', 'id');
return View::make($this->resourceView.'.create')->with(compact('categoryLists'));
}
/**
* Resource create action
* POST /resource
* @return Response
*/
public function store()
{
// Get all form data.
$data = Input::all();
// Create validation rules
$unique = $this->unique();
$rules = array(
'title' => 'required|'.$unique,
'price' => 'required|numeric',
'quantity' => 'required|integer',
'content' => 'required',
'category' => 'exists:product_categories,id',
'province' => 'required',
);
$slug = Input::input('title');
$hashslug = date('H.i.s').'-'.md5($slug).'.html';
// Custom validation message
$messages = $this->validatorMessages;
// Begin verification
$validator = Validator::make($data, $rules, $messages);
if ($validator->passes()) {
// Verification success
// Add recource
$model = $this->model;
$model->user_id = Auth::user()->id;
$model->category_id = $data['category'];
$model->title = e($data['title']);
$model->province = e($data['province']);
$model->city = e($data['city']);
$model->price = e($data['price']);
$model->quantity = e($data['quantity']);
$model->slug = $hashslug;
$model->content = e($data['content']);
$model->meta_title = e($data['title']);
$model->meta_description = e($data['title']);
$model->meta_keywords = e($data['title']);
if ($model->save()) {
// Add success
return Redirect::back()
->with('success', '<strong>'.$this->resourceName.'post success</strong>');
} else {
// Add fail
return Redirect::back()
->withInput()
->with('error', '<strong>'.$this->resourceName.'post fail</strong>');
}
} else {
// Verification fail
return Redirect::back()->withInput()->withErrors($validator);
}
}
/**
* Resource edit view
* GET /resource/{id}/edit
* @param int $id
* @return Response
*/
public function edit($id)
{
$data = $this->model->find($id);
$categoryLists = ProductCategories::lists('name', 'id');
$product = Product::where('slug', $data->slug)->first();
return View::make($this->resourceView.'.edit')->with(compact('data', 'categoryLists', 'product'));
}
/**
* Resource edit action
* PUT/PATCH /resource/{id}
* @param int $id
* @return Response
*/
public function update($id)
{
// Get all form data.
$data = Input::all();
// Create validation rules
$rules = array(
'title' => 'required',
'content' => 'required',
'slug' => 'required|'.$this->unique('slug', $id),
'category' => 'exists:product_categories,id',
'province' => 'required',
);
$model = $this->model->find($id);
$oldSlug = $model->slug;
// Custom validation message
$messages = $this->validatorMessages;
// Begin verification
$validator = Validator::make($data, $rules, $messages);
if ($validator->passes()) {
// Verification success
// Update resource
$model = $this->model->find($id);
$model->user_id = Auth::user()->id;
$model->category_id = $data['category'];
$model->title = e($data['title']);
$model->province = e($data['province']);
$model->city = e($data['city']);
$model->slug = e($data['slug']);
$model->content = e($data['content']);
$model->meta_title = e($data['title']);
$model->meta_description = e($data['title']);
$model->meta_keywords = e($data['title']);
if ($model->save()) {
// Update success
return Redirect::back()
->with('success', '<strong>'.$this->resourceName.'update success</strong>');
} else {
// Update fail
return Redirect::back()
->withInput()
->with('error', '<strong>'.$this->resourceName.'update fail</strong>');
}
} else {
// Verification fail
return Redirect::back()->withInput()->withErrors($validator);
}
}
/**
* Resource destory action
* DELETE /resource/{id}
* @param int $id
* @return Response
*/
public function destroy($id)
{
$data = $this->model->find($id);
if (is_null($data))
return Redirect::back()->with('error', 'Can\'t find '.$this->resourceName.'。');
elseif ($data)
{
$model = $this->model->find($id);
$thumbnails = $model->thumbnails;
File::delete(public_path('uploads/product_thumbnails/'.$thumbnails));
$data->delete();
return Redirect::back()->with('success', $this->resourceName.'Delete success');
}
else
return Redirect::back()->with('warning', $this->resourceName.'Delete fail');
}
/**
* Action: Add resource images
* @return Response
*/
public function postUpload($id)
{
$input = Input::all();
$rules = array(
'file' => 'image|max:3000',
);
$validation = Validator::make($input, $rules);
if ($validation->fails())
{
return Response::make($validation->errors->first(), 400);
}
$file = Input::file('file');
$destinationPath = 'uploads/products/';
$ext = $file->guessClientExtension(); // Get real extension according to mime type
$fullname = $file->getClientOriginalName(); // Client file name, including the extension of the client
$hashname = date('H.i.s').'-'.md5($fullname).'.'.$ext; // Hash processed file name, including the real extension
$picture = Image::make($file->getRealPath());
// crop the best fitting ratio and resize image
$picture->fit(1024, 683)->save(public_path($destinationPath.$hashname));
$picture->fit(585, 347)->save(public_path('uploads/product_thumbnails/'.$hashname));
$model = $this->model->find($id);
$oldThumbnails = $model->thumbnails;
$model->thumbnails = $hashname;
$model->save();
File::delete(public_path('uploads/product_thumbnails/'.$oldThumbnails));
$models = new ProductPictures;
$models->filename = $hashname;
$models->product_id = $id;
$models->user_id = Auth::user()->id;
$models->save();
if( $models->save() ) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
/**
* Action: Delete resource images
* @return Response
*/
public function deleteUpload($id)
{
// Only allows you to share pictures on the cover of the current resource being deleted
$filename = ProductPictures::where('id', $id)->where('user_id', Auth::user()->id)->first();
$oldImage = $filename->filename;
if (is_null($filename))
return Redirect::back()->with('error', 'Can\'t find picture');
elseif ($filename->delete()) {
File::delete(
public_path('uploads/products/'.$oldImage)
);
return Redirect::back()->with('success', 'Delete success');
}
else
return Redirect::back()->with('warning', 'Delete fail');
}
// ...
}