| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dizatech\Transaction\Drivers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dizatech\Transaction\Abstracts\Driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Dizatech\Transaction\Models\Transaction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Facades\Log; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class Mahamax extends Driver | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function init($amount, $orderId, $callbackUrl, $detail = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         // Create new transaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $transaction = $this->createNewTransaction($orderId, $amount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $gateway_info = $this->getInformation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $base_url = $gateway_info['base_url']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $client = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             $respone = $client->get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                 $base_url . '/request', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                     'json'      => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                         'consumer'          => 'fabreso', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                         'order_id'          => $transaction->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                         'payment_gateway'   => 'sadad', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                         'amount'            => $amount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                         'callback_url'      => $callbackUrl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     'headers'   => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                         'accept'            => 'application/json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $result = json_decode($respone->getBody()->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             if (isset($result->request_id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 if (!isset($detail['auto_redirect']) || $detail['auto_redirect'] == true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     $transaction->token = $result->request_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     $transaction->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     header("Location: " . $base_url . "/start/" . $result->request_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 throw new Exception("Failed to retrieve request id from Mahamax for order id {$orderId}. details: " . json_encode($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |             Log::debug("Payment request for order id {$orderId} failed with message: " . $e->getMessage()); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function verify($request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (isset($request['status']) && $request['status'] == 'successful') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $gateway_info = $this->getInformation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $base_url = $gateway_info['base_url']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $client = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $response = $client->get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     $base_url . "/verify", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         'json'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                             'consumer'          => 'fabreso', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                             'request_id'        => $request['request_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                             'payment_gateway'   => 'sadad', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                             'ref_no'            => $request['ref_no'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                         'headers'   => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                             'accept'            => 'application/json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $result = json_decode($response->getBody()->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 Log::debug('Mahamax tranasaction verification failed with message: ' . $e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 return (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     'status'        => 'error', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     'message'       => 'پرداخت با خطا مواجه شد.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 ];     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             return (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'status'        => 'error', | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |                 'message'       => 'پرداخت با خطا مواجه شد.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         // dd($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // if ($request->has('token')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         //     $transaction = Transaction::whereToken($request->token)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         //     dd($request->all(), $transaction); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         // } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // $class = new MahamaxIpg($this->getInformation()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         // if (isset($request['ResCode']) && $request['ResCode'] == 0 && isset($request['token'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         //     $result = $class->verify(token: $request['token']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         //     if ($result->status == 'success') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         //         $this->updateTransactionData($request['OrderId'], ['status' => 'successful', 'ref_no' => $result->ref_no]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         //     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         //         $result->message = 'پرداخت با خطا مواجه شد.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         //         $this->updateTransactionData($request['OrderId'], ['status' => 'failed']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         //     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         //     return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         // } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         // $this->updateTransactionData($request['OrderId'], ['status' => 'failed']); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  | } |