PHP code example of kreative / f3-pypl

1. Go to this page and download the library: Download kreative/f3-pypl library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kreative / f3-pypl example snippets


// F3-PYPL config
$ppconfig = array(
'user'=>'apiusername',
'pass'=>'apipassword',
'signature'=>'apisignature',
'endpoint'=>'sandbox',
'apiver'=>'204.0',
'return'=>'http://',
'cancel'=>'http://',
'log'=>'1'
);

// Instantiate the class with config
$paypal=new PayPal($ppconfig);

// Create & redirect
$paypal=new PayPal;
$result=$paypal->create("Sale","EUR","10.00");
$f3->reroute($result['redirect']);

// Return & Complete
$token=$f3->get('GET.token');
$payerid=$f3->get('GET.PayerID');

$paypal=new PayPal;
$result=$paypal->complete($token, $payerid);

// Process credit/debit card payment
$paypal=new PayPal;
$result=$paypal->dcc("Sale", "EUR", "10.00", $cardtype, $cardnumber, $expdate, $cvv, $ipaddress);

setShippingAddress($buyerName, $addressLine1, $addressLine2, $townCity, $regionProvince, $zipCode, $countryCode);

// Example
$paypal->setShippingAddress('Sherlock Holmes', '221b Baker St', 'Marylebone', 'London City', 'London', 'NW16XE', 'GB');

setLineItem($itemName, $itemQty, $itemPrice);

// Example
$paypal->setLineItem("Phone Case", 1, "10.00");
$paypal->setLineItem("Smart Phone", 1, "100.00");
$paypal->setLineItem("Screen Protector", 5, "1.00");

copyBasket($basket, $name, $amount);

// Example if using the default example naming conventions
$basket->set('name','peach');
$basket->set('amount','10.00');
$basket->set('qty','1');
$basket->save();
$basket->reset();

$basketcontents = $basket->find();  // array of basket items

$paypal = new PayPal;
$itemtotal=$paypal->copyBasket($basketcontents);

// Example if using your own naming conventions
$basket->set('item','peach');
$basket->set('cost','10.00');
$basket->set('quantity','2');
$basket->save();
$basket->reset();

$basketcontents = $basket->find();  // array of basket items

$paypal = new PayPal;
$itemtotal=$paypal->copyBasket($basketcontents,'item','quantity','cost'); // returns 20.

setShippingAmt($amount);

// Example
$paypal->setShippingAmt('10.00');

setTaxAmt($amount);

// Example
$paypal->setTaxAmt('10.00');

create($paymentAction,$currencyCode,$totalAmount,$optional[]);

// Example
$result=$paypal->create("Sale","EUR","171.00");

// Check the API call was successful
if ($result['ACK']!='Success'){
// Handle the API error
die('Error with API call -'.$result["L_ERRORCODE0"]);
} else {
// Redirect the buyer to PayPal
$f3->reroute($result['redirect']);
}

getDetails($ecToken);

// Example
// Retrieve EC Token from the URL
$token=$f3->get('GET.token');
// Retrieve the buyers shipping address via the GetExpressCheckout API
$address=$paypal->getDetails($token);

updateShippingAddress($token, $name, $street1, $street2, $city, $state, $zip, $countrycode);

// Example
// Retrieve EC Token from the URL
$token=$f3->get('GET.token');
// Retreive the buyers shipping address via the GetExpressCheckout API
$paypal = new PayPal;
$buyerdetails = $paypal->getDetails($token);

$paypal->updateShippingAddress($token, $buyerdetails[SHIPTONAME], $buyerdetails[SHIPTOSTREET], $buyerdetails[SHIPTOSTREET2], $buyerdetails[SHIPTOCITY], $buyerdetails[SHIPTOSTATE], $buyerdetails[SHIPTOZIP], $buyerdetails[SHIPTOCOUNTRYCODE]);

complete($ecToken, $payerId);

// Example
// Retrieve EC Token & PayerID from the URL
$token=$f3->get('GET.token');
$payerid=$f3->get('GET.PayerID');

//Complete the Transaction
$result=$paypal->complete($token, $payerid);

// Check the API call was successful
if ($result['ACK'] != 'Success' && $result['ACK'] != 'SuccessWithWarning'){
// Handle the API error
die('Error with API call -'.$result["L_ERRORCODE0"]);
} else {
// Redirect the buyer a receipt or order confirmation page
// Store the status & transaction ID for your records
}

refund($txnId, [$type, $currencycode, $amt]);

// Fake Transaction ID for 20..
$txnId='FAKETXNID';

//Partial Refund
refund($txnId, 'Partial', 'EUR', '10.00']);

//Full Refund
refund($txnId);

$f3->route('GET /expresscheckout',
function ($f3) {

//Instantiate the class
$paypal = new PayPal;

// Set the shipping address (if t Items
$paypal->setLineItem("Phone Case", 1, "10.00"); //10.00
$paypal->setLineItem("Smart Phone", 1, "200.00"); //200.00
$paypal->setLineItem("Screen Protector", 5, "1.00"); //5.00

// Set Shipping amount
$paypal->setShippingAmt("10.00");

// Set Tax
$paypal->setTaxAmt("21.00");

/*
Prevent the buyer from changing the 
shipping address on the PayPal website.
*/
$optional=array('ADDROVERRIDE'=>1);

// Create Transaction, Total amount = Cart Items + Shipping Amount + Tax Amount
$result = $paypal->create("Sale", "EUR", "246.00", $optional);

// Reroute buyer to PayPal with resulting transaction token
if ($result['ACK'] != 'Success') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
// Redirect Buyer to PayPal
$f3->reroute($result['redirect']);
}

}
);

$f3->route('GET /review',
function($f3) {
// grab token from URL
$token=$f3->get('GET.token');

//Instantiate the Class
$paypal=new PayPal;

// Get Express Checkout details from PayPal
$result=$paypal->getDetails($token);

// Check for successful response
if ($result['ACK'] != 'Success') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
// Use details to render an order review page
// Show shipping address order details
}

}
);

$f3->route('GET /summary',
function($f3) {
// grab token & PayerID from URL
$token=$f3->get('GET.token');
$payerid=$f3->get('GET.PayerID');

//Instantiate the Class
$paypal=new PayPal;

// complete the transaction
$result=$paypal->complete($token, $payerid);

// Check for successful response
if ($result['ACK'] != 'Success' && $result['ACK'] != 'SuccessWithWarning') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
// Update back office - save transaction id, payment status etc
// Display thank you/receipt to the buyer.
}

}
);

$f3->route('GET /expresscheckout',
function ($f3) {

//Instantiate the class
$paypal = new PayPal;

// Set Cart Items manually or use copyBasket method.
$paypal->setLineItem("Phone Case", 1, "10.00"); //10.00
$paypal->setLineItem("Smart Phone", 1, "200.00"); //200.00

// Set Tax
$paypal->setTaxAmt("21.00");

// Create Transaction, Total amount = Cart Items + Shipping Amount + Tax Amount
$result = $paypal->create("Sale", "EUR", "231.00", $optional);

// Reroute buyer to PayPal with resulting transaction token
if ($result['ACK'] != 'Success') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
// Redirect Buyer to PayPal
$f3->reroute($result['redirect']);
}

}
);

$f3->route('GET /review',
function($f3) {
// grab token from URL
$token=$f3->get('GET.token');

//Instantiate the Class
$paypal=new PayPal;

// Get Express Checkout details from PayPal
$buyerdetails=$paypal->getDetails($token);

// Check for successful response
if ($buyerdetails['ACK'] != 'Success') {
// Handle API error code
die('Error with API call - ' . $buyerdetails["L_ERRORCODE0"]);
} else {
// Use details of $result to render an order review page
// Show shipping address order details

// Update the session to store the new shipping address
// this address is passed in the final API call
$paypal->updateShippingAddress($token, $buyerdetails[SHIPTONAME], $buyerdetails[SHIPTOSTREET], $buyerdetails[SHIPTOSTREET2], $buyerdetails[SHIPTOCITY], $buyerdetails[SHIPTOSTATE], $buyerdetails[SHIPTOZIP], $buyerdetails[SHIPTOCOUNTRYCODE]);

// Update the session & order total with a new shipping amount
$paypal->updateShippingAmt($token, '10.00');

}

}
);

$f3->route('GET /summary',
function($f3) {
// grab token & PayerID from URL
$token=$f3->get('GET.token');
$payerid=$f3->get('GET.PayerID');

//Instantiate the Class
$paypal=new PayPal;

// complete the transaction
$result=$paypal->complete($token, $payerid);

// Check for successful response
if ($result['ACK'] != 'Success' && $result['ACK'] != 'SuccessWithWarning') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
// Update back office - save transaction id, payment status etc
// Display thank you/receipt to the buyer.
}

}
);

$f3->route('GET /rp',
function ($f3) {

//Instantiate the Recurring Payments Class
$paypal = new PayPalRP;

//Set a descriptive name for the Recurring Payment
$result = $paypal->setupRP("Test Subscription");

// Reroute buyer to PayPal with resulting transaction token
if ($result['ACK'] != 'Success') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
// Redirect Buyer to PayPal
$f3->reroute($result['redirect']);
}
}
);

$f3->route('GET /rpcreate',
function ($f3) {

//Instantiate the Recurring Payments Class
$paypal = new PayPalRP;

//Define the terms of the recurring payment profile.
$amt="10.00";
$startdate=date('Y-m-d')."T00:00:00Z"; // UTC/GMT format eg 2016-10-25T18:00:00Z
$period="Day"; // Day, Week, SemiMonth, Month, Year
$frequency="2"; // Cannot exceed one year
$currency="EUR";

$paypal->setRPDetails($amt, $startdate, $period, $frequency, $currency);

// grab token from URL
$token = $f3->get('GET.token');

//Create Recurring Payment Profile
$result = $paypal->createRP($token);

// Reroute buyer to PayPal with resulting transaction token
if ($result['ACK'] != 'Success' && $result['ACK'] != 'SuccessWithWarning') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
exit(print_r($result));
}

}
);

$f3->route('GET /basetup',
function ($f3) {

//Instantiate the Reference Transactions Class
$paypal = new PayPalRT;

//Set a descriptive name for the Recurring Payment
$result = $paypal->setupRP("Test Subscription");

// Reroute buyer to PayPal with resulting transaction token
if ($result['ACK'] != 'Success') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
// Redirect Buyer to PayPal
$f3->reroute($result['redirect']);
}
}
);

$f3->route('GET /bacreate',
    function ($f3) {

        // grab token & PayerID from URL
        $token = $f3->get('GET.token');

        // complete the transaction
        $paypal = new PayPalRT;
        $result = $paypal->createBA($token);

		if ($result['ACK'] != 'Success' && $result['ACK'] != 'SuccessWithWarning') {
            // Handle API error code
            die('Error with API call - ' . $result["L_ERRORCODE0"]);
        } else {
            print_r($result);
            // Update back office - save the billing agreement id.
            // Display thank you/receipt to the buyer.
        }

    }
);

// Create the transaction
$paypal = new PayPalRT;
$result = $paypal->doRT($billingAgreementId, 'Sale', 'EUR', '10.00');

if ($result['ACK'] != 'Success' && $result['ACK'] != 'SuccessWithWarning') {
    // Handle API error code
    die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
    print_r($result);
    // Update back office - save transaction id, payment status etc
    // Display thank you/receipt to the buyer if present.
}

$f3->route('GET /dcc',
function ($f3) {

//Instantiate the PayPal Class
$paypal = new PayPal;

$paymentaction="Sale"; // Can be Sale or Authorization
$currencycode="EUR"; // 3 Character currency code
$amount="10.00"; // Amount to charge
$cardtype='Visa'; // Visa, MasterCard, Discover etc
$cardnumber='XXXXXXXXXXXXXXXX'; // Valid card number
$expdate='122020'; // format MMYYYY
$cvv='123'; // Valid security code
$ipaddress='127.0.0.1';

$result=$paypal->dcc($paymentaction, $currencycode, $amount, $cardtype, $cardnumber, $expdate, $cvv, $ipaddress);

// $result will contain an associative array of the API response.  Store the useful bits like status & transaction ID.

if ($result['ACK'] != 'Success' && $result['ACK'] != 'SuccessWithWarning') {
// Handle API error code
die('Error with API call - ' . $result["L_ERRORCODE0"]);
} else {
exit(print_r($result));
}

}
);