<?php
namespace Trustpay;

class ResponseCard extends Response {
    
    protected $jsonData;
    
    public function getTransactionId() {
        return $this->paymentRequestId;
    }

    public function validateSignature() {//@ini_set( 'display_errors', 1 );
        // musi ist aby sa naplnilo $this->signature
        $sigData = $this->getSigData();
        //var_dump($this->SignatureData->getSignature($this->getSigData()));
        
        if($this->signature == $this->SignatureData->getSignature($sigData)) {
            return true;
        }
        elseif($this->signature == $this->SignatureData->getSignature($this->getAllSigData())) {
            return true;
        }
        
        return false;
    }

    protected function getSigData() {
        //$data['test'] = 'test';
        //file_put_contents('Card_response.txt', $data);
        /*
            {
                "PaymentMethod": "Card",
                "MerchantIdentification": {
                  "ProjectId": "4101111111"
                },
                "PaymentInformation": {
                  "Amount": {
                    "Amount": "25.00",
                    "Currency": "EUR"
                  },
                  "CreditDebitIndicator": "CRDT",
                  "Status": "Paid",
                  "References": {
                    "MerchantReference": "1234567890",
                    "PaymentId": "49863043",
                    "PaymentRequestId": "1234"
                    "ClearingSystemReference": "6640.2103.6494"
                  },
                  "Debtor": {
                    "Name": "Test name"
                  },
                  "CardTransaction": {
                    "Card": {
                      "MaskedPan": "420000******0000",
                      "ExpiryDate": "12/21"
                    }
                  }
                },
                "Signature": "013B192245EF81995E3F219E064F42DC1E1F4A6BC67CB546E1CCA6DC35E5CB73"
              }
         *          */
        
        $this->jsonData = $jsonData = json_decode(file_get_contents('php://input'), true);
        
        $data = array();

        if(is_array($jsonData)) {
            foreach($jsonData as $key1 => $value) {
                if(is_array($value)) {
                    foreach($value as $key2 => $value2) {
                        if(is_array($value2)) {
                            foreach($value2 as $key3 => $value3) {
                                
                                if(is_array($value3)) {
                                    foreach($value3 as $key4 => $value4) {
                                        
                                        if($key4 == 'MaskedPan' || $key4 == 'ExpiryDate') {
                                            $data[] = $value4;
                                        }
                                        
                                    }
                                } else {
                                    if($key3 == 'PaymentRequestId') {
                                        $this->paymentRequestId = $value3;

                                    } elseif($key3 == 'Amount') {
                                        $value3 = (string) number_format($value3, 2, '.', '');
                                        $this->amount = $value3;

                                    } elseif($key3 == 'Currency') {
                                        $this->currency = $value3;

                                    } elseif($key3 == 'MerchantReference') {
                                        $this->orderId = $this->reference = $value3;

                                    } elseif($key3 == 'PaymentId') {
                                        $this->paymentId = $value3;

                                    }

                                    $data[] = $value3;
                                }
                                
                            }
                        } else {
                            $data[] = $value2;
                            
                            if($key2 == 'ProjectId') {
                                $this->accountId = $value2;
                            } 
                            elseif($key2 == 'Status' && $value2 == 'Paid') {//Paid, Authorized, Rejected
                                $this->resultCode = ResultCodes::SUCCESS;
                            }
                            elseif($key2 == 'Status' && $value2 == 'Authorized') {
                                $this->resultCode = ResultCodes::AUTHORIZED;
                            }
                            elseif($key2 == 'Status' && $value2 == 'Rejected') {
                                $this->resultCode = ResultCodes::REJECTED;
                            }
                        }
                    }
                } else {
                    
                    if($key1 == 'Signature') {
                        $this->signature = $value;
                    } else {
                        $data[] = $value;
                    }
                }
            }
        }
        
        $data = array_filter($data);
        sort($data, SORT_STRING);
        
        return $data;
    }
    
    protected function getAllSigData() {
        //$data['test'] = 'test';
        //file_put_contents('Card_response.txt', $data);
        /*
            {
                "PaymentMethod": "Card",
                "MerchantIdentification": {
                  "ProjectId": "4101111111"
                },
                "PaymentInformation": {
                  "Amount": {
                    "Amount": "25.00",
                    "Currency": "EUR"
                  },
                  "CreditDebitIndicator": "CRDT",
                  "Status": "Paid",
                  "References": {
                    "MerchantReference": "1234567890",
                    "PaymentId": "49863043",
                    "PaymentRequestId": "1234"
                    "ClearingSystemReference": "6640.2103.6494"
                  },
                  "Debtor": {
                    "Name": "Test name"
                  },
                  "CardTransaction": {
                    "Card": {
                      "MaskedPan": "420000******0000",
                      "ExpiryDate": "12/21"
                      "Subtype": "GooglePay"
                    }
                  }
                },
                "Signature": "013B192245EF81995E3F219E064F42DC1E1F4A6BC67CB546E1CCA6DC35E5CB73"
              }
         *          */
        //$jsonData = json_decode(file_get_contents('php://input'), true);
        $jsonData = $this->jsonData;
        
        $data = array();

        if(is_array($jsonData)) {
            foreach($jsonData as $key1 => $value) {
                if(is_array($value)) {
                    foreach($value as $key2 => $value2) {
                        if(is_array($value2)) {
                            foreach($value2 as $key3 => $value3) {
                                
                                if(is_array($value3)) {
                                    foreach($value3 as $key4 => $value4) {
                                        
                                        //if($key4 == 'MaskedPan' || $key4 == 'ExpiryDate') {
                                            $data[] = $value4;
                                        //}
                                        
                                    }
                                } else {
                                    if($key3 == 'PaymentRequestId') {
                                        $this->paymentRequestId = $value3;

                                    } elseif($key3 == 'Amount') {
                                        $value3 = (string) number_format($value3, 2, '.', '');
                                        $this->amount = $value3;

                                    } elseif($key3 == 'Currency') {
                                        $this->currency = $value3;

                                    } elseif($key3 == 'MerchantReference') {
                                        $this->orderId = $this->reference = $value3;

                                    } elseif($key3 == 'PaymentId') {
                                        $this->paymentId = $value3;

                                    }

                                    $data[] = $value3;
                                }
                                
                            }
                        } else {
                            $data[] = $value2;
                            
                            if($key2 == 'ProjectId') {
                                $this->accountId = $value2;
                            } elseif($key2 == 'Status' && $value2 == 'Paid') {
                                $this->resultCode = ResultCodes::SUCCESS;
                            }
                            elseif($key2 == 'Status' && $value2 == 'Authorized') {
                                $this->resultCode = ResultCodes::AUTHORIZED;
                            }
                            elseif($key2 == 'Status' && $value2 == 'Rejected') {
                                $this->resultCode = ResultCodes::REJECTED;
                            }
                        }
                    }
                } else {
                    
                    if($key1 == 'Signature') {
                        $this->signature = $value;
                    } else {
                        $data[] = $value;
                    }
                }
            }
        }
        
        $data = array_filter($data);
        sort($data, SORT_STRING);
        
        return $data;
    }
}