Created
July 26, 2020 23:40
-
-
Save Daudongit/1b7e2797e59348712f4a9fab7f742b02 to your computer and use it in GitHub Desktop.
Simple Transfer Snippets in laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//transfer\routes\api.php | |
<?php | |
use Illuminate\Http\Request; | |
use Illuminate\Database\Eloquent\Model; | |
class Balance extends Model{ | |
protected $fillable = ['account_nr','balance']; | |
} | |
class Transaction extends Model{ | |
protected $fillable = ['reference','amount','account_nr']; | |
} | |
// Balance::create([ | |
// 'account_nr'=>'1234567890', | |
// 'balance'=>'27654967' | |
// ]); | |
// Balance::create([ | |
// 'account_nr'=>'0987654321', | |
// 'balance'=>'2653965' | |
// ]); | |
Route::post('/transfer',function(Request $request){ | |
DB::transaction(function ()use($request) { | |
$sender = Balance::where('account_nr',$request->from)->first(); | |
$sender->balance = $sender->balance - $request->amount; | |
$sender->save(); | |
$receiver = Balance::where('account_nr',$request->to)->first(); | |
$receiver->balance = $receiver->balance - $request->amount; | |
$receiver->save(); | |
Transaction::create([ | |
'reference'=>"receive_from_{$request->from}_".md5(time()), | |
'amount'=>$request->amount, | |
'account_nr'=>$request->to | |
]); | |
Transaction::create([ | |
'reference'=>"send_to_{$request->to}_".md5(time()), | |
'amount'=>$request->amount, | |
'account_nr'=>$request->from | |
]); | |
}); | |
return response()->json(['sucess'=>true]); | |
})->name('transfer'); | |
//transfer\database\migrations\2020_07_24_213850_create_transaction_table.php | |
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateTransactionTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('transactions', function (Blueprint $table) { | |
$table->string('reference')->unique(); | |
$table->string('amount'); | |
$table->string('account_nr'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('transactions'); | |
} | |
} | |
//transfer\database\migrations\2020_07_24_213444_create_balance_table.php | |
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateBalanceTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('balances', function (Blueprint $table) { | |
$table->string('account_nr')->unique(); | |
$table->string('balance'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('balances'); | |
} | |
} | |
//Test | |
//curl -XPOST -d"from=1234567890&to=0987654321&amount=4000" http://localhost:9000/api/transfer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment