-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hynek Vilimek
committed
Aug 3, 2015
1 parent
829443c
commit 0cd4239
Showing
23 changed files
with
976 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.iml | ||
.idea/ | ||
/vendor/ | ||
composer.lock | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- hhvm | ||
|
||
matrix: | ||
allow_failures: | ||
- php: hhvm | ||
|
||
before_script: | ||
# Update composer | ||
- composer self-update | ||
- composer install --dev | ||
# Install Nette Tester | ||
- travis_retry composer update --no-interaction --prefer-dist $dependencies | ||
|
||
script: ./vendor/bin/tester -p php -c ./tests/php.ini-unix ./tests/ | ||
|
||
after_failure: | ||
# Print *.actual content | ||
- 'for i in $(find ./tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done' | ||
|
||
sudo: false |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,59 @@ | ||
# PayPal | ||
PayPal integration for Nette | ||
MetisFW/PayPal | ||
====== | ||
|
||
[![Build Status](https://travis-ci.org/MetisFW/PayPal.svg?branch=master)](https://travis-ci.org/MetisFW/PayPal) | ||
[![Downloads this Month](https://img.shields.io/packagist/dm/metisfw/paypal.svg)](https://packagist.org/packages/metisfw/paypal) | ||
[![Latest stable](https://img.shields.io/packagist/v/metisfw/paypal.svg)](https://packagist.org/packages/metisfw/paypal) | ||
|
||
About | ||
------------ | ||
PayPal payment integration to Nette framework. | ||
Internally use [paypal/PayPal-PHP-SDK](https://github.com/paypal/PayPal-PHP-SDK) for api requests. | ||
|
||
Inspired by [Kdyby/PayPalExpress](https://github.com/Kdyby/PayPalExpress) | ||
|
||
Requirements | ||
------------ | ||
MetisFW/PayPal requires PHP 5.3.2 or higher with curl, json and openssl (for lower PHP version) extensions. | ||
|
||
- [Nette Framework](https://github.com/nette/nette) | ||
|
||
|
||
Installation | ||
------------ | ||
1) The best way to install MetisFW/PayPal is using [Composer](http://getcomposer.org/): | ||
|
||
```sh | ||
$ composer require metisfw/paypal | ||
``` | ||
|
||
2) Register extension | ||
``` | ||
extensions: | ||
payPal: MetisFW\PayPal\DI\PayPalExtension | ||
``` | ||
|
||
3) Set up extension parameters | ||
|
||
```neon | ||
payPal: | ||
clientId: AUqne4ywvozUaSQ1THTZYKFr88bhtA0SS_fXBoJTfeSTIasDBWuXLiLcFlfmSXRfL-kZ3Z5shvNrT6rP | ||
secret: EDGPDc3a65JBBY7-IKkNak7aGTVTvY-NhJgfhptegSML58fWjfp89U7UKNgGk9UI-UEZ-btfaE2sGST1 | ||
currency: EUR | ||
sdkConfig: | ||
mode: sandbox | ||
log.Enabled: true | ||
log.FileName: '%tempDir%/PayPal.log' | ||
log.LogLevel: DEBUG | ||
validation.level: log | ||
cache.enabled: true | ||
# 'http.CURLOPT_CONNECTTIMEOUT' => 30 | ||
# 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'/ | ||
``` | ||
|
||
sdkConfig is config to [paypal/PayPal-PHP-SDK](https://github.com/paypal/PayPal-PHP-SDK) | ||
see [sdk-config-sample](https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/sdk_config.ini) | ||
|
||
----- | ||
|
||
Homepage [MetisFW](https://github.com/MetisFW) and repository [MetisFW/PayPal](https://github.com/MetisFW/PayPal). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "metisfw/paypal", | ||
"type": "library", | ||
"description": "PayPal SDK integration for Nette Framework", | ||
"keywords": [ | ||
"nette", | ||
"paypal", | ||
"pay", | ||
"checkout", | ||
"payment" | ||
], | ||
"homepage": "https://github.com/MetisFW", | ||
"license": [ | ||
"BSD-3-Clause", | ||
"GPL-2.0", | ||
"GPL-3.0" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "GrowJOB s.r.o", | ||
"email": "dev@growjob.com" | ||
}, | ||
{ | ||
"name": "Hynek Vilímek", | ||
"email": "h.vilimek@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.0", | ||
"ext-curl": "*", | ||
"ext-json": "*", | ||
"nette/nette": "~2.3", | ||
"paypal/rest-api-sdk-php": "1.5.*" | ||
}, | ||
"require-dev": { | ||
"nette/tester": "@dev", | ||
"mockery/mockery": "^0.9.4" | ||
}, | ||
"support": { | ||
"email": "dev@growjob.com", | ||
"issues": "https://github.com/MetisFW/PayPal/issues" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"MetisFW\\PayPal\\": "src/" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace MetisFW\PayPal\DI; | ||
|
||
use Nette\Configurator; | ||
use Nette\DI\Compiler; | ||
use Nette\DI\CompilerExtension; | ||
use Nette\Utils\Validators; | ||
|
||
class PayPalExtension extends CompilerExtension { | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $defaults = array( | ||
'currency' => 'CZK', | ||
); | ||
|
||
public function loadConfiguration() { | ||
$builder = $this->getContainerBuilder(); | ||
$config = $this->getConfig($this->defaults); | ||
|
||
Validators::assertField($config, 'clientId'); | ||
Validators::assertField($config, 'secret'); | ||
Validators::assertField($config, 'sdkConfig', 'array'); | ||
|
||
$builder->addDefinition($this->prefix('credentials')) | ||
->setClass('PayPal\Auth\OAuthTokenCredential', array($config['clientId'], $config['secret'])); | ||
|
||
$builder->addDefinition($this->prefix('apiContext')) | ||
->setClass('PayPal\Rest\ApiContext', array($this->prefix('@credentials'))); | ||
|
||
$builder->addDefinition($this->prefix('PayPal')) | ||
->setClass('MetisFW\PayPal\PayPalContext', array($this->prefix('@apiContext'))) | ||
->addSetup('setConfig', array($config['sdkConfig'])) | ||
->addSetup('setCurrency', array($config['currency'])); | ||
} | ||
|
||
/** | ||
* @param Configurator $configurator | ||
*/ | ||
public static function register(Configurator $configurator) { | ||
$configurator->onCompile[] = function ($config, Compiler $compiler) { | ||
$compiler->addExtension('payPal', new PayPalExtension()); | ||
}; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace MetisFW\PayPal; | ||
|
||
use Nette\Object; | ||
use PayPal\Rest\ApiContext; | ||
|
||
class PayPalContext extends Object { | ||
|
||
/** @var ApiContext */ | ||
private $apiContext; | ||
|
||
/** @var string */ | ||
private $currency; | ||
|
||
/** | ||
* @param string $clientId | ||
* @param string $secret | ||
*/ | ||
public function __construct(ApiContext $apiContext) { | ||
$this->apiContext = $apiContext; | ||
} | ||
|
||
/** | ||
* @param array $config | ||
*/ | ||
public function setConfig(array $config) { | ||
$this->apiContext->setConfig($config); | ||
} | ||
|
||
/** | ||
* @param string $currency | ||
*/ | ||
public function setCurrency($currency) { | ||
$this->currency = $currency; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getCurrency() { | ||
return $this->currency; | ||
} | ||
|
||
/** | ||
* @return ApiContext | ||
*/ | ||
public function getApiContext() { | ||
return $this->apiContext; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace MetisFW\PayPal; | ||
|
||
class PayPalException extends \RuntimeException { | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace MetisFW\PayPal\Payment; | ||
|
||
use MetisFW\PayPal\PayPalContext; | ||
use MetisFW\PayPal\PayPalException; | ||
use Nette\Object; | ||
use PayPal\Api\Payer; | ||
use PayPal\Api\Payment; | ||
use PayPal\Api\PaymentExecution; | ||
use PayPal\Exception\PayPalConfigurationException; | ||
use PayPal\Exception\PayPalConnectionException; | ||
use PayPal\Exception\PayPalInvalidCredentialException; | ||
use PayPal\Exception\PayPalMissingCredentialException; | ||
|
||
abstract class BasePaymentOperation extends Object implements PaymentOperation { | ||
|
||
/** @var PayPalContext */ | ||
protected $context; | ||
|
||
/** | ||
* @var array array of callbacks, signature: function ($this) {...} | ||
*/ | ||
public $onCancel; | ||
|
||
/** | ||
* @var array array of callbacks, signature: function ($this, Payment $payment) {...} | ||
*/ | ||
public $onReturn; | ||
|
||
/** | ||
* @param PayPalContext $context | ||
*/ | ||
public function __construct(PayPalContext $context) { | ||
$this->context = $context; | ||
} | ||
|
||
/** | ||
* @return array array of PayPal\Api\Transaction | ||
*/ | ||
abstract protected function getTransactions(); | ||
|
||
/** | ||
* @see http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html | ||
* | ||
* @return Payment | ||
*/ | ||
public function getPayment() { | ||
$payer = new Payer(); | ||
$payer->setPaymentMethod('paypal'); | ||
|
||
$payment = new Payment(); | ||
$payment->setIntent("sale") | ||
->setPayer($payer); | ||
|
||
$transactions = $this->getTransactions(); | ||
$payment->setTransactions($transactions); | ||
|
||
return $payment; | ||
} | ||
|
||
/** | ||
* Execute payment api call | ||
* | ||
* @return Payment | ||
*/ | ||
public function createPayment(Payment $payment) { | ||
try { | ||
return $payment->create($this->context->getApiContext()); | ||
} | ||
catch(\Exception $exception) { | ||
throw $this->translateException($exception); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $paymentId | ||
* @param string $payerId | ||
* | ||
* @return void | ||
*/ | ||
public function handleReturn($paymentId, $payerId) { | ||
try { | ||
$payment = Payment::get($paymentId, $this->context->getApiContext()); | ||
$execution = new PaymentExecution(); | ||
$execution->setPayerId($payerId); | ||
|
||
$payment->execute($execution, $this->context->getApiContext()); | ||
$paidPayment = Payment::get($paymentId, $this->context->getApiContext()); | ||
} | ||
catch(\Exception $exception) { | ||
throw $this->translateException($exception); | ||
} | ||
|
||
$this->onReturn($this, $paidPayment); | ||
return $paidPayment; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function handleCancel() { | ||
$this->onCancel($this); | ||
} | ||
|
||
/** | ||
* @param \Exception $exception | ||
* @return \Exception | ||
*/ | ||
protected function translateException(\Exception $exception) { | ||
if($exception instanceof PayPalConfigurationException || | ||
$exception instanceof PayPalInvalidCredentialException || | ||
$exception instanceof PayPalMissingCredentialException || | ||
$exception instanceof PayPalConnectionException | ||
) { | ||
return new PayPalException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
|
||
return $exception; | ||
} | ||
|
||
} |
Oops, something went wrong.