Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
WpStarter committed Mar 17, 2023
1 parent afa3851 commit eb051f3
Show file tree
Hide file tree
Showing 32 changed files with 903 additions and 185 deletions.
27 changes: 27 additions & 0 deletions app/Admin/Controllers/MomoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Admin\Controllers;

use App\Banking\Api\Momo;
use App\Banking\Api\Momo\MomoApi;
use WpStarter\Support\Carbon;
use WpStarter\Wordpress\Admin\Facades\Notice;

class MomoController extends Controller
{
function getIndex(){
$momo=MomoApi::make();
$config=$momo->config();
return ws_view('admin::momo',[
'phone'=>$config->phone,
'name'=>$config->name,
'balance'=>wc_price($config->balance,['currency'=>'VND']),
'updated_at'=>$config->logged_in_at?Carbon::createFromTimestamp($config->logged_in_at)->tz(wp_timezone()):''
]);
}
function postUpdate(){
Notice::success('Cập nhật thành công');
MomoApi::make()->reloadUserData(true);
return ws_redirect()->back();
}
}
26 changes: 26 additions & 0 deletions app/Admin/Controllers/TransactionsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Admin\Controllers;

use App\Admin\ListTable\TransactionsListTable;
use WpStarter\Http\Request;
use WpStarter\Wordpress\Admin\Facades\Notice;

class TransactionsController
{
function index(){
$table=new TransactionsListTable();
$table->prepare_items();
return ws_view('admin::table',['table'=>$table]);
}
function getAdd(){

}
function postAdd(){

}
function getDelete(Request $request){
Notice::success('Deleted');
return ws_redirect()->back();
}
}
53 changes: 53 additions & 0 deletions app/Admin/ListTable/TransactionsListTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Admin\ListTable;

use App\Banking\Model\BankTransaction;

class TransactionsListTable extends \WP_List_Table
{
function get_bulk_actions() {

$actions = array(
'delete' => 'Delete'
);
return $actions;
}
public function get_columns()
{
return [
'cb'=>'cb',
'id'=>'ID',
'tid'=>'Transaction ID',
'bank'=>'Bank',
'content'=>'Content',
'amount'=>'Amount',
'prefix'=>'Prefix',
'order_id'=>'Order',
'status'=>'Status',
'created_at'=>'Created At',
'received_at'=>'Received At',
'notified_at'=>'Notified At',
];
}
public function prepare_items()
{
$this->items=BankTransaction::query()
->latest('created_at')
->paginate(15,'*','paged');
$this->set_pagination_args(array(
'total_items' => $this->items->total(),
'total_pages' => $this->items->lastPage(),
'per_page' => $this->items->perPage(),
));
}
function column_cb($item){
$checkbox = '<label class="screen-reader-text" for="transaction_' . $item->id . '">' . sprintf( __( 'Select %s'), $item->id ) . '</label>'
. "<input type='checkbox' name='transactions[]' id='transaction_{$item->id}' value='{$item->id}' />";
return $checkbox;
}
protected function column_default($item, $column_name)
{
return $item->$column_name;
}
}
30 changes: 30 additions & 0 deletions app/Admin/resources/views/momo.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@extends('admin::layout')
@section('content')
<h3>Thông tin tài khoản</h3>
@if($phone)
<table>
<tr>
<td>Số điện thoại</td>
<td>{{$phone}}</td>
</tr>
<tr>
<td>Tên tài khoản</td>
<td>{{$name}}</td>
</tr>
<tr>
<td>Số dư</td>
<td>{!! $balance !!}</td>
</tr>
<tr>
<td>Cập nhật</td>
<td>{{$updated_at}}</td>
</tr>
</table>
<form method="POST">
@csrf
<button name="action" value="update" class="button button-primary">Cập nhật</button>
</form>
@else
<p>Chưa kết nối momo</p>
@endif
@endsection
9 changes: 9 additions & 0 deletions app/Admin/resources/views/table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@extends('admin::layout')
@section('content')
{{$table->views()}}
<form>
<input type="hidden" name="page" value="{{ws_request('page')}}">
{{$table->search_box( 'Search', 's' )}}
{{$table->display()}}
</form>
@endsection
12 changes: 4 additions & 8 deletions app/Admin/routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

use WpStarter\Wordpress\Admin\Facades\Route;

Route::add('ws-admin-sample',\App\Admin\Controllers\SampleAdminController::class)->position(2)
->group(function (){
Route::add('ws-admin-sample-child-1',\App\Admin\Controllers\SampleAdminController::class);
});
Route::parent('ws-admin-sample')->middleware(\App\Admin\Middleware\SampleMiddleware::class)->group(function(){
Route::add('ws-admin-sample-child-2',\App\Admin\Controllers\SampleAdminController::class);
Route::add('ws-admin-sample-child-3',\App\Admin\Controllers\SampleAdminController::class);
Route::add('ws-admin-sample-child-4',\App\Admin\Controllers\SampleAdminController::class);
Route::add('momo',\App\Admin\Controllers\MomoController::class)->position(30)->group(function(){
Route::add('transactions',\App\Admin\Controllers\TransactionsController::class)
->resource()->position(30);
});

11 changes: 11 additions & 0 deletions app/Banking/Api/BankApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ abstract class BankApi
{
protected $config;
protected $prefixes;
protected $hasNewTransaction=false;
public function __construct($config)
{
$this->config=$config;
}
public function setHasNewTransaction($status=true){
$this->hasNewTransaction=$status;
return $this;
}
public function gotNewTransaction($transaction){

}
public function transactionsUpdated(){

}
protected function getPrefixes(){
if(!$this->prefixes){
Expand Down
58 changes: 57 additions & 1 deletion app/Banking/Api/Momo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,67 @@

namespace App\Banking\Api;

use App\Banking\Api\Momo\MomoApi;
use Carbon\Carbon;
use WpStarter\Support\Str;

class Momo extends BankApi
{

function transactionsUpdated()
{
if($this->hasNewTransaction){
MomoApi::make()->reloadUserData(true);
}
}

public function getTransactions()
{
// TODO: Implement getTransactions() method.
$momo=new MomoApi();
$list=$momo->getNotifications();
if(!is_array($list)){
$list=[];
}
$collect=ws_collect($list);
$receivedKeywords=['Nhận tiền','Receive'];
$transactions=$collect->filter(function($tran)use($receivedKeywords){
$caption=ws_data_get($tran,'caption');
return Str::contains($caption,$receivedKeywords);
});
$transactions=$transactions->map(function($momo){
$extra=json_decode($momo['extra'],true);
$transaction=new Transaction();
$transaction->bank='momo';
$transaction->id=$momo['tranId'];
$transaction->amount=$extra['amount'];
$transaction->date=Carbon::createFromTimestamp($momo['updateTime']);
$transaction->content_raw=$momo['caption'].'('.($extra['partnerId']??'').'). '.$momo['body'];
$transaction->content=$extra['comment'];
$transaction->prefix='';
$transaction->order_id='';
$prefixes=$this->getPrefixes();
foreach ($prefixes as $prefixKey => $prefix) {
$content = Str::lower($transaction->content) . ' ';
$regex = Str::lower($prefix);
$tokens = [];
if (Str::contains($regex, '{orderno}')) {
$tokens[] = Str::replace('{orderno}', '(\d+) ', $regex);
$tokens[] = Str::replace('{orderno}', '(\d+)\-', $regex);
} else {
$tokens[] = $regex . '(\d+) ';
$tokens[] = $regex . '(\d+)\-';
}
foreach ($tokens as $token) {
$regex = '#' . $token . '#';
if (preg_match($regex, $content, $matches)) {
$transaction->order_id = $matches[1];
$transaction->prefix=$prefixKey;
break;
}
}
}
return $transaction;
});
return $transactions;
}
}
12 changes: 7 additions & 5 deletions app/Banking/Api/Momo/ApiMsg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Banking\Api\Momo;

use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Crypt\RSA;

trait ApiMsg
{
protected function USER_LOGIN_MSG()
Expand Down Expand Up @@ -267,15 +270,15 @@ private function CURL($Action,$header,$data)

$body = curl_exec($curl);
if(curl_errno($curl) != 0){

die ("Curl failed: ". curl_error($curl));
return null;
}
if(is_object(json_decode($body))){
return json_decode($body,true);
}
if($body){
return json_decode($this->encryptDecrypt($body,$this->app->token,'DECRYPT'),true);
}
return null;

}
protected function formatHeader($headers){
Expand Down Expand Up @@ -346,9 +349,8 @@ function encryptDecrypt($data, $key, $mode = 'ENCRYPT') {
}

function encryptAppKeyUsingPublicKey($public_key) {
$rsa = new \phpseclib\Crypt\RSA();
$rsa->setEncryptionMode(\phpseclib\Crypt\RSA::ENCRYPTION_PKCS1);
$rsa->loadKey($public_key);
$rsa=PublicKeyLoader::load($public_key);
$rsa=$rsa->withPadding(RSA::ENCRYPTION_PKCS1)->withHash('sha1')->withMGFHash('sha1');
return base64_encode($rsa->encrypt($this->app->token));
}
}
38 changes: 27 additions & 11 deletions app/Banking/Api/Momo/MomoApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class MomoApi
'TRANSACTION_HISTORY_LIST' => 'https://api.momo.vn/sync/transhis/browse',
'TRANSACTION_HISTORY_DETAIL' => 'https://api.momo.vn/sync/transhis/details',
);

public function __construct($phone)
public static function make($phone=null){
return new static($phone);
}
public function __construct($phone=null)
{
$this->config=Config::load($phone);
$this->config=Config::load();
$this->app=new App();
$this->init($phone);
}
Expand All @@ -39,7 +41,7 @@ function clearConfig(){

public function init($phone)
{
if($this->config->phone !== $phone){
if($phone && $this->config->phone !== $phone){
$this->config->clear();
$this->loadUserData($phone);
}
Expand Down Expand Up @@ -84,10 +86,6 @@ public function sendOTP()

}





public function setOTP($code)
{
$this->config->ohash = hash('sha256',$this->config->phone.$this->config->rkey.$code);
Expand Down Expand Up @@ -144,16 +142,34 @@ public function loginUser($password=null)
$this->config->auth_token_lifetime=$this->session_lifetime;
return $this->config;
}

function refreshAccessToken(){
dd($this->GENERATE_TOKEN_AUTH_MSG());
function reloadUserData($force=false){
$tokenLifetime=time()-$this->config->logged_in_at;
if($force || $tokenLifetime>$this->config->auth_token_lifetime) {
$this->loginUser();
}
return false;
}
function refreshAccessToken($force=false){
$tokenLifetime=time()-$this->config->logged_in_at;
if($force || $tokenLifetime>$this->config->auth_token_lifetime) {
$response = $this->REFRESH_TOKEN_MSG();
if ($accessToken = ws_data_get($response, 'momoMsg.accessToken')) {
$this->config->auth_token = $accessToken;
return true;
}
$this->config->logged_in_at=time();
}
return false;
}






public function getNotifications($from=null, $to=null, $limit=200)
{
$this->reloadUserData();
if(!$to){
$to=Carbon::now();
}
Expand Down
Loading

0 comments on commit eb051f3

Please sign in to comment.