| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace MyFatoorah\Library\API\Payment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use MyFatoorah\Library\API\MyFatoorahList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  *  MyFatoorahPaymentForm handles the form process of MyFatoorah API endpoints | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author    MyFatoorah <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright MyFatoorah, All rights reserved | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license   GNU General Public License v3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class MyFatoorahPaymentEmbedded extends MyFatoorahPayment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * The checkoutGateways array is used to display the payment in the checkout page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected static $checkoutGateways; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     //----------------------------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * List available Payment Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param double|int $invoiceAmount  The display invoice total amount. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string     $currencyIso    The display invoice currency ISO. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param bool       $isApRegistered Is site domain is registered with ApplePay and MyFatoorah or not. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function getCheckoutGateways($invoiceAmount, $currencyIso, $isApRegistered) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (!empty(self::$checkoutGateways)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             return self::$checkoutGateways; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $gateways = $this->initiatePayment($invoiceAmount, $currencyIso); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $mfListObj    = new MyFatoorahList($this->config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $allRates     = $mfListObj->getCurrencyRates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $currencyRate = MyFatoorahList::getOneCurrencyRate($currencyIso, $allRates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self::$checkoutGateways = ['all' => [], 'cards' => [], 'form' => [], 'ap' => [], 'gp' => []]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         foreach ($gateways as $gateway) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $gateway->PaymentTotalAmount = $this->getPaymentTotalAmount($gateway, $allRates, $currencyRate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $gateway->GatewayData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 'GatewayTotalAmount'   => number_format($gateway->PaymentTotalAmount, 2), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 'GatewayCurrency'      => $gateway->PaymentCurrencyIso, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 'GatewayTransCurrency' => self::getTranslatedCurrency($gateway->PaymentCurrencyIso), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             self::$checkoutGateways = $this->addGatewayToCheckout($gateway, self::$checkoutGateways, $isApRegistered); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if ($isApRegistered) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             //add only one ap gateway | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             self::$checkoutGateways['ap'] = $this->getOneEmbeddedGateway(self::$checkoutGateways['ap'], $currencyIso, $allRates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return self::$checkoutGateways; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     //----------------------------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Calculate the amount value that will be paid in each payment method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param object $paymentMethod The payment method object obtained from the initiate payment endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param array  $allRates      The MyFatoorah currency rate array of all gateways. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param double $currencyRate  The currency rate of the invoice. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return double | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     private function getPaymentTotalAmount($paymentMethod, $allRates, $currencyRate) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $dbTrucVal = ((int) ($paymentMethod->TotalAmount * 1000)) / 1000; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if ($paymentMethod->PaymentCurrencyIso == $paymentMethod->CurrencyIso) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return ceil($dbTrucVal * 100) / 100; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         //convert to portal base currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $dueVal          = ($currencyRate == 1) ? $dbTrucVal : round($paymentMethod->TotalAmount / $currencyRate, 3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $baseTotalAmount = ceil($dueVal * 100) / 100; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         //gateway currency is not the portal currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $paymentCurrencyRate = MyFatoorahList::getOneCurrencyRate($paymentMethod->PaymentCurrencyIso, $allRates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if ($paymentCurrencyRate != 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             return ceil($baseTotalAmount * $paymentCurrencyRate * 100) / 100; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $baseTotalAmount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     //----------------------------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Returns One Apple pay array in case multiple are enabled in the account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param array  $gateways        The all available AP/GP gateways | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param string $displayCurrency The currency of the invoice total amount. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param array  $allRates        The MyFatoorah currency rate array of all gateways. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     private function getOneEmbeddedGateway($gateways, $displayCurrency, $allRates) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $displayCurrencyIndex = array_search($displayCurrency, array_column($gateways, 'PaymentCurrencyIso')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if ($displayCurrencyIndex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return $gateways[$displayCurrencyIndex]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         //get defult mf account currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $defCurKey       = array_search('1', array_column($allRates, 'Value')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $defaultCurrency = $allRates[$defCurKey]->Text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $defaultCurrencyIndex = array_search($defaultCurrency, array_column($gateways, 'PaymentCurrencyIso')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         if ($defaultCurrencyIndex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             return $gateways[$defaultCurrencyIndex]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if (isset($gateways[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             return $gateways[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     //----------------------------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * Returns the translation of the currency ISO code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @param string $currency currency ISO code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |     public static function getTranslatedCurrency($currency) | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         $currencies = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |             'KWD' => ['en' => 'KD', 'ar' => 'د.ك'], | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |             'SAR' => ['en' => 'SR', 'ar' => 'ريال'], | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |             'BHD' => ['en' => 'BD', 'ar' => 'د.ب'], | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |             'EGP' => ['en' => 'LE', 'ar' => 'ج.م'], | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |             'QAR' => ['en' => 'QR', 'ar' => 'ر.ق'], | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |             'OMR' => ['en' => 'OR', 'ar' => 'ر.ع'], | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |             'JOD' => ['en' => 'JD', 'ar' => 'د.أ'], | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |             'AED' => ['en' => 'AED', 'ar' => 'د'], | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |             'USD' => ['en' => 'USD', 'ar' => 'دولار'], | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |             'EUR' => ['en' => 'EUR', 'ar' => 'يورو'] | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |         return $currencies[$currency] ?? ['en' => '', 'ar' => '']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     //----------------------------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  |