Skip to content

Commit 1c6ab2e

Browse files
committed
m
1 parent 7a6150e commit 1c6ab2e

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

Payments.tar.gz

-344 KB
Binary file not shown.

config/nafezly-payments.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,18 @@
7272
'PAYTABS_CHECKOUT_LANG' => env('PAYTABS_CHECKOUT_LANG',"AR"),
7373
'PAYTABS_CURRENCY'=>env('PAYTABS_CURRENCY',"EGP"),
7474

75+
76+
#Binance
77+
'BINANCE_API'=>env('BINANCE_API'),
78+
'BINANCE_SECRET'=>env('BINANCE_SECRET'),
79+
80+
7581
'VERIFY_ROUTE_NAME' => "payment-verify",
7682
'APP_NAME'=>env('APP_NAME'),
7783

84+
85+
86+
7887

7988

8089
];

src/Classes/BinancePayment.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace Nafezly\Payments\Classes;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Http;
7+
8+
use Nafezly\Payments\Interfaces\PaymentInterface;
9+
use Nafezly\Payments\Classes\BaseController;
10+
11+
class BinancePayment extends BaseController implements PaymentInterface
12+
{
13+
public $binance_api;
14+
public $binance_secret;
15+
public $verify_route_name;
16+
17+
public function __construct()
18+
{
19+
$this->binance_api = config('nafezly-payments.BINANCE_API');
20+
$this->binance_secret = config('nafezly-payments.BINANCE_SECRET');
21+
$this->verify_route_name = config('nafezly-payments.VERIFY_ROUTE_NAME');
22+
}
23+
24+
25+
/**
26+
* @param $amount
27+
* @param null $user_id
28+
* @param null $user_first_name
29+
* @param null $user_last_name
30+
* @param null $user_email
31+
* @param null $user_phone
32+
* @param null $source
33+
* @return string[]
34+
* @throws MissingPaymentInfoException
35+
*/
36+
public function pay($amount = null, $user_id = null, $user_first_name = null, $user_last_name = null, $user_email = null, $user_phone = null, $source = null): array
37+
{
38+
$this->setPassedVariablesToGlobal($amount,$user_id,$user_first_name,$user_last_name,$user_email,$user_phone,$source);
39+
$required_fields = ['amount'];
40+
$this->checkRequiredFields($required_fields, 'BINANCE');
41+
42+
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
43+
$nonce = '';
44+
for($i=1; $i <= 32; $i++)
45+
{
46+
$pos = mt_rand(0, strlen($chars) - 1);
47+
$char = $chars[$pos];
48+
$nonce .= $char;
49+
}
50+
$ch = curl_init();
51+
52+
$uniqid= uniqid().rand(10000,99999);
53+
$timestamp = round(microtime(true) * 1000);
54+
// Request body
55+
$data = array(
56+
"env" => [
57+
"terminalType" => "APP"
58+
],
59+
"merchantTradeNo" => $uniqid,
60+
"orderAmount" => $this->amount,
61+
"currency" => "BUSD",
62+
"goods" => [
63+
"goodsType" => "02",
64+
"goodsCategory" => "Z000",
65+
"referenceGoodsId" => $uniqid,
66+
"goodsName" => "Credit",
67+
"goodsDetail" => "Credit"
68+
]
69+
);
70+
71+
72+
$json_request = json_encode($data);
73+
$payload = $timestamp."\n".$nonce."\n".$json_request."\n";
74+
$binance_pay_key = $this->binance_api;
75+
$binance_pay_secret = $this->binance_secret;
76+
$signature = strtoupper(hash_hmac('SHA512',$payload,$binance_pay_secret));
77+
$response = \Http::withHeaders([
78+
"Content-Type"=>"application/json",
79+
"BinancePay-Timestamp"=>$timestamp,
80+
"BinancePay-Nonce"=>$nonce,
81+
"BinancePay-Certificate-SN"=>$binance_pay_key,
82+
"BinancePay-Signature"=>$signature,
83+
])->post('https://bpay.binanceapi.com/binancepay/openapi/v3/order',$data)->json();
84+
85+
86+
if($response['status']== "SUCCESS")
87+
return [
88+
'payment_id'=>$uniqid,
89+
'html'=>$response['errorMessage'],
90+
'redirect_url'=>$response['checkoutUrl']
91+
];
92+
return [
93+
'payment_id'=>$uniqid,
94+
'html'=>$response['errorMessage'],
95+
'redirect_url'=>""
96+
];
97+
}
98+
99+
/**
100+
* @param Request $request
101+
* @return array|void
102+
*/
103+
public function verify(Request $request)
104+
{
105+
if ($request['status']=="SUCCESS") {
106+
return [
107+
'success' => true,
108+
'payment_id'=>"",
109+
'message' => __('nafezly::messages.PAYMENT_DONE'),
110+
'process_data' => $request->all()
111+
];
112+
} else {
113+
return [
114+
'success' => false,
115+
'payment_id'=>"",
116+
'message' => __('nafezly::messages.PAYMENT_FAILED'),
117+
'process_data' => $request->all()
118+
];
119+
}
120+
}
121+
122+
}

0 commit comments

Comments
 (0)