1: <?php
 2: 
 3:  4:  5: 
 6: class Syspay_Merchant_Redirect
 7: {
 8:     protected $secrets = array();
 9:     protected $skipAuthCheck;
10: 
11:     12: 13: 14: 15: 
16:     public function __construct(array $secrets, $skipAuthCheck = false)
17:     {
18:         $this->secrets       = $secrets;
19:         $this->skipAuthCheck = $skipAuthCheck;
20:     }
21: 
22:     23: 24: 25: 26: 27: 28: 
29:     public function getResult(array $source)
30:     {
31:         $result   = isset($source['result'])?$source['result']:null;
32:         $merchant = isset($source['merchant'])?$source['merchant']:null;
33:         $checksum = isset($source['checksum'])?$source['checksum']:null;
34: 
35:         if (!$this->skipAuthCheck) {
36:             $this->checkChecksum($result, $merchant, $checksum);
37:         }
38: 
39:         $result = base64_decode($result);
40:         if ($result === false) {
41:             throw new Syspay_Merchant_RedirectException(
42:                 'Unable to decode the result parameter',
43:                 Syspay_Merchant_RedirectException::CODE_INVALID_CONTENT
44:             );
45:         }
46: 
47:         $result = json_decode($result);
48:         if ($result === null || empty($result->payment)) {
49:             throw new Syspay_Merchant_RedirectException(
50:                 'Unable to decode the result parameter',
51:                 Syspay_Merchant_RedirectException::CODE_INVALID_CONTENT
52:             );
53:         }
54:         return Syspay_Merchant_Entity_Payment::buildFromResponse($result->payment);
55:     }
56: 
57:     58: 59: 60: 
61:     private function checkChecksum($result, $merchant, $checksum)
62:     {
63:         if (empty($merchant) || empty($checksum) || empty($result)) {
64:             throw new Syspay_Merchant_RedirectException(
65:                 'Missing parameter',
66:                 Syspay_Merchant_RedirectException::CODE_MISSING_PARAM
67:             );
68:         }
69: 
70:         if (empty($this->secrets[$merchant])) {
71:             throw new Syspay_Merchant_RedirectException(
72:                 'Unknown merchant: ' . $merchant,
73:                 Syspay_Merchant_RedirectException::CODE_UNKNOWN_MERCHANT
74:             );
75:         }
76: 
77:         if (!Syspay_Merchant_Utils::checkChecksum($result, $this->secrets[$merchant], $checksum)) {
78:             throw new Syspay_Merchant_RedirectException(
79:                 'Invalid checksum',
80:                 Syspay_Merchant_RedirectException::CODE_INVALID_CHECKSUM
81:             );
82:         }
83:     }
84: }
85: