Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions lib/ClassicAPI.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace kun391\paypal;

use PayPal\CoreComponentTypes\BasicAmountType;
Expand All @@ -18,6 +19,7 @@ class ClassicAPI extends Component

/**
* @param $config
*
* @return mixed
*/
public function init()
Expand All @@ -26,17 +28,17 @@ public function init()

//set config default for paypal
if (!$this->pathFileConfig) {
$this->pathFileConfig = __DIR__ . '/config-classic.php';
$this->pathFileConfig = __DIR__.'/config-classic.php';
}
// check file config already exist.
if (!file_exists($this->pathFileConfig)) {
throw new \Exception("File config does not exist.", 500);
throw new \Exception('File config does not exist.', 500);
}

$this->_credentials = require $this->pathFileConfig;

if (!in_array($this->_credentials['mode'], ['sandbox', 'live'])) {
throw new \Exception("Error Processing Request", 503);
throw new \Exception('Error Processing Request', 503);
}

return $this->_credentials;
Expand All @@ -49,6 +51,7 @@ public function setConfig()

/**
* @param $params
*
* @return mixed
*/
public function getAccountInfo(array $params = [])
Expand All @@ -58,23 +61,23 @@ public function getAccountInfo(array $params = [])
}

if (!isset($params['email'])) {
throw new \Exception("Email cannot be blank.", 400);
throw new \Exception('Email cannot be blank.', 400);
}

$getVerifiedStatus = new GetVerifiedStatusRequest();
$accountIdentifier = new AccountIdentifierType();

$accountIdentifier->emailAddress = $params['email'];
$accountIdentifier->emailAddress = $params['email'];
$getVerifiedStatus->accountIdentifier = $accountIdentifier;
$getVerifiedStatus->matchCriteria = 'NONE';

if (isset($params['matchCriteria']) && strtolower($params['matchCriteria']) == 'name') {
$getVerifiedStatus->matchCriteria = 'NAME';
if (!isset($params['firstName']) || !isset($params['lastName'])) {
throw new \Exception("Firstname or lastname cannot be blank.", 400);
throw new \Exception('Firstname or lastname cannot be blank.', 400);
}
$getVerifiedStatus->firstName = $params['firstName'];
$getVerifiedStatus->lastName = $params['lastName'];
$getVerifiedStatus->lastName = $params['lastName'];
}

$getVerifiedStatus->requestEnvelope = [
Expand All @@ -99,17 +102,17 @@ public function sendMoney(array $params = [])
}

if (!isset($params['balance'])) {
throw new \Exception("Balance cannot be blank.", 400);
throw new \Exception('Balance cannot be blank.', 400);
}

$massPayReq = new MassPayReq();
$massPayItemArray = array();
$massPayReq = new MassPayReq();
$massPayItemArray = [];

$amount = new BasicAmountType("USD", $params['balance']);
$massPayRequestItem = new MassPayRequestItemType($amount);
$amount = new BasicAmountType('USD', $params['balance']);
$massPayRequestItem = new MassPayRequestItemType($amount);
$massPayRequestItem->ReceiverEmail = $params['email'];

$massPayRequest = new MassPayRequestType($massPayRequestItem);
$massPayRequest = new MassPayRequestType($massPayRequestItem);
$massPayReq->MassPayRequest = $massPayRequest;

$service = new PayPalAPIInterfaceServiceService($this->_credentials);
Expand Down
67 changes: 35 additions & 32 deletions lib/RestAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

namespace kun391\paypal;

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Invoice;
use PayPal\Api\MerchantInfo;
use PayPal\Api\Address;
use PayPal\Api\Amount;
use PayPal\Api\BillingInfo;
use PayPal\Api\Currency;
use PayPal\Api\Invoice;
use PayPal\Api\InvoiceItem;
use PayPal\Api\Phone;
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\MerchantInfo;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
use PayPal\Api\Address;
use PayPal\Api\Currency;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use yii\base\Component;

class RestAPI extends Component
Expand All @@ -30,8 +29,10 @@ class RestAPI extends Component
public $cancelUrl = '';

public $pathFileConfig;

/**
* @param $config
*
* @return mixed
*/
public function __construct($config = [])
Expand All @@ -40,7 +41,7 @@ public function __construct($config = [])

//set config default for paypal
if (!$this->pathFileConfig) {
$this->pathFileConfig = __DIR__ . '/config-rest.php';
$this->pathFileConfig = __DIR__.'/config-rest.php';
}

// check file config already exist.
Expand All @@ -49,7 +50,7 @@ public function __construct($config = [])
}

//set config file
$this->_credentials = require($this->pathFileConfig);
$this->_credentials = require $this->pathFileConfig;

if (!in_array($this->_credentials['config']['mode'], ['sandbox', 'live'])) {
throw new \Exception('Error Processing Request', 503);
Expand All @@ -59,7 +60,7 @@ public function __construct($config = [])
}

/**
* Get api context
* Get api context.
*
* @return mixed
*/
Expand Down Expand Up @@ -92,18 +93,20 @@ private function setConfig()
private function getBaseUrl()
{
if (PHP_SAPI == 'cli') {
$trace=debug_backtrace();
$trace = debug_backtrace();
$relativePath = substr(dirname($trace[0]['file']), strlen(dirname(dirname(__FILE__))));
echo 'Warning: This sample may require a server to handle return URL. Cannot execute in command line. Defaulting URL to http://localhost$relativePath \n';
return 'http://localhost' . $relativePath;

return 'http://localhost'.$relativePath;
}
$protocol = 'http';
if ($_SERVER['SERVER_PORT'] == 443 || (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on')) {
$protocol .= 's';
}
$host = $_SERVER['HTTP_HOST'];
$request = $_SERVER['PHP_SELF'];
return dirname($protocol . '://' . $host . $request);

return dirname($protocol.'://'.$host.$request);
}

public function createInvoice($params = null)
Expand All @@ -118,7 +121,7 @@ public function createInvoice($params = null)
// required for invoice APIs
$invoice
->setMerchantInfo(new MerchantInfo())
->setBillingInfo(array(new BillingInfo()));
->setBillingInfo([new BillingInfo()]);

// ### Merchant Info
// A resource representing merchant information that can be
Expand All @@ -134,15 +137,15 @@ public function createInvoice($params = null)

$items = [];
foreach ($params['items'] as $key => $item) {
# code...
// code...
$items[$key] = new InvoiceItem();
$items[$key]
->setName($item['name'])
->setQuantity($item['quantity'])
->setUnitPrice(new Currency());
$items[$key]->getUnitPrice()
->setCurrency($params['currency'])
->setValue((double) $item['price']);
->setValue((float) $item['price']);
}
// ### Items List
$invoice->setItems($items);
Expand All @@ -155,18 +158,18 @@ public function createInvoice($params = null)
$errors = [
'code' => $ex->getCode(),
'data' => $ex->getData(),
'message' => $ex->getMessage()
'message' => $ex->getMessage(),
];
} catch (\Exception $ex) {
$errors = [
'code' => $ex->getCode(),
'message' => $ex->getMessage()
'message' => $ex->getMessage(),
];
}

return [
'errors' => $errors,
'invoices' => $invoice
'invoices' => $invoice,
];
}

Expand Down Expand Up @@ -218,14 +221,14 @@ public function getLinkCheckOut($params = null)
$baseUrl = $this->getBaseUrl();

try {
$redirectUrls->setReturnUrl($baseUrl . $this->successUrl)
->setCancelUrl($baseUrl . $this->cancelUrl);
$redirectUrls->setReturnUrl($baseUrl.$this->successUrl)
->setCancelUrl($baseUrl.$this->cancelUrl);
} catch (\InvalidArgumentException $ex) {
return [
'errors' => [
'code' => $ex->getCode(),
'message' => $ex->getMessage()
]
'message' => $ex->getMessage(),
],
];
}
// ### Payment
Expand All @@ -248,12 +251,12 @@ public function getLinkCheckOut($params = null)
$errors = [
'code' => $ex->getCode(),
'data' => $ex->getData(),
'message' => $ex->getMessage()
'message' => $ex->getMessage(),
];
} catch (\Exception $ex) {
$errors = [
'code' => $ex->getCode(),
'message' => $ex->getMessage()
'message' => $ex->getMessage(),
];
}

Expand All @@ -266,16 +269,16 @@ public function getLinkCheckOut($params = null)
];
}

public function getResult($paymentId) {

public function getResult($paymentId)
{
$payment = Payment::get($paymentId, $this->config);

$execution = new PaymentExecution();
$execution->setPayerId($_GET['PayerID']);
$payment = $payment->execute($execution, $this->config);

$result = @$payment->toArray();

return $result;
}

}
4 changes: 2 additions & 2 deletions lib/config-classic.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* Information PAYPAL's enviroments
* Information PAYPAL's enviroments.
*
* @var string
*/

Expand All @@ -20,4 +21,3 @@
'acct1.Signature' => 'APP9kKh6roKmPNKj6yBK5oSwdD39ADujX4sfPXjr.hGf1wjRi1THwoVq',
'mode' => 'sandbox',
];

8 changes: 3 additions & 5 deletions lib/config-rest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* Information PAYPAL's enviroments
* Information PAYPAL's enviroments.
*
* @var string
*/

Expand All @@ -14,8 +15,6 @@
// 'business_owner' => 'nguyentruongthanh.dn-facilitator-1@gmail.com',
// ];



$setting = [
'endpoint' => 'api.sandbox.paypal.com',
'client_id' => 'AX9sEz0g3cCzD_heoGyedx7LKSuEx1Lx7H8aGXIrzQmDhqV-V5bV0sbVFc195mNKbE81OkAPZZi_7dfa',
Expand All @@ -29,6 +28,5 @@
'log.LogEnabled' => false,
'log.FileName' => '@api/runtime/PayPal.log',
'log.LogLevel' => 'FINE',
]
],
], $setting);

1 change: 0 additions & 1 deletion tests/ClassicAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function testConstructThrowWhenNoConfigFile()

public function testConstructWithConfig()
{

}

public function testGetAccountInfoEmptyParams()
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<?php

include '/vendor/autoload.php';