CoderZone.org

Category: >> PHP Code >> Accept Payments with Google Checkout Bookmark and Share

<< lastnext >>

Snippet Name: Accept Payments with Google Checkout

Description: Google Checkout is an online checkout process that you integrate with your website, which enables your customers to buy from you quickly, easily and securely, using only one account and a single username and password for purchases. Once a customer purchases something, you can use Google Checkout to charge the amount, process their orders, and receive payments to your personal or business bank account.

Also see:
» PayPal Instant Payment Notificatio...
» Standard PayPal Payment Integration
» CyberCash payment class
» Add dashes to credit card numbers
» Validate credit card numbers
» Active Processes (DBA)
» Automatically process email
» Delete by wildcard

Comment: (none)

Author:
Language: PHP
Highlight Mode: PHP
Last Modified: March 28th, 2012

<?php
 
require_once ('google/ResponseHandlerAPIFunctions.php');
 
require_once('google/config.php');
 
require_once('google/classes/gCart.php'); 
 
require_once('google/classes/gItem.php'); 
 
require_once('google/classes/gMessageRedirect.php');
 
require_once('google/classes/gMerchantCalculations.php');
 
$googleMode = 1;//0 for sandbox and 1 for live mode
 
$merchant_id='1234567893';
$mercant_key='ZWFdgdetr#$df';
 
$GCheckout = new gCart($merchant_id,$mercant_key);
 
$GCheckout->setMerchantCheckoutFlowSupport("http://www.mystie.com/index.php","http://www.mystie.com/payment_success.php?action=success");
 
$id='returnid';
 
$varcounter =2;// total products
 
$shop_items[0]= new gItem('first product name','its description','2','2220');//product name, description, quantity, amount
$shop_items[0]->setPrivateItemData($id);
 
$shop_items[1]= new gItem('second product name','its description','2','2120');//product name, description, quantity, amount
$shop_items[1]->setPrivateItemData($id);
 
$shop_items[$varcounter]= new gItem("Shipping","Amount for shipping","1",'20');//20 is the shipping amount
$shop_items[$varcounter+1]= new gItem("Tax","Tax amount","1",'110');//110 is the tax amounr
$shop_items[$varcounter+2]= new gItem("Discount","Coupon discount","1",'-25');//25 is the discount amount
 
$GCheckout->addItems($shop_items);
$newvar = $GCheckout->getCart();
//echo  $newvar;exit;
$newvar1 = strstr($newvar, '</merchant-checkout-flow-support>');
 
$firstpartlen=strlen($newvar)-strlen($newvar1);//getting the length diff
 
$firstpart=substr($newvar,0,$firstpartlen); 
 
$secondpart=substr($newvar,-strlen($newvar1)); 
 
$final_xml=$firstpart.$insertvar.$secondpart;
 
//echo  $final_xml;exit;
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Google Checkout</title>
<script language="javascript" type="text/javaScript">
 
function submitForm()
 
{
 
	document.confirmation.submit();
 
}
 
</script>
</head>
<body   onload="submitForm();">
<?PHP if($googleMode == 0){?><form  method="post" action="https://sandbox.google.com/checkout/cws/v2/Merchant/<?php echo $merchant_id; ?>/checkout"  name="confirmation" accept-charset="utf-8">
 
<?PHP }else {?><form method="POST" action="https://checkout.google.com/cws/v2/Merchant/<?php echo $merchant_id; ?>/checkout"  name="confirmation" accept-charset="utf-8"><?PHP } ?>
 
<input type="hidden" name="cart" value="<?php echo base64_encode($final_xml);?>"/>
 
<input type="hidden" name="_charset_"/>
 
<input type="hidden" name="signature" value="<?php echo base64_encode($GCheckout->getSignature($final_xml));?>" />
 
</form> 
 
</body>
</html>
 
Need to set the call back url in the merchant’s account page.
Please refer the following link : http://checkout.google.com/support/sell/bin/answer.py?answer=134463.
 
The callback page will run in the backend. You will all the return values from here including transaction id, customer values etc. You can update the database from here.
Call back page
 
<?php
 
$merchant_id='1234567893';
$mercant_key='ZWFdgdetr#$df';
 
//you can check the return value from here by sending a mail to your email id.
foreach($HTTP_POST_VARS as $key=>$value)
{
$privatedata.=$key."=".$value."<br />";
 
}
$header 	 = "From:Google checkout call back<mytest@mail.com>\n";
$header    .= 'MIME-Version: 1.0' . "\r\n";
$header    .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
@mail("mymail@gmail.com","Comment From google checkout",$privatedata,$header); 
 
// check your mail for return values and you can update the database using the reutnr value
if(trim($_REQUEST['_type'])=="new-order-notification")
{
	if($_REQUEST['shopping-cart_items_item-1_merchant-private-item-data'])
	{
		$returnid=trim($_REQUEST['shopping-cart_items_item-1_merchant-private-item-data']);// this is the custom id which we have passed
	}
 
	$google_transaction_id=$_REQUEST['google-order-number'];
 
	//firstly check whether the google order number is already in your database. if it is not in your database , firstly you have o charge the order.. (or it can be done by the merchant from his account itself.)
 
	///For charging the order
	$totAmount='200000';
	$objGoogle = new GoogleRequest($merchant_id, $mercant_key, 'sandbox', 'USD');
	$objGoogle->SendChargeOrder($google_transaction_id,$totAmount);
 
}
 
?>
 
 
// For charging and refund
 
<?php
 
//you can charge the order from you site itself. (or it can be done by the merchant from his account itself.)
 
include_once('google_charge_refund_class.php');
 
///For charging the order
$totAmount='200000';
$objGoogle = new GoogleRequest($merchant_id, $mercant_key, 'sandbox', 'USD');
$objGoogle->SendChargeOrder($google_transaction_id,$totAmount);
 
// for refund order
 
$objGoogle = new GoogleRequest($merchant_id, $mercant_key, 'sandbox', 'USD');
$refund_status=$objGoogle->SendRefundOrder($transaction_id, $refund_amount, '', '');
 
?>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 11