Skip to content

Commit

Permalink
Merge pull request #233 from razorpay/payment-link
Browse files Browse the repository at this point in the history
payment link end point
  • Loading branch information
neera11 committed Sep 16, 2021
2 parents 93638ed + 5ed1c1b commit f562c91
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Changelog for Razorpay-PHP SDK. Follows [keepachangelog.com](https://keepachangelog.com/en/1.0.0/) for formatting.

### Added

- Added Payment Link end point API [[#233](https://github.com/razorpay/razorpay-php/pull/233)]

## [2.7.1][2.7.1] - 2021-09-16

## Unreleased

## [2.7.0][2.7.0] - 2021-05-07
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,20 @@ $transfer = $api->transfer->fetch($transferId)->edit($options); // Edit a trans
$reversal = $api->transfer->fetch($transferId)->reverse(); // Reverse a transfer

// Payment Links
$links = $api->invoice->all();
$link = $api->invoice->fetch('inv_00000000000001');
$link = $api->invoice->create(arary('type' => 'link', 'amount' => 500, 'description' => 'For XYZ purpose', 'customer' => array('email' => 'test@test.test')));
$links = $api->payment_link->all(); // fetch all payment links

$link = $api->payment_link->fetch('plink_GiwM9xbIZqbkJp'); // fetch payment link with id

$data = json_encode(
[
'amount' => 98765,
'description' => 'For XYZ purpose',
'customer' => array('email' => 'test@test.test')
]);
$link->payment_link->create($data); // create payment link , pass $data.
$link = $api->payment_link->fetch('plink_GiwM9xbIZqbkJp'); // cancel payment link , first fetch payment link with id and then call cancel method like $link->cancel();
$link->cancel();

$link->notifyBy('sms');

// Invoices
Expand Down
2 changes: 1 addition & 1 deletion 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 = '2.7.0';
const VERSION = '2.7.1';

/**
* @param string $key
Expand Down
41 changes: 31 additions & 10 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
class Entity extends Resource implements ArrayableInterface
{
protected $attributes = array();

protected function create($attributes = null)
private $additionHeader = array();
/**
* Create method
*
* @param array $attributes
* @param array $additionHeader
*
*
*/
protected function create($attributes = null)
{
$entityUrl = $this->getEntityUrl();

return $this->request('POST', $entityUrl, $attributes);
return $this->request('POST', $entityUrl, $attributes, $this->additionHeader);
}

protected function fetch($id)
Expand All @@ -22,7 +30,7 @@ protected function fetch($id)
$this->validateIdPresence($id);

$relativeUrl = $entityUrl . $id;

return $this->request('GET', $relativeUrl);
}

Expand All @@ -46,7 +54,7 @@ protected function validateIdPresence($id)
protected function all($options = array())
{
$entityUrl = $this->getEntityUrl();

return $this->request('GET', $entityUrl, $options);
}

Expand Down Expand Up @@ -76,17 +84,17 @@ protected function snakeCase($input)
* @param string $method
* @param string $relativeUrl
* @param array $data
* @param array $additionHeader
*
* @return Entity
*/
protected function request($method, $relativeUrl, $data = null)
protected function request($method, $relativeUrl, $data = null, $additionHeader = array())
{
$request = new Request();

$response = $request->request($method, $relativeUrl, $data);
$response = $request->request($method, $relativeUrl, $data, $additionHeader);

if ((isset($response['entity'])) and
($response['entity'] == $this->getEntity()))
if ((isset($response['entity'])) and ($response['entity'] == $this->getEntity()))
{
$this->fill($response);

Expand All @@ -98,6 +106,19 @@ protected function request($method, $relativeUrl, $data = null)
}
}

/**
* set Additional Header
*
*
* @param array $additionHeader
*
* @return Entity
*/
protected function setAdditionHeader($additionHeader){

$this->additionHeader = $additionHeader;

}
/**
* Given the JSON response of an API call, wraps it to corresponding entity
* class or a collection and returns the same.
Expand Down
62 changes: 62 additions & 0 deletions src/Payment_link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Razorpay\Api;

use Requests;


class Payment_link extends Entity
{
/**
* Creates Payment link .
*
* @param array $attributes
*
* @return Payment_link
*/
public function create($attributes = array())
{
$additionHeader['Content-Type'] = 'application/json';

parent::setAdditionHeader($additionHeader) ;

return parent::create($attributes);
}

/**
* Fetches Payment link entity with given id
*
* @param string $id
*
* @return Payment_link
*/
public function fetch($id)
{
return parent::fetch($id);
}

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

/**
* Cancels Payment link
*
* @return Payment_link
*/
public function cancel()
{
$url = $this->getEntityUrl() . $this->id . '/cancel';

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

}
24 changes: 15 additions & 9 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ class Request
* @var array
*/
protected static $headers = array(
'Razorpay-API' => 1
'Razorpay-API' => 1
);

/**
* Fires a request to the API
* @param string $method HTTP Verb
* @param string $url Relative URL for the request
* @param array $data Data to be passed along the request
* @param array $additionHeader headers to be passed along the request
* @return array Response data in array format. Not meant
* to be used directly
*/
public function request($method, $url, $data = array())
public function request($method, $url, $data = array(), $additionHeader = array())
{
$url = Api::getFullUrl($url);

Expand All @@ -48,13 +49,12 @@ public function request($method, $url, $data = array())
$options = array(
'auth' => array(Api::getKey(), Api::getSecret()),
'hook' => $hooks,
'timeout' => 60,
'timeout' => 60
);

$headers = $this->getRequestHeaders($additionHeader);

$headers = $this->getRequestHeaders();

$response = Requests::request($url, $headers, $data, $method, $options);

$response = Requests::request($url, $headers, $data, $method, $options);
$this->checkErrors($response);

return json_decode($response->body, true);
Expand Down Expand Up @@ -153,14 +153,20 @@ protected function throwServerError($body, $httpStatusCode)
$httpStatusCode);
}

protected function getRequestHeaders()
protected function getRequestHeaders($additionHeader)
{
$uaHeader = array(
'User-Agent' => $this->constructUa()

);

$headers = array_merge(self::$headers, $uaHeader);

if(empty($additionHeader) === false)
{
$headers = array_merge($headers, $additionHeader);
}

return $headers;
}

Expand Down

0 comments on commit f562c91

Please sign in to comment.