Skip to content

Commit

Permalink
Merge pull request #6 from xsolla/shoppingcart3
Browse files Browse the repository at this point in the history
1.1.0 add Shopping Cart Protocol 3.0
  • Loading branch information
craaazy19 committed Jul 28, 2014
2 parents a9fbee5 + 5a923b4 commit 330090a
Show file tree
Hide file tree
Showing 22 changed files with 873 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 1.1.0 (2014-07-24)
* add Shopping Cart Protocol 3.0 http://xsolla.github.io/en/shopingcart3.html

## 1.0.4 (2014-06-04)
* fix incorrect sign code for Shopping Cart Protocol 2.0
* add `$reasonCode` and `$reasonDescription` optional arguments to `PaymentStorageInterface::cancel`
Expand Down
57 changes: 57 additions & 0 deletions example/ipn_shopping_cart_3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Xsolla\SDK\Exception\InvoiceNotFoundException;
use Xsolla\SDK\Project;
use Xsolla\SDK\Protocol\ProtocolFactory;
use Xsolla\SDK\Protocol\Storage\PaymentShoppingCart3StorageInterface;
use Xsolla\SDK\User;
use Xsolla\SDK\Protocol\Storage\UserStorageInterface;
use Xsolla\SDK\Exception\UnprocessableRequestException;

class PaymentShoppingCart3DemoStorage implements PaymentShoppingCart3StorageInterface
{
public function cancel($xsollaPaymentId, $reasonCode = NULL, $reasonDescription = NULL)
{
if (1 == $xsollaPaymentId) {
return;
}
throw new InvoiceNotFoundException();
}

public function pay($xsollaPaymentId, $paymentAmount, $paymentCurrency, User $user, \DateTime $date, $dryRun)
{
if (1 == $xsollaPaymentId) {
return time();//"unique" id
}
throw new UnprocessableRequestException('Demo unprocessable error response');
}
}

class UsersDemoStorage implements UserStorageInterface
{
public function isUserExists(User $user)
{
return 'demo' === $user->getV1();
}

public function getAdditionalUserFields(User $user)
{
return array();
}
}

$userStorage = new UsersDemoStorage;
$paymentStorage = new PaymentShoppingCart3DemoStorage;

$demoProject = new Project(
'4783',//demo project id
'key'//demo project secret key
);
$protocolBuilder = new ProtocolFactory($demoProject);
$protocol = $protocolBuilder->getShoppingCart3Protocol($userStorage, $paymentStorage);

$request = Request::createFromGlobals();
$response = $protocol->run($request);
$response->send();
25 changes: 25 additions & 0 deletions resources/mysql/shopping_cart3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE `xsolla_standard_user` (
`v1` VARCHAR(255) NOT NULL,
`v2` VARCHAR(200) NULL DEFAULT NULL,
`v3` VARCHAR(100) NULL DEFAULT NULL,
PRIMARY KEY (`v1`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

CREATE TABLE `xsolla_shoppingcart3_invoice` (
`id` INT NOT NULL AUTO_INCREMENT,
`v1` VARCHAR(255) NOT NULL,
`id_xsolla` INT UNSIGNED NOT NULL,
`timestamp_ipn` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`timestamp_xsolla_ipn` TIMESTAMP NOT NULL,
`payment_amount` DECIMAL(12,2) UNSIGNED NOT NULL,
`payment_currency` CHAR(3) NOT NULL,
`is_dry_run` TINYINT(1) NOT NULL,
`is_canceled` TINYINT(1) NOT NULL DEFAULT 0,
`timestamp_canceled` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_xsolla_UNIQUE` (`id_xsolla` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
37 changes: 35 additions & 2 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ class Invoice
protected $amount;
protected $currency;
protected $id;
protected $amountShoppingCart3;
protected $currencyShoppingCart3;

public function __construct($virtualCurrencyAmount = null, $amount = null, $currency = null, $id = null)
{
public function __construct(
$virtualCurrencyAmount = null,
$amount = null,
$currency = null,
$id = null,
$amountShoppingCart3 = null,
$currencyShoppingCart3 = null
) {
$this->amount = $amount;
$this->virtualCurrencyAmount = $virtualCurrencyAmount;
$this->currency = $currency;
$this->id = $id;
$this->amountShoppingCart3 = $amountShoppingCart3;
$this->currencyShoppingCart3 = $currencyShoppingCart3;
}

public function getVirtualCurrencyAmount()
Expand Down Expand Up @@ -64,4 +74,27 @@ public function setAmount($sum)

return $this;
}

public function setAmountShoppingCart3($amountShoppingCart3)
{
$this->amountShoppingCart3 = $amountShoppingCart3;
return $this;
}

public function getAmountShoppingCart3()
{
return $this->amountShoppingCart3;
}

public function setCurrencyShoppingCart3($currencyShoppingCart3)
{
$this->currencyShoppingCart3 = $currencyShoppingCart3;
return $this;
}

public function getCurrencyShoppingCart3()
{
return $this->currencyShoppingCart3;
}

}
3 changes: 2 additions & 1 deletion src/PaymentPage/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public function setInvoice(Invoice $invoice, $lockForUser = true, $hideFromUser
$this->setParameter('out', $invoice->getVirtualCurrencyAmount(), $lockForUser, $hideFromUser);
$this->setParameter('currency', $invoice->getCurrency(), $lockForUser, $hideFromUser);
$this->setParameter('sum', $invoice->getAmount(), $lockForUser, $hideFromUser);

$this->setParameter('payment_amount', $invoice->getAmountShoppingCart3(), $lockForUser, $hideFromUser);
$this->setParameter('payment_currency', $invoice->getCurrencyShoppingCart3(), $lockForUser, $hideFromUser);
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Command/Cancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function process(Request $request)

public function checkSign(Request $request)
{
return $this->generateSign($request, array('command', 'id')) == $request->query->get('md5');
return $this->generateSign($request, array('command', 'id')) === $request->query->get('md5');
}

public function getRequiredParams()
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Command/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function process(Request $request)

public function checkSign(Request $request)
{
return $this->generateSign($request, array('command', 'v1')) == $request->query->get('md5');
return $this->generateSign($request, array('command', 'v1')) === $request->query->get('md5');
}

public function getRequiredParams()
Expand Down
19 changes: 19 additions & 0 deletions src/Protocol/Command/CheckShoppingCart3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Xsolla\SDK\Protocol\Command;

use Symfony\Component\HttpFoundation\Request;
use Xsolla\SDK\Protocol\ShoppingCart3;

class CheckShoppingCart3 extends Check
{
public function __construct(ShoppingCart3 $protocol)
{
$this->userStorage = $protocol->getUserStorage();
$this->project = $protocol->getProject();
}

public function checkSign(Request $request)
{
return $this->generateSign($request, array('command', 'v1', 'foreignInvoice')) === $request->query->get('md5');
}
}
2 changes: 1 addition & 1 deletion src/Protocol/Command/PayShoppingCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function checkSign(Request $request)
{
$actualSign = $this->generateSign($request, array('v1', 'amount', 'currency', 'id'));

return $actualSign == $request->query->get('sign');
return $actualSign === $request->query->get('sign');
}

public function getCommentFieldName()
Expand Down
62 changes: 62 additions & 0 deletions src/Protocol/Command/PayShoppingCart3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace Xsolla\SDK\Protocol\Command;

use Symfony\Component\HttpFoundation\Request;
use Xsolla\SDK\Protocol\ShoppingCart3;
use Xsolla\SDK\Protocol\Storage\PaymentShoppingCart3StorageInterface;
use Xsolla\SDK\Protocol\Storage\UserStorageInterface;

class PayShoppingCart3 extends StandardCommand
{
/**
* @var PaymentShoppingCart3StorageInterface
*/
protected $paymentStorage;

/**
* @var UserStorageInterface
*/
protected $userStorage;

public function __construct(ShoppingCart3 $protocol)
{
$this->userStorage = $protocol->getUserStorage();
$this->project = $protocol->getProject();
$this->paymentStorage = $protocol->getPaymentShoppingCart3Storage();
}

public function checkSign(Request $request)
{
return $this->generateSign($request, array('command', 'v1', 'foreignInvoice', 'id')) === $request->query->get('md5');
}

public function process(Request $request)
{
$user = $this->createUser($request);
if (!$this->userStorage->isUserExists($user)) {
return array(
'result' => self::CODE_INVALID_ORDER_DETAILS,
self::COMMENT_FIELD_NAME => 'User not found'
);
}
$datetime = $this->getDateTimeXsolla('Y-m-d H:i:s', $request->query->get('date'));
$id = $this->paymentStorage->pay(
$request->query->get('id'),
$request->query->get('payment_amount'),
$request->query->get('payment_currency'),
$user,
$datetime,
(bool) $request->query->get('dry_run')
);
return array(
'result' => self::CODE_SUCCESS,
self::COMMENT_FIELD_NAME => '',
'id_shop' => $id
);
}

public function getRequiredParams()
{
return array('command', 'md5', 'id', 'v1', 'date', 'payment_amount', 'payment_currency');
}
}
2 changes: 1 addition & 1 deletion src/Protocol/Command/PayStandard.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public function process(Request $request)

public function checkSign(Request $request)
{
return $this->generateSign($request, array('command', 'v1', 'id')) == $request->query->get('md5');
return $this->generateSign($request, array('command', 'v1', 'id')) === $request->query->get('md5');
}
}
36 changes: 36 additions & 0 deletions src/Protocol/CommandFactory/ShoppingCart3Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Xsolla\SDK\Protocol\CommandFactory;

use Xsolla\SDK\Exception\WrongCommandException;
use Xsolla\SDK\Protocol\Command\Cancel;
use Xsolla\SDK\Protocol\Command\CheckShoppingCart3;
use Xsolla\SDK\Protocol\Command\Command;
use Xsolla\SDK\Protocol\Command\PayShoppingCart3;
use Xsolla\SDK\Protocol\ShoppingCart3;

class ShoppingCart3Factory
{
/**
* @param \Xsolla\SDK\Protocol\ShoppingCart3 $protocol
* @param $commandName
* @throws WrongCommandException
* @return Command
*/
public function getCommand(ShoppingCart3 $protocol, $commandName)
{
switch ($commandName) {
case 'check':
return new CheckShoppingCart3($protocol);
case 'pay':
return new PayShoppingCart3($protocol);
case 'cancel':
return new Cancel($protocol, $protocol->getPaymentShoppingCart3Storage());
default:
throw new WrongCommandException(sprintf(
'Wrong command: "%s". Available commands for Standard protocol are: "%s".',
$commandName,
join('", "', $protocol->getProtocolCommands())
));
}
}
}
31 changes: 31 additions & 0 deletions src/Protocol/ProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Xsolla\SDK\Protocol;

use Xsolla\SDK\Protocol\CommandFactory\ShoppingCart3Factory;
use Xsolla\SDK\Protocol\CommandFactory\ShoppingCartFactory;
use Xsolla\SDK\Protocol\CommandFactory\StandardFactory;
use Xsolla\SDK\Protocol\Storage\PaymentShoppingCart3StorageInterface;
use Xsolla\SDK\Protocol\Storage\PaymentShoppingCartStorageInterface;
use Xsolla\SDK\Protocol\Storage\PaymentStandardStorageInterface;
use Xsolla\SDK\Protocol\Storage\UserStorageInterface;
Expand All @@ -20,6 +22,11 @@ public function __construct($project, $ipChecker = null)
$this->ipChecker = $ipChecker;
}

/**
* @param PaymentShoppingCartStorageInterface $paymentStorage
* @return ShoppingCart
* @see http://xsolla.github.io/en/cash.html
*/
public function getShoppingCartProtocol(PaymentShoppingCartStorageInterface $paymentStorage)
{
return new ShoppingCart(
Expand All @@ -31,6 +38,12 @@ public function getShoppingCartProtocol(PaymentShoppingCartStorageInterface $pay
);
}

/**
* @param UserStorageInterface $userStorage
* @param PaymentStandardStorageInterface $paymentStorage
* @return Standard
* @see http://xsolla.github.io/en/currency.html
*/
public function getStandardProtocol(UserStorageInterface $userStorage, PaymentStandardStorageInterface $paymentStorage)
{
return new Standard(
Expand All @@ -42,4 +55,22 @@ public function getStandardProtocol(UserStorageInterface $userStorage, PaymentSt
$this->ipChecker
);
}

/**
* @param UserStorageInterface $userStorage
* @param PaymentShoppingCart3StorageInterface $paymentStorage
* @return ShoppingCart3
* @see http://xsolla.github.io/en/shopingcart3.html
*/
public function getShoppingCart3Protocol(UserStorageInterface $userStorage, PaymentShoppingCart3StorageInterface $paymentStorage)
{
return new ShoppingCart3(
$this->project,
new XmlResponseBuilder($this->enableVersionHeader),
new ShoppingCart3Factory(),
$userStorage,
$paymentStorage,
$this->ipChecker
);
}
}
Loading

0 comments on commit 330090a

Please sign in to comment.