Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Veselinov committed Apr 23, 2015
0 parents commit 00e88a5
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
/composer.lock
/.idea
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (C) 2015 Vladislav Veselinov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "v3labs/payubiz",
"description": "PayUbiz client for PHP",
"keywords": ["payu", "payubiz"],
"license": "MIT",
"authors": [
{
"name": "Vladislav Veselinov",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"symfony/options-resolver": "~2.6",
"symfony/http-foundation": "~2.6"
},
"require-dev": {

},
"autoload": {
"psr-4": {
"V3labs\\PayUbiz\\": "src/"
}
}
}
85 changes: 85 additions & 0 deletions src/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace V3labs\PayUbiz;

class CompletePurchaseResponse
{
const STATUS_COMPLETED = 'Completed';
const STATUS_PENDING = 'Pending';
const STATUS_FAILED = 'Failed';
const STATUS_TAMPERED = 'Tampered';

/** @var PayUbiz */
private $client;

/** @var array */
private $params;

public function __construct(PayUbiz $client, array $params)
{
$this->client = $client;
$this->params = $params;
}

/**
* @return array
*/
public function getParams()
{
return $this->params;
}

/**
* @return string
*/
public function getStatus()
{
if ($this->checksumIsValid()) {
switch (strtolower($this->getTransactionStatus())) {
case 'success':
return self::STATUS_COMPLETED;
break;
case 'pending':
return self::STATUS_PENDING;
break;
case 'failure':
default:
return self::STATUS_FAILED;
}
}

return self::STATUS_TAMPERED;
}

/**
* @return string|null
*/
public function getTransactionId()
{
return array_key_exists('mihpayid', $this->params) ? (string)$this->params['mihpayid'] : null;
}

/**
* @return string|null
*/
public function getTransactionStatus()
{
return array_key_exists('status', $this->params) ? (string)$this->params['status'] : null;
}

/**
* @return string|null
*/
public function getChecksum()
{
return array_key_exists('hash', $this->params) ? (string)$this->params['hash'] : null;
}

/**
* @return bool
*/
public function checksumIsValid()
{
return false;
}
}
151 changes: 151 additions & 0 deletions src/PayUbiz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace V3labs\PayUbiz;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PayUbiz
{
const PRODUCTION_URL = 'https://secure.payu.in/_payment.php';
const TEST_URL = 'https://test.payu.in/_payment.php';

/** @var string */
private $merchantId;

/** @var string */
private $secretKey;

/** @var bool */
private $testMode;

/**
* @param array $options
*/
public function __construct(array $options)
{
$resolver = (new OptionsResolver())
->setDefaults(['testMode' => true])
->setRequired(['merchantId', 'secretKey', 'testMode'])
->setAllowedTypes('merchantId', 'string')
->setAllowedTypes('secretKey', 'string')
->setAllowedTypes('testMode', 'bool');

$options = $resolver->resolve($options);

$this->merchantId = $options['merchantId'];
$this->secretKey = $options['secretKey'];
$this->testMode = $options['testMode'];
}

/**
* @return string
*/
public function getMerchantId()
{
return $this->merchantId;
}

/**
* @return string
*/
public function getSecretKey()
{
return $this->secretKey;
}

/**
* @return bool
*/
public function getTestMode()
{
return $this->testMode;
}

/**
* @return string
*/
public function getServiceUrl()
{
return $this->testMode ? self::TEST_URL : self::PRODUCTION_URL;
}

/**
* @return array
*/
public function getChecksumParams()
{
return array_merge(
['txnid', 'amount', 'productinfo', 'firstname', 'email'],
array_map(function($i) { return "udf{$i}"; }, range(1, 10))
);
}

/**
* @param array $params
* @return string
*/
private function getChecksum(array $params)
{
$values = array_map(
function($field) use ($params) {
return array_key_exists($field, $params) ? $params[$field] : '';
},
$this->getChecksumParams()
);

$values = array_merge([$this->getMerchantId()], $values, [$this->getSecretKey()]);

return hash('sha512', implode('|', $values));
}

/**
* @param array $params
* @return Response
*/
public function purchase(array $params)
{
$requiredParams = ['txnid', 'amount', 'firstname', 'email', 'phone', 'productinfo', 'surl', 'furl'];

$params = array_merge(
$this->sanitizeParams($params),
['hash' => $this->getChecksum($params), 'key' => $this->getMerchantId()]
);

$params = array_map(
function($param) { return htmlentities($param, ENT_QUOTES, 'UTF-8', false); },
$params
);

$output = sprintf('<form id="payment_form" method="POST" action="%s">', $this->getServiceUrl());

foreach ($params as $key => $value) {
$output .= sprintf('<input type="text" name="%s" value="%s" />', $key, $value);
}

$output .= '<input id="payment_form_submit" type="submit" value="Proceed to PayUbiz" />' .
'</form>' .
'<script>
document.getElementById(\'payment_form_submit\').style.display = \'none\';
document.getElementById(\'payment_form\').submit();
</script>';

return new Response($output);
}

public function completePurchase(array $params)
{
return new CompletePurchaseResponse($this, $params);
}

private function sanitizeParams(array $params)
{
foreach (['address1', 'address2', 'city', 'state', 'country', 'productinfo', 'email', 'phone'] as $field) {
if (isset($params[$field])) {
$params[$field] = preg_replace('/[^a-zA-Z0-9\-_@\/\s.]/', '', $params[$field]);
}
}

return $params;
}
}

0 comments on commit 00e88a5

Please sign in to comment.