Skip to content

Commit

Permalink
Merge pull request #11 from coralsio/delete-payment-method
Browse files Browse the repository at this point in the history
delete payment method
  • Loading branch information
saeed-corals authored Jun 24, 2024
2 parents 983a8d4 + 507c723 commit 8771682
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Classes/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function getMoneyISOCurrency($amount, $currencyCode = null)

$number = Number::fromString($amount);

$money = $moneyParser->parse((string)$number, $currency->getCode());
$money = $moneyParser->parse((string)$number, $currency);

// Check for a negative amount.
if (! $this->negativeAmountAllowed && $money->isNegative()) {
Expand Down
39 changes: 39 additions & 0 deletions src/Common/Http/Controllers/API/PaymentMethodsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php


namespace Corals\Modules\Payment\Common\Http\Controllers\API;

use Corals\Foundation\Http\Controllers\APIBaseController;
use Corals\Modules\Payment\Common\Http\Requests\PaymentMethodRequest;
use Corals\Modules\Payment\Common\Models\PaymentMethod;
use Corals\Modules\Payment\Common\Services\PaymentMethodService;


class PaymentMethodsController extends APIBaseController
{
/**
* PaymentMethodsController constructor.
* @param PaymentMethodService $paymentMethodService
*/
public function __construct(protected PaymentMethodService $paymentMethodService)
{
parent::__construct();
}

/**
* @param PaymentMethodRequest $request
* @param PaymentMethod $paymentMethod
* @return mixed
*/
public function destroy(PaymentMethodRequest $request, PaymentMethod $paymentMethod)
{
try {

$this->paymentMethodService->destroy($request, $paymentMethod);

return apiResponse([], trans('Corals::messages.success.deleted', ['item' => $paymentMethod->last_four]));
} catch (\Exception $e) {
return apiExceptionResponse($e);
}
}
}
49 changes: 49 additions & 0 deletions src/Common/Http/Requests/PaymentMethodRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Corals\Modules\Payment\Common\Http\Requests;

use Corals\Foundation\Http\Requests\BaseRequest;
use Corals\Modules\Payment\Common\Models\PaymentMethod;

class PaymentMethodRequest extends BaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
$this->setModel(PaymentMethod::class);

return $this->isAuthorized();
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = parent::rules();

if ($this->isUpdate() || $this->isStore()) {
$rules = array_merge($rules, [

]);
}

if ($this->isStore()) {
$rules = array_merge($rules, []);
}

if ($this->isUpdate()) {
$paymentMethod = $this->route('payment_method');
$rules = array_merge($rules, []);
}

return $rules;
}

}
3 changes: 2 additions & 1 deletion src/Common/Models/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Corals\Foundation\Models\BaseModel;
use Corals\Foundation\Traits\ModelPropertiesTrait;
use Corals\Foundation\Transformers\PresentableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;

class PaymentMethod extends BaseModel
{
use PresentableTrait, LogsActivity, ModelPropertiesTrait;
use PresentableTrait, LogsActivity, ModelPropertiesTrait, SoftDeletes;

protected $casts = [
'properties' => 'json',
Expand Down
51 changes: 51 additions & 0 deletions src/Common/Policies/PaymentMethodPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Corals\Modules\Payment\Common\Policies;

use Corals\Foundation\Policies\BasePolicy;
use Corals\Modules\Payment\Common\Models\PaymentMethod;
use Corals\User\Models\User;

class PaymentMethodPolicy extends BasePolicy
{
protected $administrationPermission = 'Administrations::admin.payment';

/**
* @param User $user
* @param PaymentMethod|null $paymentMethod
* @return bool
*/
public function view(User $user, PaymentMethod $paymentMethod = null)
{
return true;
}

/**
* @param User $user
* @return bool
*/
public function create(User $user)
{
return true;
}

/**
* @param User $user
* @param PaymentMethod $paymentMethod
* @return bool
*/
public function update(User $user, PaymentMethod $paymentMethod)
{
return true;
}

/**
* @param User $user
* @param PaymentMethod $paymentMethod
* @return bool
*/
public function destroy(User $user, PaymentMethod $paymentMethod)
{
return $paymentMethod->parent_id === $user->id;
}
}
9 changes: 9 additions & 0 deletions src/Common/Services/PaymentMethodService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Corals\Modules\Payment\Common\Services;

use Corals\Foundation\Services\BaseServiceClass;

class PaymentMethodService extends BaseServiceClass
{
}
5 changes: 3 additions & 2 deletions src/Common/Transformers/InvoiceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public function transform(Invoice $invoice)
$email = "";
$billing_address = "";

if ($invoice->hasProperty('billing_address')) {
$billing_details = $invoice->getProperty('billing_address');
$billing_details = $invoice->getProperty('billing_address');

if ($invoice->hasProperty('billing_address') && $billing_details) {
$customer = $billing_details['first_name'] . ' ' . $billing_details['last_name'];
$email = $billing_details['email'];
$billing_address = $invoice->display_address($billing_details);
Expand Down
1 change: 1 addition & 0 deletions src/Common/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@

Route::apiResource('tax-classes', 'TaxClassesController', ['as' => 'api.payment', 'only' => ['index']]);
Route::apiResource('tax-classes.taxes', 'TaxesController', ['as' => 'api.payment', 'only' => ['index']]);
Route::apiResource('payment-methods', 'PaymentMethodsController')->only('destroy');
3 changes: 3 additions & 0 deletions src/Providers/PaymentAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Corals\Modules\Payment\Common\Models\Currency;
use Corals\Modules\Payment\Common\Models\Invoice;
use Corals\Modules\Payment\Common\Models\PaymentMethod;
use Corals\Modules\Payment\Common\Models\Tax;
use Corals\Modules\Payment\Common\Models\TaxClass;
use Corals\Modules\Payment\Common\Models\Transaction;
use Corals\Modules\Payment\Common\Policies\CurrencyPolicy;
use Corals\Modules\Payment\Common\Policies\InvoicePolicy;
use Corals\Modules\Payment\Common\Policies\PaymentMethodPolicy;
use Corals\Modules\Payment\Common\Policies\TaxClassPolicy;
use Corals\Modules\Payment\Common\Policies\TaxPolicy;
use Corals\Modules\Payment\Common\Policies\TransactionPolicy;
Expand All @@ -28,6 +30,7 @@ class PaymentAuthServiceProvider extends ServiceProvider
Currency::class => CurrencyPolicy::class,
TaxClass::class => TaxClassPolicy::class,
Tax::class => TaxPolicy::class,
PaymentMethod::class => PaymentMethodPolicy::class
];

/**
Expand Down

0 comments on commit 8771682

Please sign in to comment.