Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hynek Vilimek committed Aug 3, 2015
1 parent 829443c commit 0cd4239
Show file tree
Hide file tree
Showing 23 changed files with 976 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.iml
.idea/
/vendor/
composer.lock


27 changes: 27 additions & 0 deletions .travis.yml
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015, Metis framework
Copyright (c) 2015, Metis framework by GrowJOB
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
61 changes: 59 additions & 2 deletions README.md
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).
48 changes: 48 additions & 0 deletions composer.json
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": "[email protected]"
},
{
"name": "Hynek Vilímek",
"email": "[email protected]"
}
],
"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": "[email protected]",
"issues": "https://github.com/MetisFW/PayPal/issues"
},
"autoload": {
"psr-0": {
"MetisFW\\PayPal\\": "src/"
}
}
}
48 changes: 48 additions & 0 deletions src/MetisFW/PayPal/DI/PayPalExtension.php
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());
};
}

}
52 changes: 52 additions & 0 deletions src/MetisFW/PayPal/PayPalContext.php
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;
}

}
7 changes: 7 additions & 0 deletions src/MetisFW/PayPal/PayPalException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace MetisFW\PayPal;

class PayPalException extends \RuntimeException {

}
122 changes: 122 additions & 0 deletions src/MetisFW/PayPal/Payment/BasePaymentOperation.php
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;
}

}
Loading

0 comments on commit 0cd4239

Please sign in to comment.