Last active
July 28, 2020 12:59
-
-
Save RobertoNovelo/12a4b4062aa89f83de5b to your computer and use it in GitHub Desktop.
CodeIgniter 3.x Paypal Rest API Authorize Capture Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
require_once(BASEPATH . '../application/libraries/PayPal-PHP-SDK/autoload.php'); | |
use PayPal\Rest\ApiContext; | |
use PayPal\Auth\OAuthTokenCredential; | |
use PayPal\Api\Capture; | |
use PayPal\Api\Authorization; | |
use PayPal\Api\Amount; | |
use PayPal\Exception\PayPalConnectionException; | |
class Paypal_wrapper { | |
private $debug = false; | |
private $sandbox = true; | |
private $productionClientID = ""; | |
private $productionClientSecret = ""; | |
private $sandboxClientID = ""; | |
private $sandboxClientSecret = ""; | |
protected $apiContext = null; | |
public function __construct() | |
{ | |
if($this->sandbox) | |
{ | |
$clientID = $this->sandboxClientID; | |
$clientSecret = $this->sandboxClientSecret; | |
} | |
else | |
{ | |
$clientID = $this->productionClientID; | |
$clientSecret = $this->productionClientSecret; | |
} | |
$this->apiContext = new ApiContext( | |
new OAuthTokenCredential( | |
$clientID, | |
$clientSecret | |
) | |
); | |
} | |
public function capture_payment($paymentAmount,$authID,$final=true) | |
{ | |
$amount = new Amount(); | |
$amount->setCurrency("USD"); | |
$amount->setTotal(number_format($paymentAmount, 2, '.', '')); | |
$captur = new Capture(); | |
$captur->setId($authID); | |
$captur->setAmount($amount); | |
$captur->setIsFinalCapture($final); | |
$response = new StdClass(); | |
//Catches invalid authIDs and captured authIDs | |
try | |
{ | |
$authorization = Authorization::get($authID, $this->apiContext); | |
$capt = $authorization->capture($captur, $this->apiContext); | |
$response->captureID = $capt->getId(); | |
$response->responseStatus = true; | |
return $response; | |
} | |
catch (PayPalConnectionException $ex) | |
{ | |
if($this->debug) | |
{ | |
$response->exception = $ex; | |
} | |
$response->responseStatus = false; | |
return $response; | |
} | |
} | |
} | |
// You can use this wrapper by calling: | |
//$this->load->library('paypal_wrapper'); | |
//$capture = $this->paypal_wrapper->capture_payment($amount,$authID); | |
//if($capture->responseStatus)... | |
//in any of your controllers/models by placing both PayPal_Wrapper.php and the PayPal-PHP-SDK library folder under the application/libraries CI folder. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment