Skip to content

Instantly share code, notes, and snippets.

@neo22s
Created December 13, 2015 19:42
Show Gist options
  • Save neo22s/3f01738f7ecbf457a4e5 to your computer and use it in GitHub Desktop.
Save neo22s/3f01738f7ecbf457a4e5 to your computer and use it in GitHub Desktop.
Paysbuy integration for php
<?
include 'paysbuy.php';
//config the gateway
//by default is FALSE, put this to true to test
paysbuy::$sandbox = TRUE;
//set your emails here if none written demo email will be used
//paysbuy::$email = 'your mail account here';
if ($_POST)
{
if ( paysbuy::result() === TRUE )
echo 'Paid do something';
else
echo 'NOT paid';
}
else
{
//TODO!!
//sample fill it dynamically
$invoice = "INV-".($inv=time());
$description ="Product Description ".$inv;
$price = "10";
$postURL = "index.php";//change this where you check the result of the payment
?>
<form name="paysbuy" id="paysbuy" method="post" action="<?=paysbuy::url_gateway()?>">
<input type="Hidden" Name="psb" value="psb"/>
<input Type="Hidden" Name="biz" value="<?=paysbuy::$email?>"/>
<input Type="Hidden" Name="inv" value="<?=$invoice?>"/>
<input Type="Hidden" Name="itm" value="<?=$description?>"/>
<input Type="Hidden" Name="amt" value="<?=$price?>"/>
<input Type="Hidden" Name="postURL" value="<?=$postURL?>"/>
<input type="image" src="https://www.paysbuy.com/imgs/S_click2buy.gif" border="0" name="submit" alt="Make it easier,PaySbuy - it's fast,free and secure!"/>
</form >
<?
}
?>
<?php
/**
* paysbuy helper class
*
* @category Payment
* @author Chema <[email protected]>
* @copyright (c) 2015 garridodiaz.com
* @license GPL v3
*/
class paysbuy
{
public static $email = '[email protected]';
public static $sandbox = FALSE;
const url_gateway = 'https://www.paysbuy.com/paynow.aspx';
const url_recheck = 'https://www.paysbuy.com/getinvoice/getinvoicestatus.asmx/GetInvoice';
const url_sandbox_gateway = 'https://demo.paysbuy.com/paynow.aspx';
const url_sandbox_recheck = 'https://demo.paysbuy.com/getinvoice/getinvoicestatus.asmx/GetInvoice';
public static function result()
{
// Security Checking : The results from Paysbuy should be completed and not corrupted.
if (empty($_POST['result']) OR empty($_POST['apCode']) OR empty($_REQUEST['amt']))
return FALSE;
// Value from untrusts identity which Act as Paysbuy Co. Ltd.
$payment_status = substr($_POST["result"], 0, 2);
$cartnumber = trim(substr($_POST["result"],2));
$amount = $_POST['amt'];
$psbRef = $_POST['apCode'];
//correct payment?
if($payment_status == '00' AND paysbuy::recheck(self::email, $cartnumber, $psbRef,$amount) )
return TRUE;
else
return FALSE;
}
// Original Source by : Siambox.com http://www.thaihosttalk.com/index.php?topic=19899.0
public static function recheck($psbmail, $cart, $psbRef, $amount)
{
$query = "invoiceNo=$cart&merchantEmail=$psbmail&strApCode=$psbRef";
// Request URI (Secure)
$ch = curl_init((self::$sandbox)?self::url_sandbox_recheck:self::url_recheck);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$query");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlResponse = curl_exec($ch);
curl_close($ch);
$StatusResult = XMLGetValue($xmlResponse, 'StatusResult');
$AmountResult = XMLGetValue($xmlResponse, 'AmountResult');
if ($StatusResult == 'Accept' AND $AmountResult == $amount)
return TRUE;
else
return FALSE; // Reject the payment.
}
private static function XMLGetValue($msg, $str)
{
$str1 = "<$str>";
$str2 = "</$str>";
$start_pos = strpos($msg, $str1);
$stop_post = strpos($msg, $str2);
$start_pos += strlen($str1);
return substr($msg, $start_pos, $stop_post - $start_pos);
}
/**
* get the url of the gateway
* @return string
*/
public static function url_gateway()
{
return ((self::$sandbox)?self::url_sandbox_gateway:self::url_gateway).'?c=true&m=true&j=true&a=true&p=true&psb=true';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment