Skip to content

Instantly share code, notes, and snippets.

@mdobydullah
Last active October 26, 2021 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdobydullah/44f52dbb1cf9f954d66a15b93c95640d to your computer and use it in GitHub Desktop.
Save mdobydullah/44f52dbb1cf9f954d66a15b93c95640d to your computer and use it in GitHub Desktop.
LaraPal - PayPalController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Obydul\LaraPal\Services\ExpressCheckout;
class PayPalController extends Controller
{
/**
* Construct.
*/
private $payPalExpressCheckout;
public function __construct()
{
$return_url = "http://example.com/do-the-payment";
$cancel_url = "http://example.com/cancel-payment";
$this->payPalExpressCheckout = new ExpressCheckout($return_url, $cancel_url);
}
/**
* See payment status
*/
public function paymentStatus(Request $request)
{
$status = $request->input('status');
$transaction_id = $request->input('transaction_id');
$message = $request->input('message');
if ($status == 'completed') {
echo 'Success! The Transaction ID: ' . $transaction_id;
} else if ($status == 'cancelled') {
echo 'The transaction has been cancelled.';
} else if ($status == 'refunded') {
echo $message;
} else {
echo 'No status!';
}
}
/**
* Single payment
*/
public function singlePayment()
{
// generate invoice ID (optional). you can remove this from the function.
$last_invoice_id = 22; // take the last invoice from your payment database.
$invoice_id = 'INV_' . str_pad($last_invoice_id + 1, 7, "0", STR_PAD_LEFT);
// Redirect user to PayPal to obtain charging permissions
$results = $this->payPalExpressCheckout->doExpressCheckout(17.87, 'LaraPal Test Checkout', $invoice_id, 'USD');
// After taking the permission, taking payer information. If everything is fine, you will be redirected to $gateway->returnUrl with token and PayerID.
if ($results['ACK'] == 'SUCCESS') {
header('Location: ' . $this->payPalExpressCheckout->gateway->getGate() . 'cmd=_express-checkout&useraction=commit&token=' . $results['TOKEN']);
exit();
} else {
return $results['L_LONGMESSAGE0'];
}
}
/**
* Multiple items payment
*/
public function multipleItemsPayment()
{
// your products/items array should like this
$items = array(
array(
"name" => "Product 1",
"price" => "12.25",
"quantity" => "1",
"product_id" => 111
),
array(
"name" => "Product 2",
"price" => "25.50",
"quantity" => "1",
"product_id" => 112
)
);
// generate invoice ID
$last_invoice_id = 20; // take the last invoice from your payment database.
$invoice_id = 'INV_' . str_pad($last_invoice_id + 1, 7, "0", STR_PAD_LEFT);
// pass custom fields (Optional)
$customFields = array(
'identifier' => "Example.com/ID",
'customerEmail' => "customer@email.com",
);
// Redirect user to PayPal to obtain charging permissions
$results = $this->payPalExpressCheckout->doExpressMultipleCheckout($items, $invoice_id, 'USD', false, $customFields); // if you don't like to pass custom fields, then remove $customFields from here.
// After taking the permission, taking payer information. If everything is fine, you will be redirected to $gateway->returnUrl with token and PayerID.
if ($results['ACK'] == 'SUCCESS') {
header('Location: ' . $this->payPalExpressCheckout->gateway->getGate() . 'cmd=_express-checkout&useraction=commit&token=' . $results['TOKEN']);
exit();
} else {
return $results['L_LONGMESSAGE0'];
}
}
/**
* PayPal do the payment after verification
*/
public function doThePayment(Request $request)
{
$token = $request->input('token');
$PayerID = $request->input('PayerID');
if (empty($token) || empty($PayerID)) {
return "Token or PayerID missing";
} else {
// for single payment
$transaction = $this->payPalExpressCheckout->doSinglePayment($token, $PayerID);
// for multiple payment
// $transaction = $this->payPalExpressCheckout->doMultiplePayment($token, $PayerID);
// get transaction details
$details = $this->payPalExpressCheckout->getTransactionDetails($transaction['PAYMENTINFO_0_TRANSACTIONID']);
return redirect()->route('payment-status', ['status' => 'completed', 'transaction_id' => $details['TRANSACTIONID']]);
}
}
/**
* Refund full or partial amount
*/
public function doRefund(Request $request)
{
// enter your transaction ID
$transactionId = '8CE475579A478253U';
$invoiceId = "INV_0000005";
// full amount
$refund = $this->payPalExpressCheckout->doRefund($transactionId, $invoiceId, false, 0, 'USD', '');
// partial amount
// $refund = $this->payPalExpressCheckout->doRefund($transactionId, $invoiceId, true, 5.25, 'USD', '');
if ($refund['ACK'] == 'SUCCESS') {
return redirect()->route('payment-status', ['status' => 'refunded', 'message' => 'The Refunded Amount: ' . $refund['GROSSREFUNDAMT'] . ' (' . $refund['CURRENCYCODE'] . ')']);
} else {
return redirect()->route('payment-status', ['status' => 'refunded', 'message' => $refund['L_LONGMESSAGE0']]);
}
}
/**
* Cancel transaction
*/
public function cancelPayment()
{
return redirect()->route('payment-status', ['status' => 'cancelled']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment