-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from xsolla/shoppingcart3
1.1.0 add Shopping Cart Protocol 3.0
- Loading branch information
Showing
22 changed files
with
873 additions
and
11 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
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,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(); |
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,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; |
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
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
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,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'); | ||
} | ||
} |
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 |
---|---|---|
@@ -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'); | ||
} | ||
} |
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 |
---|---|---|
@@ -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()) | ||
)); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.