forked from LiveCarta/PayPal-PHP-SDK
Initial commit
This commit is contained in:
111
sample/payments/CreatePayment.php
Normal file
111
sample/payments/CreatePayment.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
// # CreatePaymentSample
|
||||
// This sample code demonstrate how you can process
|
||||
// a payment with a credit card.
|
||||
// API used: /v1/payments/payment
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
use PayPal\Api\Address;
|
||||
use PayPal\Api\Amount;
|
||||
use PayPal\Api\CreditCard;
|
||||
use PayPal\Api\Payer;
|
||||
use PayPal\Api\Payment;
|
||||
use PayPal\Api\FundingInstrument;
|
||||
use PayPal\Api\Transaction;
|
||||
use PayPal\Rest\ApiContext;
|
||||
|
||||
// ### Address
|
||||
// Base Address object used as shipping or billing
|
||||
// address in a payment. [Optional]
|
||||
$addr = new Address();
|
||||
$addr->setLine1("3909 Witmer Road");
|
||||
$addr->setLine2("Niagara Falls");
|
||||
$addr->setCity("Niagara Falls");
|
||||
$addr->setState("NY");
|
||||
$addr->setPostal_code("14305");
|
||||
$addr->setCountry_code("US");
|
||||
$addr->setPhone("716-298-1822");
|
||||
|
||||
// ### CreditCard
|
||||
// A resource representing a credit card that can be
|
||||
// used to fund a payment.
|
||||
$card = new CreditCard();
|
||||
$card->setType("visa");
|
||||
$card->setNumber("4417119669820331");
|
||||
$card->setExpire_month("11");
|
||||
$card->setExpire_year("2019");
|
||||
$card->setCvv2("012");
|
||||
$card->setFirst_name("Joe");
|
||||
$card->setLast_name("Shopper");
|
||||
$card->setBilling_address($addr);
|
||||
|
||||
// ### FundingInstrument
|
||||
// A resource representing a Payer's funding instrument.
|
||||
// Use a Payer ID (A unique identifier of the payer generated
|
||||
// and provided by the facilitator. This is required when
|
||||
// creating or using a tokenized funding instrument)
|
||||
// and the `CreditCardDetails`
|
||||
$fi = new FundingInstrument();
|
||||
$fi->setCredit_card($card);
|
||||
|
||||
// ### Payer
|
||||
// A resource representing a Payer that funds a payment
|
||||
// Use the List of `FundingInstrument` and the Payment Method
|
||||
// as 'credit_card'
|
||||
$payer = new Payer();
|
||||
$payer->setPayment_method("credit_card");
|
||||
$payer->setFunding_instruments(array($fi));
|
||||
|
||||
// ### Amount
|
||||
// Let's you specify a payment amount.
|
||||
$amount = new Amount();
|
||||
$amount->setCurrency("USD");
|
||||
$amount->setTotal("1.00");
|
||||
|
||||
// ### Transaction
|
||||
// A transaction defines the contract of a
|
||||
// payment - what is the payment for and who
|
||||
// is fulfilling it. Transaction is created with
|
||||
// a `Payee` and `Amount` types
|
||||
$transaction = new Transaction();
|
||||
$transaction->setAmount($amount);
|
||||
$transaction->setDescription("This is the payment description.");
|
||||
|
||||
// ### Payment
|
||||
// A Payment Resource; create one using
|
||||
// the above types and intent as 'sale'
|
||||
$payment = new Payment();
|
||||
$payment->setIntent("sale");
|
||||
$payment->setPayer($payer);
|
||||
$payment->setTransactions(array($transaction));
|
||||
|
||||
// ### Api Context
|
||||
// Pass in a `ApiContext` object to authenticate
|
||||
// the call and to send a unique request id
|
||||
// (that ensures idempotency). The SDK generates
|
||||
// a request id if you do not pass one explicitly.
|
||||
$apiContext = new ApiContext($cred, 'Request' . time());
|
||||
|
||||
// ### Create Payment
|
||||
// Create a payment by posting to the APIService
|
||||
// using a valid ApiContext
|
||||
// The return object contains the status;
|
||||
try {
|
||||
$payment->create($apiContext);
|
||||
} catch (\PPConnectionException $ex) {
|
||||
echo "Exception: " . $ex->getMessage() . PHP_EOL;
|
||||
var_dump($ex->getData());
|
||||
exit(1);
|
||||
}
|
||||
?>
|
||||
<html>
|
||||
<body>
|
||||
<div>
|
||||
Created payment:
|
||||
<?php echo $payment->getId();?>
|
||||
</div>
|
||||
<pre><?php var_dump($payment->toArray());?></pre>
|
||||
<a href='../index.html'>Back</a>
|
||||
</body>
|
||||
</html>
|
||||
93
sample/payments/CreatePaymentUsingPayPal.php
Normal file
93
sample/payments/CreatePaymentUsingPayPal.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
// # Create Payment using PayPal as payment method
|
||||
// This sample code demonstrates how you can process a
|
||||
// PayPal Account based Payment.
|
||||
// API used: /v1/payments/payment
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
use PayPal\Api\Address;
|
||||
use PayPal\Api\Amount;
|
||||
use PayPal\Api\Payer;
|
||||
use PayPal\Api\Payment;
|
||||
use PayPal\Api\FundingInstrument;
|
||||
use PayPal\Api\RedirectUrls;
|
||||
use PayPal\Api\Transaction;
|
||||
use PayPal\Rest\ApiContext;
|
||||
session_start();
|
||||
|
||||
// ### Payer
|
||||
// A resource representing a Payer that funds a payment
|
||||
// Use the List of `FundingInstrument` and the Payment Method
|
||||
// as 'credit_card'
|
||||
$payer = new Payer();
|
||||
$payer->setPayment_method("paypal");
|
||||
|
||||
// ### Amount
|
||||
// Let's you specify a payment amount.
|
||||
$amount = new Amount();
|
||||
$amount->setCurrency("USD");
|
||||
$amount->setTotal("1.00");
|
||||
|
||||
// ### Transaction
|
||||
// A transaction defines the contract of a
|
||||
// payment - what is the payment for and who
|
||||
// is fulfilling it. Transaction is created with
|
||||
// a `Payee` and `Amount` types
|
||||
$transaction = new Transaction();
|
||||
$transaction->setAmount($amount);
|
||||
$transaction->setDescription("This is the payment description.");
|
||||
|
||||
// ### Redirect urls
|
||||
// Set the urls that the buyer must be redirected to after
|
||||
// payment approval/ cancellation.
|
||||
$baseUrl = getBaseUrl();
|
||||
$redirectUrls = new RedirectUrls();
|
||||
$redirectUrls->setReturn_url("$baseUrl/ExecutePayment.php?success=true");
|
||||
$redirectUrls->setCancel_url("$baseUrl/ExecutePayment.php?success=false");
|
||||
|
||||
// ### Payment
|
||||
// A Payment Resource; create one using
|
||||
// the above types and intent as 'sale'
|
||||
$payment = new Payment();
|
||||
$payment->setIntent("sale");
|
||||
$payment->setPayer($payer);
|
||||
$payment->setRedirect_urls($redirectUrls);
|
||||
$payment->setTransactions(array($transaction));
|
||||
|
||||
// ### Api Context
|
||||
// Pass in a `ApiContext` object to authenticate
|
||||
// the call and to send a unique request id
|
||||
// (that ensures idempotency). The SDK generates
|
||||
// a request id if you do not pass one explicitly.
|
||||
$apiContext = new ApiContext($cred, 'Request' . time());
|
||||
|
||||
// ### Create Payment
|
||||
// Create a payment by posting to the APIService
|
||||
// using a valid apiContext
|
||||
// The return object contains the status and the
|
||||
// url to which the buyer must be redirected to
|
||||
// for payment approval
|
||||
try {
|
||||
$payment->create($apiContext);
|
||||
} catch (\PPConnectionException $ex) {
|
||||
echo "Exception: " . $ex->getMessage() . PHP_EOL;
|
||||
var_dump($ex->getData());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// ### Redirect buyer to paypal
|
||||
// Retrieve buyer approval url from the `payment` object.
|
||||
foreach($payment->getLinks() as $link) {
|
||||
if($link->getRel() == 'approval_url') {
|
||||
$redirectUrl = $link->getHref();
|
||||
}
|
||||
}
|
||||
// It is not really a great idea to store the payment id
|
||||
// in the session. In a real world app, please store the
|
||||
// payment id in a database.
|
||||
$_SESSION['paymentId'] = $payment->getId();
|
||||
if(isset($redirectUrl)) {
|
||||
header("Location: $redirectUrl");
|
||||
exit;
|
||||
}
|
||||
96
sample/payments/CreatePaymentUsingSavedCard.php
Normal file
96
sample/payments/CreatePaymentUsingSavedCard.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
// # Create payment using a saved credit card
|
||||
// This sample code demonstrates how you can process a
|
||||
// Payment using a previously saved credit card.
|
||||
// API used: /v1/payments/payment
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
use PayPal\Api\Address;
|
||||
use PayPal\Api\Amount;
|
||||
use PayPal\Api\CreditCard;
|
||||
use PayPal\Api\CreditCardToken;
|
||||
use PayPal\Api\Payer;
|
||||
use PayPal\Api\Payment;
|
||||
use PayPal\Api\FundingInstrument;
|
||||
use PayPal\Api\RedirectUrls;
|
||||
use PayPal\Api\Transaction;
|
||||
use PayPal\Auth\OAuthTokenCredential;
|
||||
use PayPal\Rest\ApiContext;
|
||||
|
||||
// ### Credit card token
|
||||
// Saved credit card id from a previous call to
|
||||
// CreateCreditCard.php
|
||||
$creditCardId = 'CARD-5BT058015C739554AKE2GCEI';
|
||||
$creditCardToken = new CreditCardToken();
|
||||
$creditCardToken->setCredit_card_id($creditCardId);
|
||||
|
||||
// ### FundingInstrument
|
||||
// A resource representing a Payer's funding instrument.
|
||||
// Use a Payer ID (A unique identifier of the payer generated
|
||||
// and provided by the facilitator. This is required when
|
||||
// creating or using a tokenized funding instrument)
|
||||
// and the `CreditCardDetails`
|
||||
$fi = new FundingInstrument();
|
||||
$fi->setCredit_card_token($creditCardToken);
|
||||
|
||||
// ### Payer
|
||||
// A resource representing a Payer that funds a payment
|
||||
// Use the List of `FundingInstrument` and the Payment Method
|
||||
// as 'credit_card'
|
||||
$payer = new Payer();
|
||||
$payer->setPayment_method("credit_card");
|
||||
$payer->setFunding_instruments(array($fi));
|
||||
|
||||
// ### Amount
|
||||
// Let's you specify a payment amount.
|
||||
$amount = new Amount();
|
||||
$amount->setCurrency("USD");
|
||||
$amount->setTotal("1.00");
|
||||
|
||||
// ### Transaction
|
||||
// A transaction defines the contract of a
|
||||
// payment - what is the payment for and who
|
||||
// is fulfilling it. Transaction is created with
|
||||
// a `Payee` and `Amount` types
|
||||
$transaction = new Transaction();
|
||||
$transaction->setAmount($amount);
|
||||
$transaction->setDescription("This is the payment description.");
|
||||
|
||||
// ### Payment
|
||||
// A Payment Resource; create one using
|
||||
// the above types and intent as 'sale'
|
||||
$payment = new Payment();
|
||||
$payment->setIntent("sale");
|
||||
$payment->setPayer($payer);
|
||||
$payment->setTransactions(array($transaction));
|
||||
|
||||
// ### Api Context
|
||||
// Pass in a `ApiContext` object to authenticate
|
||||
// the call and to send a unique request id
|
||||
// (that ensures idempotency). The SDK generates
|
||||
// a request id if you do not pass one explicitly.
|
||||
$apiContext = new ApiContext($cred, 'Request' . time());
|
||||
|
||||
// ###Create Payment
|
||||
// Create a payment by posting to the APIService
|
||||
// using a valid apiContext
|
||||
// The return object contains the status;
|
||||
try {
|
||||
$payment->create($apiContext);
|
||||
} catch (\PPConnectionException $ex) {
|
||||
echo "Exception: " . $ex->getMessage() . PHP_EOL;
|
||||
var_dump($ex->getData());
|
||||
exit(1);
|
||||
}
|
||||
?>
|
||||
<html>
|
||||
<body>
|
||||
<div>
|
||||
Created payment:
|
||||
<?php echo $payment->getId();?>
|
||||
</div>
|
||||
<pre><?php var_dump($payment->toArray());?></pre>
|
||||
<a href='../index.html'>Back</a>
|
||||
</body>
|
||||
</html>
|
||||
47
sample/payments/ExecutePayment.php
Normal file
47
sample/payments/ExecutePayment.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
// #Execute Payment Sample
|
||||
// This sample shows how you can complete
|
||||
// a payment that has been approved by
|
||||
// the buyer by logging into paypal site.
|
||||
// You can optionally update transaction
|
||||
// information by passing in one or more transactions.
|
||||
// API used: POST '/v1/payments/payment/<payment-id>/execute'.
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
use PayPal\Api\ExecutePayment;
|
||||
use PayPal\Api\Payment;
|
||||
use PayPal\Api\PaymentExecution;
|
||||
use PayPal\Rest\ApiContext;
|
||||
session_start();
|
||||
|
||||
if(isset($_GET['success']) && $_GET['success'] == 'true') {
|
||||
// ### Api Context
|
||||
// Pass in a `ApiContext` object to authenticate
|
||||
// the call and to send a unique request id
|
||||
// (that ensures idempotency). The SDK generates
|
||||
// a request id if you do not pass one explicitly.
|
||||
$apiContext = new ApiContext($cred);
|
||||
|
||||
// Get the payment Object by passing paymentId
|
||||
// payment id was previously stored in session in
|
||||
// CreatePaymentUsingPayPal.php
|
||||
$paymentId = $_SESSION['paymentId'];
|
||||
$payment = Payment::get($paymentId);
|
||||
|
||||
// PaymentExecution object includes information necessary
|
||||
// to execute a PayPal account payment.
|
||||
// The payer_id is added to the request query parameters
|
||||
// when the user is redirected from paypal back to your site
|
||||
$execution = new PaymentExecution();
|
||||
$execution->setPayer_id($_GET['PayerID']);
|
||||
|
||||
//Execute the payment
|
||||
$payment->execute($execution, $apiContext);
|
||||
|
||||
echo "<html><body><pre>";
|
||||
var_dump($payment->toArray());
|
||||
echo "</pre><a href='../index.html'>Back</a></body></html>";
|
||||
|
||||
} else {
|
||||
echo "User cancelled payment.";
|
||||
}
|
||||
39
sample/payments/GetPayment.php
Normal file
39
sample/payments/GetPayment.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
// # GetPaymentSample
|
||||
// This sample code demonstrate how you can
|
||||
// retrieve a list of all Payment resources
|
||||
// you've created using the Payments API.
|
||||
// Note various query parameters that you can
|
||||
// use to filter, and paginate through the
|
||||
// payments list.
|
||||
// API used: GET /v1/payments/payments
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
use PayPal\Api\Payment;
|
||||
|
||||
$paymentId = "PAY-0XL713371A312273YKE2GCNI";
|
||||
|
||||
// ### Authentication
|
||||
// Pass in a `OAuthTokenCredential` object
|
||||
// explicilty to authenticate the call.
|
||||
Payment::setCredential($cred);
|
||||
// ### Retrieve payment
|
||||
// Retrieve the payment object by calling the
|
||||
// static `get` method
|
||||
// on the Payment class by passing a valid
|
||||
// Payment ID
|
||||
try {
|
||||
$payment = Payment::get($paymentId);
|
||||
} catch (\PPConnectionException $ex) {
|
||||
echo "Exception:" . $ex->getMessage() . PHP_EOL;
|
||||
var_dump($ex->getData());
|
||||
exit(1);
|
||||
}
|
||||
?>
|
||||
<html>
|
||||
<body>
|
||||
<div>Retrieving Payment ID: <?php echo $paymentId;?></div>
|
||||
<pre><?php var_dump($payment->toArray());?></pre>
|
||||
<a href='../index.html'>Back</a>
|
||||
</body>
|
||||
</html>
|
||||
42
sample/payments/ListPayments.php
Normal file
42
sample/payments/ListPayments.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
// #GetPaymentList
|
||||
// This sample code demonstrate how you can
|
||||
// retrieve a list of all Payment resources
|
||||
// you've created using the Payments API.
|
||||
// Note various query parameters that you can
|
||||
// use to filter, and paginate through the
|
||||
// payments list.
|
||||
// API used: GET /v1/payments/payments
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
use PayPal\Api\Payment;
|
||||
|
||||
// ### Authentication
|
||||
// Pass in a `OAuthTokenCredential` object
|
||||
// explicilty to authenticate the call.
|
||||
// If you skip this step, the client id/secret
|
||||
// set in the config file will be used.
|
||||
Payment::setCredential($cred);
|
||||
|
||||
// ### Retrieve payment
|
||||
// Retrieve the PaymentHistory object by calling the
|
||||
// static `get` method on the Payment class,
|
||||
// and pass a Map object that contains
|
||||
// query parameters for paginations and filtering.
|
||||
// Refer the method doc for valid values for keys
|
||||
try {
|
||||
$payments = Payment::all(array('count' => 10, 'start_index' => 5));
|
||||
} catch (\PPConnectionException $ex) {
|
||||
echo "Exception:" . $ex->getMessage() . PHP_EOL;
|
||||
var_dump($ex->getData());
|
||||
exit(1);
|
||||
}
|
||||
?>
|
||||
<html>
|
||||
<body>
|
||||
<div>Got <?php echo $payments->getCount(); ?> matching payments </div>
|
||||
<pre><?php var_dump($payments->toArray());?></pre>
|
||||
<a href='../index.html'>Back</a>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user