Example #1
0
 /**
  * Returns a list of Object from Array or Json String. It is generally used when your json
  * contains an array of this object
  *
  * @param mixed $data Array object or json string representation
  * @return array
  */
 public static function getList($data)
 {
     // Return Null if Null
     if ($data === null) {
         return null;
     }
     if (is_a($data, get_class(new \stdClass()))) {
         //This means, root element is object
         return new static(json_encode($data));
     }
     $list = array();
     if (is_array($data)) {
         $data = json_encode($data);
     }
     if (JsonValidator::validate($data)) {
         // It is valid JSON
         $decoded = json_decode($data);
         if ($decoded === null) {
             return $list;
         }
         if (is_array($decoded)) {
             foreach ($decoded as $k => $v) {
                 $list[] = self::getList($v);
             }
         }
         if (is_a($decoded, get_class(new \stdClass()))) {
             //This means, root element is object
             $list[] = new static(json_encode($decoded));
         }
     }
     return $list;
 }
 /**
  * A pull method which would read the persisted data based on clientId.
  * If clientId is not provided, an array with all the tokens would be passed.
  *
  * @param array|null $config
  * @param string $clientId
  * @return mixed|null
  */
 public static function pull($config = null, $clientId = null)
 {
     // Return if not enabled
     if (!self::isEnabled($config)) {
         return null;
     }
     $tokens = null;
     $cachePath = self::cachePath($config);
     if (file_exists($cachePath)) {
         // Read from the file
         $cachedToken = file_get_contents($cachePath);
         if ($cachedToken && JsonValidator::validate($cachedToken, true)) {
             $tokens = json_decode($cachedToken, true);
             if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) {
                 // If client Id is found, just send in that data only
                 return $tokens[$clientId];
             } else {
                 if ($clientId) {
                     // If client Id is provided, but no key in persisted data found matching it.
                     return null;
                 }
             }
         }
     }
     return $tokens;
 }
Example #3
0
 /**
  * Returns a list of Object from Array or Json String. It is generally used when you json
  * contains an array of this object
  *
  * @param mixed $data Array object or json string representation
  * @return array
  */
 public static function getList($data)
 {
     if (!is_array($data) && JsonValidator::validate($data)) {
         //Convert to Array if Json Data Sent
         $data = json_decode($data, true);
     }
     if (!ArrayUtil::isAssocArray($data)) {
         $list = array();
         //This means, root element is array
         foreach ($data as $k => $v) {
             $obj = new static();
             $obj->fromArray($v);
             $list[] = $obj;
         }
         return $list;
     }
 }
 /**
  * Validates Received Event from Webhook, and returns the webhook event object. Because security verifications by verifying certificate chain is not enabled in PHP yet,
  * we need to fallback to default behavior of retrieving the ID attribute of the data, and make a separate GET call to PayPal APIs, to retrieve the data.
  * This is important to do again, as hacker could have faked the data, and the retrieved data cannot be trusted without either doing client side security validation, or making a separate call
  * to PayPal APIs to retrieve the actual data. This limits the hacker to mimick a fake data, as hacker wont be able to predict the Id correctly.
  *
  * NOTE: PLEASE DO NOT USE THE DATA PROVIDED IN WEBHOOK DIRECTLY, AS HACKER COULD PASS IN FAKE DATA. IT IS VERY IMPORTANT THAT YOU RETRIEVE THE ID AND MAKE A SEPARATE CALL TO PAYPAL API.
  *
  * @param string     $body
  * @param ApiContext $apiContext
  * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
  * @return WebhookEvent
  * @throws \InvalidArgumentException if input arguments are incorrect, or Id is not found.
  * @throws PayPalConnectionException if any exception from PayPal APIs other than not found is sent.
  */
 public static function validateAndGetReceivedEvent($body, $apiContext = null, $restCall = null)
 {
     if ($body == null | empty($body)) {
         throw new \InvalidArgumentException("Body cannot be null or empty");
     }
     if (!JsonValidator::validate($body, true)) {
         throw new \InvalidArgumentException("Request Body is not a valid JSON.");
     }
     $object = new WebhookEvent($body);
     if ($object->getId() == null) {
         throw new \InvalidArgumentException("Id attribute not found in JSON. Possible reason could be invalid JSON Object");
     }
     try {
         return self::get($object->getId(), $apiContext, $restCall);
     } catch (PayPalConnectionException $ex) {
         if ($ex->getCode() == 404) {
             // It means that the given webhook event Id is not found for this merchant.
             throw new \InvalidArgumentException("Webhook Event Id provided in the data is incorrect. This could happen if anyone other than PayPal is faking the incoming webhook data.");
         }
         throw $ex;
     }
 }
Example #5
0
 protected static function printObject($object, $error = null)
 {
     if ($error) {
         echo '<p class="error"><i class="fa fa-exclamation-triangle"></i> ' . $error . '</p>';
     }
     if ($object) {
         if (is_a($object, 'PayPal\\Common\\PayPalModel')) {
             /** @var $object \PayPal\Common\PayPalModel */
             echo '<pre class="prettyprint ' . ($error ? 'error' : '') . '">' . $object->toJSON(128) . "</pre>";
         } elseif (is_string($object) && \PayPal\Validation\JsonValidator::validate($object, true)) {
             echo '<pre class="prettyprint ' . ($error ? 'error' : '') . '">' . str_replace('\\/', '/', json_encode(json_decode($object), 128)) . "</pre>";
         } elseif (is_string($object)) {
             echo '<pre class="prettyprint ' . ($error ? 'error' : '') . '">' . $object . '</pre>';
         } else {
             echo "<pre>";
             print_r($object);
             echo "</pre>";
         }
     } else {
         echo "<span>No Data</span>";
     }
 }
Example #6
0
 /**
  *
  * @dataProvider invalidProvider
  */
 public function testInvalidJsonSilent($input)
 {
     $this->assertFalse(JsonValidator::validate($input, true));
 }