| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace LauLamanApps\IzettleApi\Client\Purchase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use LauLamanApps\IzettleApi\API\Purchase\AbstractPayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use LauLamanApps\IzettleApi\API\Purchase\Payment\CardPayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use LauLamanApps\IzettleApi\API\Purchase\Payment\CashPayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use LauLamanApps\IzettleApi\API\Purchase\Payment\InvoicePayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use LauLamanApps\IzettleApi\API\Purchase\Payment\MobilePayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use LauLamanApps\IzettleApi\API\Purchase\Payment\SwishPayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use LauLamanApps\IzettleApi\API\Purchase\Payment\VippsPayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use LauLamanApps\IzettleApi\Client\Exceptions\PaymentTypeNotConfiguredException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Money\Currency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Money\Money; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Ramsey\Uuid\Uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | final class PaymentBuilder implements PaymentBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const CARD = 'IZETTLE_CARD'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const CASH = 'IZETTLE_CASH'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     const INVOICE = 'IZETTLE_INVOICE'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     const MOBILE = 'MOBILE_PAY'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     const SWISH = 'SWISH'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     const VIPPS = 'VIPPS'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 3 |  |     public function buildFromArray(array $payments, Currency $currency): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |         foreach ($payments as $payment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |             $data[] = $this->build($payment, $currency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 11 |  |     public function build(array $payment, Currency $currency): AbstractPayment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 11 |  |         switch ($payment['type']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 11 |  |             case self::CARD: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 4 |  |                 return $this->parseCardPayment($payment, $currency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 8 |  |             case self::CASH: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |                 return $this->parseCashPayment($payment, $currency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 5 |  |             case self::INVOICE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |                 return $this->parseInvoicePayment($payment, $currency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 4 |  |             case self::MOBILE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |                 return $this->parseMobilePayment($payment, $currency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |             case self::SWISH: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |                 return $this->parseSwichPayment($payment, $currency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |             case self::VIPPS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |                 return $this->parseVippsPayment($payment, $currency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         throw new PaymentTypeNotConfiguredException('Payment type \'' . $payment['type'] . '\' not configured'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |     private function parseCardPayment($payment, Currency $currency): CardPayment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 4 |  |         return new CardPayment( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 4 |  |             Uuid::fromString($payment['uuid']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |             new Money($payment['amount'], $currency), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |             $payment['attributes']['referenceNumber'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |             $payment['attributes']['maskedPan'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  |             $payment['attributes']['cardType'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |             $payment['attributes']['cardPaymentEntryMode'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 4 |  |             $this->getFromKey('applicationName', $payment['attributes']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 4 |  |             $this->getFromKey('applicationIdentifier', $payment['attributes']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |             $this->getFromKey('terminalVerificationResults', $payment['attributes']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |             (int) $this->getFromKey('nrOfInstallments', $payment['attributes']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |     private function parseCashPayment($payment, Currency $currency): CashPayment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |         return new CashPayment( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |             Uuid::fromString($payment['uuid']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |             new Money($payment['amount'], $currency), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |             new Money($payment['attributes']['handedAmount'], $currency) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 83 | 1 | View Code Duplication |     private function parseInvoicePayment($payment, Currency $currency): InvoicePayment | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |         return new InvoicePayment( | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |             Uuid::fromString($payment['uuid']), | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |             new Money($payment['amount'], $currency) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 91 | 1 | View Code Duplication |     private function parseMobilePayment($payment, Currency $currency): MobilePayment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         return new MobilePayment( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             Uuid::fromString($payment['uuid']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             new Money($payment['amount'], $currency) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 99 | 1 | View Code Duplication |     private function parseSwichPayment($payment, Currency $currency): SwishPayment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         return new SwishPayment( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |             Uuid::fromString($payment['uuid']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |             new Money($payment['amount'], $currency) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 107 | 1 | View Code Duplication |     private function parseVippsPayment($payment, Currency $currency): VippsPayment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         return new VippsPayment( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |             Uuid::fromString($payment['uuid']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |             new Money($payment['amount'], $currency) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 4 |  |     private function getFromKey($key, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |         if (!array_key_exists($key, $data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 4 |  |         return $data[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  |