Skip to content

Commit

Permalink
remove migration file and all usage of Order Model (making package in…
Browse files Browse the repository at this point in the history
…dependent), add localization to response messages, add custom exceptions, and make all classes use PaymentInterface as a contract
  • Loading branch information
omar-ehab committed Sep 27, 2022
1 parent a726b08 commit 23f6d41
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 265 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Payment Helper of Payment Gateways ( PayPal - Paymob - Fawry - Thawani - WeAccept - Kashier - Hyperpay )
![payment-gateways.png](https://github.com/nafezly/payments/blob/master/payment-gateways.png?raw=true)


## Supported gateways

- [PayPal](https://paypal.com/)
Expand Down Expand Up @@ -84,7 +83,9 @@ return [

];
```

## Put keys in .env File

```php


Expand Down Expand Up @@ -132,7 +133,8 @@ Route::get('/payments/verify/{payment?}',[FrontController::class,'payment_verify
## How To Use

```jsx
use Nafezly\Payments\ThawaniPayment;
use
Nafezly\Payments\ThawaniPayment;

$payment = new PaymobPayment();
//pay
Expand All @@ -146,12 +148,12 @@ $payment->verify($request);

```php

$payment = new \Nafezly\Payments\FawryPayment();
$payment = new \Nafezly\Payments\HyperPayPayment();
$payment = new \Nafezly\Payments\KashierPayment();
$payment = new \Nafezly\Payments\PaymobPayment();
$payment = new \Nafezly\Payments\PayPalPayment();
$payment = new \Nafezly\Payments\ThawaniPayment();
$payment = new \Nafezly\Payments\Classes\FawryPayment();
$payment = new \Nafezly\Payments\Classes\HyperPayPayment();
$payment = new \Nafezly\Payments\Classes\KashierPayment();
$payment = new \Nafezly\Payments\Classes\PaymobPayment();
$payment = new \Nafezly\Payments\Classes\PayPalPayment();
$payment = new \Nafezly\Payments\Classes\ThawaniPayment();
```

## Test Cards
Expand Down
6 changes: 3 additions & 3 deletions config/nafezly-payments.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
return [

#PAYMOB
'PAYMOB_API_KEY' => env('PAYMOB_API_KEY'),
'PAYMOB_INTEGRATION_ID' => env('PAYMOB_INTEGRATION_ID'),
Expand Down Expand Up @@ -38,9 +38,9 @@


#THAWANI
'THAWANI_API_KEY' => env('THAWANI_API_KEY', 'rRQ26GcsZzoEhbrP2HZvLYDbn9C9et'),
'THAWANI_API_KEY' => env('THAWANI_API_KEY', ''),
'THAWANI_URL' => env('THAWANI_URL', "https://uatcheckout.thawani.om/"),
'THAWANI_PUBLISHABLE_KEY' => env('THAWANI_PUBLISHABLE_KEY', 'HGvTMLDssJghr9tlN9gr4DVYt0qyBy'),
'THAWANI_PUBLISHABLE_KEY' => env('THAWANI_PUBLISHABLE_KEY', ''),


'verify_route_name' => "verify-payment"
Expand Down
42 changes: 0 additions & 42 deletions database/migrations/2030_09_29_154000_update_orders_table.php

This file was deleted.

7 changes: 7 additions & 0 deletions resources/lang/ar/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'PAYMENT_DONE' => 'تمت العملية بنجاح',
'PAYMENT_FAILED' => 'حدث خطأ أثناء تنفيذ العملية',
'PAYMENT_FAILED_WITH_CODE' => 'حدث خطأ أثناء تنفيذ العملية ، كود الخطأ : :CODE',
];
7 changes: 7 additions & 0 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'PAYMENT_DONE' => 'operation completed successfully',
'PAYMENT_FAILED' => 'An error occurred while executing the operation',
'PAYMENT_FAILED_WITH_CODE' => 'An error occurred while executing the operation, error code: :CODE',
];
85 changes: 45 additions & 40 deletions src/FawryPayment.php → src/Classes/FawryPayment.php
Original file line number Diff line number Diff line change
@@ -1,95 +1,100 @@
<?php

namespace Nafezly\Payments;
namespace Nafezly\Payments\Classes;

use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Nafezly\Payments\Exceptions\MissingPaymentInfoException;
use Nafezly\Payments\Interfaces\PaymentInterface;

class FawryPayment
class FawryPayment implements PaymentInterface
{
private $fawry_url;
private $fawry_secret;
private $fawry_merchant;
private $verify_route_name;

public function __construct()
{
$this->fawry_url = config('nafezly-payments.FAWRY_URL');
$this->fawry_merchant = config('nafezly-payments.FAWRY_MERCHANT');
$this->fawry_secret = config('nafezly-payments.FAWRY_SECRET');
$this->verify_route_name = config('nafezly-payments.verify_route_name');
}


/**
* @param Order $order
* @param $amount
* @param null $user_id
* @param null $user_name
* @param null $user_email
* @param null $user_phone
* @param null $source
* @return string[]
* @throws MissingPaymentInfoException
*/
public function pay(Order $order): array
public function pay($amount, $user_id = null, $user_name = null, $user_email = null, $user_phone = null, $source = null): array
{
if (is_null($user_id)) throw new MissingPaymentInfoException('user_id', 'FAWRY');
if (is_null($user_name)) throw new MissingPaymentInfoException('user_name', 'FAWRY');
if (is_null($user_email)) throw new MissingPaymentInfoException('user_email', 'FAWRY');
if (is_null($user_phone)) throw new MissingPaymentInfoException('user_phone', 'FAWRY');

$unique_id = uniqid();

$data = [
'fawry_url' => config('nafezly-payments.FAWRY_URL'),
'fawry_merchant' => config('nafezly-payments.FAWRY_MERCHANT'),
'fawry_secret' => config('nafezly-payments.FAWRY_SECRET'),
'user_id' => auth()->user()->id,
'user_name' => auth()->user()->name,
'user_email' => auth()->user()->email,
'user_phone' => isset(auth()->user()->phone) ? auth()->user()->phone : "01234567890",
'fawry_url' => $this->fawry_url,
'fawry_merchant' => $this->fawry_merchant,
'fawry_secret' => $this->fawry_secret,
'user_id' => $user_id,
'user_name' => $user_name,
'user_email' => $user_email,
'user_phone' => $user_phone,
'unique_id' => $unique_id,
'item_id' => 1,
'item_quantity' => 1,
'amount' => $order->amount,
'amount' => $amount,
];

$secret = $data['fawry_merchant'] . $data['unique_id'] . $data['user_id'] . $data['item_id'] . $data['item_quantity'] . $data['amount'] . $data['fawry_secret'];
$data['secret'] = $secret;
$order->update(['payment_id' => $unique_id]);

return ['html' => $this->generate_html($order, $data)];
return ['payment_id' => $unique_id, 'html' => $this->generate_html($data)];

}

/**
* @param Request $request
* @return array|void
*/
public function verify(Request $request)
{
$response = json_decode($request['chargeResponse'], true);
$reference_id = $response['merchantRefNumber'];
$res = json_decode($request['chargeResponse'], true);
$reference_id = $res['merchantRefNumber'];

$hash = hash('sha256', $this->fawry_merchant . $reference_id . $this->fawry_secret);
$order = Order::where('payment_id', $reference_id)
->where('status', 'PENDING')
->firstOrFail();
$response = Http::get($this->fawry_url . 'ECommerceWeb/Fawry/payments/status/v2?merchantCode=' . $this->fawry_merchant . '&merchantRefNumber=' . $reference_id . '&signature=' . $hash);

if ($response->offsetGet('statusCode') == 200 && $response->offsetGet('paymentStatus') == "PAID" && (double)$order->amount == (double)$response->offsetGet('paymentAmount')) {
$response = Http::get($this->fawry_url . 'ECommerceWeb/Fawry/payments/status/v2?merchantCode=' . $this->fawry_merchant . '&merchantRefNumber=' . $reference_id . '&signature=' . $hash);

Order::where('payment_id', $reference_id)->where('status', 'PENDING')->update([
'status' => "DONE",
'process_data' => json_encode($request->all())
]);
if ($response->offsetGet('statusCode') == 200 && $response->offsetGet('paymentStatus') == "PAID") {
return [
'success' => true,
'message' => "تمت العملية بنجاح",
'order' => $order
'message' => __('messages.PAYMENT_DONE'),
'process_data' => $request->all()
];

} else if ($response->offsetGet('statusCode') != 200) {
Order::where('payment_id', $reference_id)->where('status', 'PENDING')->update([
'status' => "FAILED",
'process_data' => json_encode($request->all())
]);
return [
'success' => true,
'message' => "تمت العملية بنجاح",
'order' => $order
'success' => false,
'message' => __('messages.PAYMENT_FAILED'),
'process_data' => $request->all()
];
}
}

public function generate_html(Order $order, $data): string
private function generate_html($data): string
{
return "<link rel='stylesheet' href='https://atfawry.fawrystaging.com/atfawry/plugin/assets/payments/css/fawrypay-payments.css'><script type='text/javascript' src='" . $data['fawry_url'] . "atfawry/plugin/assets/payments/js/fawrypay-payments.js'></script><script>
let chargeRequest = {};
const chargeRequest = {};
chargeRequest.language= 'ar-eg';
chargeRequest.merchantCode= '" . $data['fawry_merchant'] . "';
chargeRequest.merchantRefNumber= '" . $data['payment_id'] . "';
Expand All @@ -102,15 +107,15 @@ public function generate_html(Order $order, $data): string
chargeRequest.order.description = 'Credit';
chargeRequest.order.expiry = '';
chargeRequest.order.orderItems = [];
let item = {};
const item = {};
item.productSKU =1;
item.description ='Credit';
item.price =" . $data['amount'] . ";
item.quantity =" . $data['item_quantity'] . ";
chargeRequest.order.orderItems.push(item);
chargeRequest.signature = '" . $data['secret'] . "';
setTimeout(function(){
FawryPay.checkout(chargeRequest,'" . route(config('nafezly-payments.verify_route_name'), ['payment' => "fawry"]) . "','" . route(config('nafezly-payments.verify_route_name'), ['payment' => "fawry"]) . "');
FawryPay.checkout(chargeRequest,'" . route($this->verify_route_name, ['payment' => "fawry"]) . "','" . route($this->verify_route_name, ['payment' => "fawry"]) . "');
},100);
</script>";
}
Expand Down
Loading

0 comments on commit 23f6d41

Please sign in to comment.