This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/plugins/checkout/PAYPAL.php

249 lines
7.2 KiB
PHP
Raw Normal View History

<?php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software; you can redistribute it and/or
* modify it under the terms of the Open AgileBill License
* License as published at http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Recent modifications by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
2009-08-03 04:10:16 +00:00
* @subpackage Checkout:Paypal
*/
2009-08-03 04:10:16 +00:00
if (defined('PATH_MODULES'))
include_once(PATH_PLUGINS.'checkout/PAYPAL/PAYPAL.php');
else
include_once('PAYPAL/PAYPAL.php');
/**
* The main AgileBill Paypal Payment Class
*
* @package AgileBill
* @subpackage Checkout:Paypal
*/
class plg_chout_PAYPAL extends plg_chout_base_PAYPAL {
public function __construct($checkout_id=false,$multi=false) {
$this->name = 'PAYPAL';
parent::__construct($checkout_id,$multi);
$this->recurr_only = false;
}
# Perform the checkout transaction (new purchase):
2009-08-03 04:10:16 +00:00
public function bill_checkout($amount,$invoice,$currency_iso,$acct_fields,$total_recurring=false,$recurr_bill_arr=array(),$invoices=array()) {
# Validate we have configured the account
if (! $this->cfg['email']) {
global $C_debug;
$C_debug->alert(_('Sorry, unable to use this payment method, it has not been configured properly.'));
return false;
}
# Validate the currency:
2009-08-03 04:10:16 +00:00
if (! $this->validate_currency($currency_iso))
return false;
# Special JPY formatting:
2009-08-03 04:10:16 +00:00
if ($currency_iso == 'JPY')
$amount = round($amount);
# Set the vars
2009-08-03 04:10:16 +00:00
$vals = array(
array('cmd','_cart'),
array('upload','1'),
array('bn','osb_BuyNow_WPS_AU'),
array('no_shipping','1'),
array('no_note','1'),
array('rm','2'),
array('business',$this->cfg['email']),
array('tax_cart','0'),
array('return',$this->success_url.$invoice),
array('cancel_return',$this->decline_url.$invoice),
array('notify_url',$this->return_url),
array('currency_code',$currency_iso),
array('invoice',$invoice),
array('first_name',$acct_fields['first_name']),
array('last_name',$acct_fields['last_name']),
array('payer_business_name',$acct_fields['company']),
array('address_street',$acct_fields['address1']),
array('address_city',$acct_fields['city']),
array('address_state',$acct_fields['state']),
array('address_zip',$acct_fields['zip']),
array('address_country',$acct_fields['country_id']),
array('payer_email',$acct_fields['email']),
array('payer_id',$acct_fields['id'])
);
2009-08-03 04:10:16 +00:00
if (! $invoices)
$invoices[$invoice] = $amount;
2009-08-03 04:10:16 +00:00
$i = 1;
foreach ($invoices as $id => $total) {
array_push($vals,array('item_number_'.$i,'INVOICE'));
array_push($vals,array('item_name_'.$i,sprintf('%s - %s #%s',SITE_NAME,_('Invoice'),$id)));
array_push($vals,array('amount_'.$i,$total));
$i++;
}
2009-08-03 04:10:16 +00:00
# Calculate the payment fee to be added
switch ($this->cfg['feetype']) {
case '2':
array_push($vals,array('item_number_'.$i,'PAYFEE'));
array_push($vals,array('item_name_'.$i,_('Payment Processing Fee')));
array_push($vals,array('amount_'.$i,round($this->cfg['fee'],2)));
2009-08-03 04:10:16 +00:00
break;
2009-08-03 04:10:16 +00:00
case '1':
array_push($vals,array('item_number_'.$i,'PAYFEE'));
array_push($vals,array('item_name_'.$i,_('Payment Processing Fee')));
array_push($vals,array('amount_'.$i,round($amount*$this->cfg['fee'],2)));
break;
}
$this->post_vars(sprintf('%s/cgi-bin/webscr',$this->getLocation()),$vals);
return true;
}
# Postback Validation
2009-08-03 04:10:16 +00:00
public function postback() {
# Read the post from PayPal system and add 'cmd'
global $_POST,$C_debug;
2010-11-29 22:41:08 +00:00
$ret = array();
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
2009-08-03 04:10:16 +00:00
$req .= sprintf('&%s=%s',$key,$value);
}
2009-08-03 04:10:16 +00:00
# Post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
2009-08-03 04:10:16 +00:00
$header .= sprintf("Content-Length: %s\r\n\r\n",strlen($req));
# Validate
2010-11-29 22:41:08 +00:00
$fp = fsockopen($this->getLocation(false,true),80,$errno,$errstr,30);
2009-08-03 04:10:16 +00:00
if (! $fp) {
$C_debug->error(__FILE__,__METHOD__,sprintf('Unable to connect to %s',$this->getLocation(false,true)));
} else {
2010-11-29 22:41:08 +00:00
$ret['invoice_id'] = $_POST['invoice'];
$ret['transaction_id'] = $_POST['txn_id'];
$ret['currency'] = $_POST['mc_currency'];
if (! empty($_POST['mc_gross']))
$ret['amount'] = $_POST['mc_gross'];
else
$ret['amount'] = $_POST['payment_gross'];
# Get a list of items paid
$i = array('item_number','item_name','mc_gross_');
foreach ($_POST as $k => $v)
foreach ($i as $j)
if (preg_match("/^{$j}([0-9]+)/",$k,$m))
$ret['items'][$m[1]][$j] = $v;
2009-08-03 04:10:16 +00:00
fputs($fp,$header.$req);
while (! feof($fp)) {
$res = trim(strtoupper(fgets($fp,1024)));
# HTTP traffic
if (! $res
|| preg_match('/^HTTP/',$res)
|| preg_match('/^DATE/',$res)
|| preg_match('/^SERVER/',$res)
|| preg_match('/^SET-COOKIE/',$res)
|| preg_match('/^CONNECTION/',$res)
|| preg_match('/^CONTENT-TYPE/',$res))
continue;
switch ($res) {
case 'VERIFIED':
# Get the payment status
$ret['status'] = true;
2010-11-29 22:41:08 +00:00
if (isset($_POST['txn_type']))
switch($_POST['txn_type']) {
case 'cart': $ret['status'] = true; break;
default: $ret['status'] = false; break;
}
2009-08-03 04:10:16 +00:00
if ($ret['status'] != false) {
switch($_POST['payment_status']) {
case 'Canceled_Reversal': $ret['status'] = true; break;
case 'Completed': $ret['status'] = true; break;
default: $ret['status'] = false; break;
}
}
# Get the processor details
$this->getDetailsName($this->name);
2010-11-29 22:41:08 +00:00
$ret['checkout_id'] = $this->checkout_id;
2009-08-03 04:10:16 +00:00
$cfg = unserialize($this->flds['plugin_data']);
if ($_POST['receiver_email'] == $cfg['email']) {
include_once(PATH_MODULES.'checkout/checkout.inc.php');
2010-11-29 22:41:08 +00:00
$checkout = new checkout($this->checkout_id);
$C_debug->error(__FILE__,__METHOD__,sprintf("Calling checkout %s: with: %s",$this->name,print_r($ret,true)));
$checkout->postback($ret,$this->name);
}
2009-08-03 04:10:16 +00:00
fclose($fp);
header('HTTP/1.1 200 OK');
header('Status: 200 OK');
return;
break;
default:
# Log for manual investigation
$C_debug->error(__FILE__,__METHOD__,sprintf('Postback for Invoice %s is %s (%s)',$ret['invoice_id'],$res,serialize($res)));
fclose($fp);
header('HTTP/1.0 404 Not Found');
return false;
}
}
}
2009-08-03 04:10:16 +00:00
header('HTTP/1.0 500 Temporary Failure');
}
}
# Postback Function
2009-08-03 04:10:16 +00:00
if (empty($VAR) && empty($VAR['do'])) {
2010-11-29 22:41:08 +00:00
require_once('../../config.inc.php');
2009-08-03 04:10:16 +00:00
require_once(PATH_ADODB.'adodb.inc.php');
require_once(PATH_CORE.'database.inc.php');
require_once(PATH_CORE.'setup.inc.php');
2010-11-29 22:41:08 +00:00
require_once(PATH_CORE.'list.inc.php');
require_once(PATH_CORE.'translate.inc.php');
require_once(PATH_MODULES.'module.inc.php');
2009-08-03 04:10:16 +00:00
$C_debug = new CORE_debugger;
2010-11-29 22:41:08 +00:00
$C_translate = new CORE_translate;
2009-08-03 04:10:16 +00:00
$C_db = &DB();
$C_setup = new CORE_setup;
2010-11-29 22:41:08 +00:00
$C_list = new CORE_list;
$plg = new plg_chout_PAYPAL;
2009-08-03 04:10:16 +00:00
$plg->postback();
}
?>