Posts Tagged paypal php sdk

PHP Class for the PayPal Payments Pro NVP API

I wrote this PHP class to handle making calls against PayPal’s NVP API. It allows you to simply create a nested array set of all the data and pass that into the class. The result is an array with all of the PayPal fields returned as well as extra array sets for things like errors, order items, fraud filters, etc.

I’ve been using it awhile already and it’s saved me quite a bit of time. Hopefully it’ll do the same for you!

Complete PHP Class for PayPal Payments Pro NVP API

———————————-
DoDirectPayment Usage Sample
———————————-

<?php
# Add PayPal class
require_once('../PayPalApps/paypal.nvp.class.php');

# Setup the PayPal object
$ppConfig = array('Sandbox' => true);
$pp = new PayPal($ppConfig);

# Populate data arrays for API call.
$DPFields = array(
					'paymentaction' => 'Sale',
					'ipaddress' => '192.168.1.34',
					'returnfmfdetails' => '1'
				);

$CCDetails = array(
					'creditcardtype' => 'Visa',
					'acct' => '4635800000835916',
					'expdate' => '052012',
					'cvv2' => '123',
					'startdate' => ''
				);

$PayerInfo = array(
					'email' => 'tester@testerson.com',
					'business' => 'Testers, LLC'
				);

$PayerName = array(
					'salutation' => 'Mr.',
					'firstname' => 'Tester',
					'middlename' => 'T.',
					'lastname' => 'Testerson',
					'suffix' => 'Jr.'
				);

$BillingAddress = array(
						'street' => '123 Test Ave.',
						'street2' => 'Apt. 3',
						'city' => 'Testersville',
						'state' => 'MO',
						'countrycode' => 'US',
						'zip' => '64030',
						'phonenum' => '555-555-5555'
					);

$ShippingAddress = array(
						'shiptoname' => 'Mr. Tester Testerson Jr.',
						'shiptostreet' => '123 Test Ave',
						'shiptostreet2' => 'Apt. 3',
						'shiptocity' => 'Testersville',
						'shiptostate' => 'MO',
						'shiptozip' => '64030',
						'shiptocountrycode' => 'US',
						'shiptophonenum' => '555-555-5555'
						);

$PaymentDetails = array(
						'amt' => '25.00',
						'currencycode' => 'USD',
						'itemamt' => '15.00',
						'shippingamt' => '10.00',
						'handlingamt' => '',
						'taxamt' => '',
						'desc' => 'This is a test order.',
						'custom' => '',
						'invnum' => '1234-ABC',
						'buttonsource' => '',
						'notifyurl' => ''
					);

# Now combine your data arrays into a single nested array to pass into the class.
$DPData = array(
				'DPFields' => $DPFields,
				'CCDetails' => $CCDetails,
				'PayerInfo' => $PayerInfo,
				'PayerName' => $PayerName,
				'BillingAddress' => $BillingAddress,
				'ShippingAddress' => $ShippingAddress,
				'PaymentDetails' => $PaymentDetails);

# Now we pass the nested array of all our data into the class.
$DPResult = $pp -> DoDirectPayment($DPData);

# Now lets study the result array
echo '<pre />';
print_r($DPResult);
exit();
?>

———————————-
SetExpressCheckout Usage Sample
———————————-

<?php
# Add PayPal class
require_once('../PayPalApps/paypal.nvp.class.php');

# Setup the PayPal object
$ppConfig = array('Sandbox' => true);
$pp = new PayPal($ppConfig);

# Populate data arrays for API call.
$SECFields = array(
					'returnurl' => 'http://www.domain.com/return.php',
					'cancelurl' => 'http://www.domain.com/cancel.php',
					'paymentaction' => 'Sale'
					);

$PaymentDetails = array(
						'amt' => '18.00',
						'currencycode' => 'USD',
						'itemamt' => '10.00',
						'shippingamt' => '5.00',
						'handlingamt' => '2.00',
						'taxamt' => '1.00',
						'desc' => 'This is a test order.',
						'custom' => '',
						'invnum' => '1234-ABC'
						);

# Now combine your data arrays into a single nested array to pass into the class.
$SECData = array('SECFields' => $SECFields, 'PaymentDetails' => $PaymentDetails);

# Now we pass the nested array of all our data into the class.
$SECResult = $pp -> SetExpressCheckout($SECData);

# Now lets study the result array
echo '<pre />';
print_r($SECResult);
exit();

# Now we can just use the returned REDIRECTURL field to redirect the user to PayPal based on our input.
header('Location: ' . $SECResult['REDIRECTURL']);
?>

, , , , ,

1 Comment