Skip to content

Commit

Permalink
Merge pull request #2 from nuocgansoi/develop
Browse files Browse the repository at this point in the history
v2: + multiple shop; + helper; + amount exchange config;
  • Loading branch information
nuocgansoi authored Oct 31, 2017
2 parents 7db87c2 + 53c2df1 commit eedb5c4
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/Controllers/CanCreateOnepayResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private function validateOnepayResult(Request $request)
}
}
$stringHashData = trim($stringHashData, "&");
$secureHash = $this->secureHashEncode($stringHashData);
$secureHash = secureHashEncode($stringHashData);
if ($secureHash != $request->get('vpc_SecureHash')) {
return [
'success' => false,
Expand Down Expand Up @@ -99,4 +99,4 @@ private function getResponseDescription($responseCode)
}
return $result;
}
}
}
48 changes: 23 additions & 25 deletions src/Controllers/OnepayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,34 @@

class OnepayController extends Controller
{
public $itemModel;

use CanCreateOnepayResult;

public function __construct()
public function shop($model)
{
$this->itemModel = app(config('onepay.items.model'));
}
$shopInstance = getShopInstance($model);
if (!$shopInstance) return redirect('/');

$items = $shopInstance->all();

public function shop()
{
$items = $this->itemModel->all();
return view('onepay::shop', compact('items'));
}

public function pay(Request $request, $itemId)
public function pay(Request $request, $model, $itemId)
{
// Validate request
$item = $this->itemModel->find($itemId);
$shopInstance = getShopInstance($model);
if (!$shopInstance) return redirect('/');

$item = $shopInstance->find($itemId);
if (!$item) return redirect('/');

// Make hash data
$amount = $item->{config('onepay.items.price')} * 100;
$price = getPrice($item);
if (!$price) return "Check your config price of {$model}!!!";

$amount = price2Amount($price);
$ticketNo = $request->ip();
$hashData = OnepayPayment::makeHashData($amount, $ticketNo);
$hashData = OnepayPayment::makeHashData($model, $amount, $ticketNo, $request->get('order_info'));

// Encode secure hash
$stringHashData = '';
Expand All @@ -44,7 +47,7 @@ public function pay(Request $request, $itemId)
$stringHashData .= $key . '=' . $value . '&';
}
$stringHashData = trim($stringHashData, '&');
$secureHash = $this->secureHashEncode($stringHashData);
$secureHash = secureHashEncode($stringHashData);

$url .= '&vpc_SecureHash=' . $secureHash;

Expand All @@ -54,21 +57,16 @@ public function pay(Request $request, $itemId)
return redirect($url);
}

public function result(Request $request)
public function result(Request $request, $model)
{
$validator = $this->validateOnepayResult($request);
if (!$validator['success']) return $validator['message'];

OnepayResult::createFromRequest($request);

$message = $validator['message'];
$items = $this->itemModel->all();

return view('onepay::result', compact('message', 'items'));
}
$validator = $this->validateOnepayResult($request);
$view = $validator['success'] ? 'onepay::success' : 'onepay::failed';

private function secureHashEncode($stringHashData)
{
return strtoupper(hash_hmac('SHA256', $stringHashData, pack('H*', config('onepay.secure_secret'))));
return view($view, [
'model' => $model,
'message' => $validator['message']
]);
}
}
56 changes: 56 additions & 0 deletions src/Helpers/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: nuocgansoi
* Date: 10/31/2017
* Time: 2:06 PM
*/

/**
* @param $model
* @return \Illuminate\Foundation\Application|mixed|null
*/
function getShopInstance($model)
{
$model = strtolower($model);
$class = config("onepay.shop.{$model}.model");

return $class ? app($class) : null;
}

/**
* @param $model
* @return \Illuminate\Config\Repository|mixed
*/
function getPriceAttribute($model)
{
$model = strtolower($model);

return config("onepay.shop.{$model}.price");
}

/**
* @param $stringHashData
* @return string
*/
function secureHashEncode($stringHashData)
{
return strtoupper(hash_hmac('SHA256', $stringHashData, pack('H*', config('onepay.secure_secret'))));
}

/**
* @param $item
* @return null
*/
function getPrice($item)
{
$model = strtolower(class_basename($item));
$attr = getPriceAttribute($model);

return $attr ? $item->{$attr} : null;
}

function price2Amount($price)
{
return $price * config('onepay.amount_exchange');
}
5 changes: 3 additions & 2 deletions src/Models/OnepayPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OnepayPayment extends Model
{
protected $guarded = ['id'];

public static function makeHashData($amount, $ticketNo, $orderInfo = null)
public static function makeHashData($model, $amount, $ticketNo, $orderInfo = null)
{
$merchTxnRef = 'ONEPAY_' . round(microtime(true));
$orderInfo = $orderInfo ?? $merchTxnRef;
Expand All @@ -19,7 +19,7 @@ public static function makeHashData($amount, $ticketNo, $orderInfo = null)
'vpc_Command' => config('onepay.command'),
'vpc_Locale' => config('onepay.locale'),
'vpc_Merchant' => config('onepay.merchant_id'),
'vpc_ReturnURL' => config('onepay.return_url'),
'vpc_ReturnURL' => config('onepay.return_url') . '/' . strtolower($model),
'vpc_Version' => config('onepay.version'),
'vpc_Amount' => $amount,
'vpc_MerchTxnRef' => $merchTxnRef,
Expand All @@ -35,6 +35,7 @@ public static function createFromHashData($user, $item, $hashData, $secureHash,
{
return static::create([
'user_id' => $user->id,
'item_type' => get_class($item),
'item_id' => $item->id,
'access_code' => $hashData['vpc_AccessCode'],
'currency' => $hashData['vpc_Currency'],
Expand Down
2 changes: 2 additions & 0 deletions src/Providers/LaravelOnepayServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function boot()
*/
public function register()
{
include __DIR__ . '/../Helpers/functions.php';

$this->mergeConfigFrom(
__DIR__.'/../config/onepay.php', 'onepay'
);
Expand Down
11 changes: 7 additions & 4 deletions src/config/onepay.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
'locale' => env('ONEPAY_LOCALE', 'vn'),
'return_url' => env('ONEPAY_RETURN_URL', 'http://localhost'),
'title' => env('ONEPAY_TITLE', 'OnePay Gate'),
'items' => [
'model' => App\Item::class,
'price' => 'price',
],
'amount_exchange' => env('ONEPAY_AMOUNT_EXCHANGE', 100),
'shop' => [
'item' => [
'model' => App\Item::class,
'price' => 'price',
],
]
];
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function up()
Schema::create('onepay_payments', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('user_id')->nullable();
$table->string('item_type')->nullable();
$table->bigInteger('item_id')->nullable();
$table->string('access_code', 8);
$table->string('currency', 3);
Expand Down
6 changes: 3 additions & 3 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'namespace' => 'NuocGanSoi\LaravelOnepay\Controllers',
'middleware' => ['web', 'auth'],
], function () {
Route::get('onepay/pay/{item}', 'OnepayController@pay')->name('onepay.pay');
Route::get('onepay/shop', 'OnepayController@shop')->name('onepay.shop');
Route::get('onepay/result', 'OnepayController@result')->name('onepay.result');
Route::get('onepay/shop/{model}', 'OnepayController@shop')->name('onepay.shop');
Route::get('onepay/pay/{model}/{item}', 'OnepayController@pay')->name('onepay.pay');
Route::get('onepay/result/{model}', 'OnepayController@result')->name('onepay.result');
});
5 changes: 5 additions & 0 deletions src/views/failed.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@extends('onepay::result')

@section('message')
<div style="font-size: 40px; color: red;">{{ $message }}</div>
@endsection
97 changes: 93 additions & 4 deletions src/views/result.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,94 @@
@extends('onepay::shop')
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

@section('message')
<div style="font-size: 40px;">{{ $message }}</div>
@endsection
<title>{{ config('onepay.title') }}</title>

<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet"
type="text/css">

<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Raleway', sans-serif;
font-weight: 100;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 12px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
@if(Route::has('login'))
<div class="top-right links">
@auth
<a href="{{ url('/home') }}">Home</a>
@else
<a href="{{ route('login') }}">Login</a>
<a href="{{ route('register') }}">Register</a>
@endauth
</div>
@endif

@auth
<div class="content">
<div class="title m-b-md">
User id: {{ Auth::id() }}
@yield('message')
</div>
</div>
@else
<div><a href="{{ route('login') }}">Login</a></div>
<div><a href="{{ route('register') }}">Register</a></div>
@endauth
</div>
</body>
</html>
7 changes: 4 additions & 3 deletions src/views/shop.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@
<div class="content">
<div class="title m-b-md">
User id: {{ Auth::id() }}
@yield('message')
</div>
<div class="links">
@forelse($items as $item)
<a href="{{route('onepay.pay', $item)}}">
Item {{ $item->id }}: {{ $item->{config('onepay.items.price')} }}
<a href="{{route('onepay.pay', [class_basename($item), $item])}}">
{{ class_basename($item) }}
{{ $item->id }}:
{{ getPrice($item) }}
{{ config('onepay.currency') }}
</a>
@empty
Expand Down
5 changes: 5 additions & 0 deletions src/views/success.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@extends('onepay::result')

@section('message')
<div style="font-size: 40px; color: green;">{{ $message }}</div>
@endsection

0 comments on commit eedb5c4

Please sign in to comment.