|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TomatoPHP\FilamentPayments\Services\Drivers; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Cryptomus\Api\Client; |
| 7 | +use TomatoPHP\FilamentPayments\Models\Payment; |
| 8 | +use TomatoPHP\FilamentPayments\Services\Contracts\PaymentCurrency; |
| 9 | +use TomatoPHP\FilamentPayments\Services\Contracts\PaymentGateway; |
| 10 | + |
| 11 | + |
| 12 | +class Cryptomus extends Driver |
| 13 | +{ |
| 14 | + public static function process(Payment $payment): false|string |
| 15 | + { |
| 16 | + $gatewayData = $payment->gateway->gateway_parameters; |
| 17 | + |
| 18 | + $cryptomusGateway = Client::payment($gatewayData['payment_key'], $gatewayData['merchant_uuid']); |
| 19 | + |
| 20 | + try { |
| 21 | + $param = [ |
| 22 | + 'amount' => strval($payment->amount + $payment->charge), |
| 23 | + 'currency' => $payment->method_currency, |
| 24 | + 'order_id' => $payment->trx, |
| 25 | + 'url_return' => route('payment.cancel', $payment->trx), |
| 26 | + 'url_success' => route('payments.callback', 'Cryptomus') . "?session=$payment->trx", |
| 27 | + 'url_callback' => route('payments.callback', 'Cryptomus') . "?session=$payment->trx", |
| 28 | + 'is_payment_multiple' => true, |
| 29 | + 'lifetime' => '3600', |
| 30 | + 'is_refresh' => true, |
| 31 | + 'course_source' => 'Binance' |
| 32 | + ]; |
| 33 | + |
| 34 | + $response = $cryptomusGateway->create($param); |
| 35 | + } catch (\Exception $e) { |
| 36 | + $send['error'] = true; |
| 37 | + $send['message'] = $e->getMessage(); |
| 38 | + return json_encode($send); |
| 39 | + } |
| 40 | + |
| 41 | + if ($response && $response['status'] == 'check' ) { |
| 42 | + $send['redirect'] = $response['url']; |
| 43 | + $send['session'] = $response['uuid']; |
| 44 | + return json_encode($send); |
| 45 | + } else { |
| 46 | + $send['error'] = true; |
| 47 | + $send['message'] = ['message']; |
| 48 | + return json_encode($send); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static function verify(Request $request): \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
| 53 | + { |
| 54 | + $gatewayData = \TomatoPHP\FilamentPayments\Models\PaymentGateway::where('alias', 'Cryptomus')->orderBy('id', 'desc')->firstOrFail(); |
| 55 | + $gatewayParameter = $gatewayData->gateway_parameters; |
| 56 | + |
| 57 | + $sessionId = $request->get('session'); |
| 58 | + |
| 59 | + $payment = Payment::where('trx', $sessionId)->where('status', 0)->firstOrFail(); |
| 60 | + |
| 61 | + $cryptomusGateway = Client::payment($gatewayParameter['payment_key'], $gatewayParameter['merchant_uuid']); |
| 62 | + |
| 63 | + $data = ["order_id" => $sessionId]; |
| 64 | + |
| 65 | + $result = $cryptomusGateway->info($data); |
| 66 | + |
| 67 | + if ($result['is_final'] && $result['order_id'] && in_array($result['payment_status'], ['paid', 'paid_over'])) { |
| 68 | + self::paymentDataUpdate($payment); |
| 69 | + return redirect($payment->success_url); |
| 70 | + } |
| 71 | + |
| 72 | + self::paymentDataUpdate($payment, true); |
| 73 | + return redirect($payment->failed_url); |
| 74 | + } |
| 75 | + |
| 76 | + public function integration(): array |
| 77 | + { |
| 78 | + return PaymentGateway::make('Cryptomus') |
| 79 | + ->alias('Cryptomus') |
| 80 | + ->status(true) |
| 81 | + ->crypto(true) |
| 82 | + ->gateway_parameters([ |
| 83 | + "payment_key" => "", |
| 84 | + "merchant_uuid" => "" |
| 85 | + ]) |
| 86 | + ->supported_currencies([ |
| 87 | + PaymentCurrency::make('USD') |
| 88 | + ->symbol('$') |
| 89 | + ->rate(1) |
| 90 | + ->minimum_amount(1) |
| 91 | + ->maximum_amount(1000) |
| 92 | + ->fixed_charge(0.2) |
| 93 | + ->percent_charge(2) |
| 94 | + ->toArray() |
| 95 | + ]) |
| 96 | + ->toArray(); |
| 97 | + } |
| 98 | +} |
0 commit comments