Skip to content
This repository has been archived by the owner on May 2, 2020. It is now read-only.

Commit

Permalink
Implement charging
Browse files Browse the repository at this point in the history
  • Loading branch information
suphon-t committed Aug 12, 2018
1 parent ab59988 commit d76b513
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 157 deletions.
42 changes: 40 additions & 2 deletions app/Http/Controllers/Api/ShopController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use OmiseCharge;

class ShopController extends Controller
{
Expand Down Expand Up @@ -77,8 +78,45 @@ public function makeOrder(Request $request) {
}

public function myOrder(Request $request) {
$user = $request->user();
return Order::mine($user->id)->firstOrFail();
$order = $this->getMyOrder($request);
if ($order->charge_id) {
$charge = OmiseCharge::retrieve($order->charge_id);
$order->charge = [
'status' => $charge['status'],
'authorized' => $charge['authorized'],
'reversed' => $charge['reversed'],
'paid' => $charge['paid'],
];
}
return $order;
}

public function chargeMyOrder(Request $request) {
$data = $request->all();
$validator = Validator::make($data, [
'token' => 'required',
]);

if ($validator->fails()) {
return response()->json(['error' => 'missing_token'], 400);
}

$order = $this->getMyOrder($request);

$charge = OmiseCharge::create([
'amount' => $order->total_price * 100,
'currency' => 'thb',
'card' => $data['token'],
]);

$order->charge_id = $charge['id'];
$order->save();

return ["true"];
}

private function getMyOrder(Request $request) {
return Order::mine($request->user()->id)->firstOrFail();
}

protected function createUser($email, $password)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"laravel/framework": "5.6.*",
"laravel/passport": "^6.0",
"laravel/tinker": "^1.0",
"paragonie/random_compat": "2.*"
"paragonie/random_compat": "2.*",
"omise/omise-php": "dev-master"
},
"require-dev": {
"filp/whoops": "^2.0",
Expand Down
Loading

0 comments on commit d76b513

Please sign in to comment.