Skip to content

Commit

Permalink
Merge pull request #66 from razorpay/release/1.6.0
Browse files Browse the repository at this point in the history
Release/1.6.0
  • Loading branch information
captn3m0 authored Oct 26, 2017
2 parents 17d6ddb + c74f01f commit e368546
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 102 deletions.
4 changes: 2 additions & 2 deletions razorpay-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Plugin Name: Razorpay for WooCommerce
Plugin URI: https://razorpay.com
Description: Razorpay Payment Gateway Integration for WooCommerce
Version: 1.6.0-beta
Stable tag: 1.5.3
Version: 1.6.0
Stable tag: 1.6.0
Author: Razorpay
Author URI: https://razorpay.com
*/
Expand Down
21 changes: 17 additions & 4 deletions razorpay-sdk/Razorpay.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<?php

if(class_exists('Requests') === false){
require_once __DIR__.'/libs/Requests-1.6.1/library/Requests.php';
// Include Requests only if not already defined
if (class_exists('Requests') === false)
{
require_once __DIR__.'/libs/Requests-1.6.1/library/Requests.php';
}

try
{
Requests::register_autoloader();

// Register requests autoloader
Requests::register_autoloader();
if (version_compare(Requests::VERSION, '1.6.0') === -1)
{
throw new Exception('Requests class found but did not match');
}
}
catch (\Exception $e)
{
throw new Exception('Requests class found but did not match');
}

spl_autoload_register(function ($class)
Expand Down
21 changes: 21 additions & 0 deletions razorpay-sdk/src/Addon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Razorpay\Api;

class Addon extends Entity
{
// To create an Addon,
// use the createAddon method of the Subscription class

public function fetch($id)
{
return parent::fetch($id);
}

public function delete()
{
$entityUrl = $this->getEntityUrl();

return $this->request('DELETE', $entityUrl . $this->id);
}
}
3 changes: 1 addition & 2 deletions razorpay-sdk/src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Api
*/
public static $appsDetails = array();

const VERSION = '1.2.9';
const VERSION = '2.2.0';

/**
* @param string $key
Expand All @@ -30,7 +30,6 @@ public function __construct($key, $secret)

/*
* Set Headers
*
*/
public function setHeader($header, $value)
{
Expand Down
117 changes: 59 additions & 58 deletions razorpay-sdk/src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,30 @@ protected function getEntityUrl()
$fullClassName = get_class($this);
$pos = strrpos($fullClassName, '\\');
$className = substr($fullClassName, $pos + 1);
$className = lcfirst($className);
$className = $this->snakeCase($className);
return $className.'s/';
}

protected function snakeCase($input)
{
$delimiter = '_';
$output = preg_replace('/\s+/u', '', ucwords($input));
$output = preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $output);
$output = strtolower($output);
return $output;
}

/**
* Makes a HTTP request using Request class and assuming the API returns
* formatted entity or collection result, wraps the returned JSON as entity
* and returns.
*
* @param string $method
* @param string $relativeUrl
* @param array $data
*
* @return Entity
*/
protected function request($method, $relativeUrl, $data = null)
{
$request = new Request();
Expand All @@ -67,21 +87,23 @@ protected function request($method, $relativeUrl, $data = null)
}
else
{
return static::buildEntity(null, $response);
return static::buildEntity($response);
}
}

protected static function buildEntity($key, $data)
/**
* Given the JSON response of an API call, wraps it to corresponding entity
* class or a collection and returns the same.
*
* @param array $data
*
* @return Entity
*/
protected static function buildEntity($data)
{
$entities = static::getDefinedEntitiesArray();

$arrayableAttributes = static::getArrayableAttributes();

if (in_array($key, $arrayableAttributes))
{
$entity = $data;
}
else if (isset($data['entity']))
if (isset($data['entity']))
{
if (in_array($data['entity'], $entities))
{
Expand All @@ -90,26 +112,17 @@ protected static function buildEntity($key, $data)
}
else
{
$entity = new self;
$entity = new static;
}

$entity->fill($data);
}
else
{
$entity = new self;

$entity->fill($data);
$entity = new static;
}

return $entity;
}
$entity->fill($data);

protected static function getArrayableAttributes()
{
return array(
'notes'
);
return $entity;
}

protected static function getDefinedEntitiesArray()
Expand Down Expand Up @@ -145,7 +158,29 @@ public function fill($data)
{
if (is_array($value))
{
$value = self::getArrayValue($key, $value);
if (static::isAssocArray($value) === false)
{
$collection = array();

foreach ($value as $v)
{
if (is_array($v))
{
$entity = static::buildEntity($v);
array_push($collection, $entity);
}
else
{
array_push($collection, $v);
}
}

$value = $collection;
}
else
{
$value = static::buildEntity($value);
}
}

$attributes[$key] = $value;
Expand All @@ -154,40 +189,6 @@ public function fill($data)
$this->attributes = $attributes;
}

protected static function getArrayValue($key, $value)
{
if (static::isAssocArray($value) === false)
{
$value = self::getAssocArrayValue($key, $value);
}
else
{
$value = static::buildEntity($key, $value);
}

return $value;
}

protected static function getAssocArrayValue($key, $value)
{
$collection = array();

foreach ($value as $v)
{
if (is_array($v))
{
$entity = static::buildEntity($key, $v);
array_push($collection, $entity);
}
else
{
array_push($collection, $v);
}
}

return $collection;
}

public static function isAssocArray($arr)
{
return array_keys($arr) !== range(0, count($arr) - 1);
Expand Down
2 changes: 1 addition & 1 deletion razorpay-sdk/src/Errors/ErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public static function exists($code)

return defined(get_class() . '::' . $code);
}
}
}
99 changes: 94 additions & 5 deletions razorpay-sdk/src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,112 @@

namespace Razorpay\Api;

use Requests;

/**
* Invoice entity gets used for both Payment Links and Invoices system.
* Few of the methods are only meaningful for Invoices system and calling those
* for against/for a Payment Link would throw Bad request error.
*/
class Invoice extends Entity
{
/**
* @param $id Invoice id
* Creates invoice of any type(invoice|link|ecod).
*
* @param array $attributes
*
* @return Invoice
*/
public function fetch($id)
public function create($attributes = array())
{
return parent::fetch($id);
return parent::create($attributes);
}

public function create($attributes = array())
/**
* Fetches invoice entity with given id
*
* @param string $id
*
* @return Invoice
*/
public function fetch($id)
{
return parent::create($attributes);
return parent::fetch($id);
}

/**
* Fetches multiple invoices with given query options
*
* @param array $options
*
* @return Collection
*/
public function all($options = array())
{
return parent::all($options);
}

/**
* Cancels issued invoice
*
* @return Invoice
*/
public function cancel()
{
$url = $this->getEntityUrl() . $this->id . '/cancel';

return $this->request(Requests::POST, $url);
}

/**
* Send/re-send notification for invoice by given medium
*
* @param $medium - sms|email
*
* @return array
*/
public function notifyBy($medium)
{
$url = $this->getEntityUrl() . $this->id . '/notify_by/' . $medium;

return (new Request())->request(Requests::POST, $url);
}

/**
* Patches given invoice with new attributes
*
* @param array $attributes
*
* @return Invoice
*/
public function edit($attributes = array())
{
$url = $this->getEntityUrl() . $this->id;

return $this->request(Requests::PATCH, $url, $attributes);
}

/**
* Issues drafted invoice
*
* @return Invoice
*/
public function issue()
{
$url = $this->getEntityUrl() . $this->id . '/issue';

return $this->request(Requests::POST, $url);
}

/**
* Deletes drafted invoice
*
* @return Invoice
*/
public function delete()
{
$url = $this->getEntityUrl() . $this->id;

return (new Request())->request(Requests::DELETE, $url);
}
}
Loading

0 comments on commit e368546

Please sign in to comment.