Commit c2099aa6 authored by Mark Harding's avatar Mark Harding

(feat): working stripe with wire

parent d4433729
No related merge requests found
Pipeline #72528990 failed with stages
in 4 minutes
<?php
namespace Minds\Controllers\Cli;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Cli;
use Minds\Interfaces;
use Minds\Exceptions;
use Minds\Entities;
use Minds\Core\Data\ElasticSearch\Prepared;
use Minds\Core\Analytics\Iterators\SignupsOffsetIterator;
use Minds\Core\Boost\Network\Manager;
use Minds\Core\Util\BigNumber;
use Minds\Helpers\Counters;
class Stripe extends Cli\Controller implements Interfaces\CliControllerInterface
{
private $db;
private $es;
private $elasticRepository;
private $pendingBulkInserts = [];
public function __construct()
{
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
public function help($command = null)
{
$this->out('TBD');
}
public function exec()
{
echo "1";
}
public function get_payment_intent()
{
$intent = new Core\Payments\Stripe\Intents\PaymentIntent();
$intent->setAmount(2000);
$intentManager = new Core\Payments\Stripe\Intents\Manager();
$intent = $intentManager->add($intent);
var_dump($intent);
}
public function get_setup_intent()
{
$intent = new Core\Payments\Stripe\Intents\SetupIntent();
$intentManager = new Core\Payments\Stripe\Intents\Manager();
$intent = $intentManager->add($intent);
var_dump($intent->getClientSecret());
}
public function get_setup_intent_payment_method()
{
$id = $this->getOpt('id');
$intentManager = new Core\Payments\Stripe\Intents\Manager();
$intent = $intentManager->get($id);
var_dump($intent);
}
}
<?php
/**
*
*/
namespace Minds\Controllers\api\v2\payments\stripe;
use Minds\Api\Factory;
use Minds\Common\Cookie;
use Minds\Core\Di\Di;
use Minds\Core\Config;
use Minds\Core\Session;
use Minds\Interfaces;
use Minds\Core\Payments\Stripe;
class intents implements Interfaces\Api
{
public function get($pages)
{
return Factory::response([
]);
}
public function post($pages)
{
$user = Session::getLoggedInUser();
switch ($pages[0]) {
case 'apply':
$intent = new Stripe\Intents\SetupIntent();
$intent->setId($_POST['intent_id']);
$intentManager = new Stripe\Intents\Manager();
$intent = $intentManager->get($_POST['intent_id']);
$customersManager = new Stripe\Customers\Manager();
$customer = $customersManager->getFromUserGuid($user->getGuid());
if (!$customer) {
$customer = new Stripe\Customers\Customer;
$customer->setUser($user)
->setUserGuid($user->getGuid());
}
$customer->setPaymentMethod($intent->getPaymentMethod());
$customersManager->updatePaymentMethod($customer);
var_dump($customer); exit;
break;
}
return Factory::response([]);
}
public function put($pages)
{
$user = Session::getLoggedInUser();
$intent = new Stripe\Intents\SetupIntent();
$intentManager = new Stripe\Intents\Manager();
$intent = $intentManager->add($intent);
return Factory::response([
'intent' => $intent->export(),
]);
}
public function delete($pages)
{
$user = Session::getLoggedInUser();
$user->setCanary(false);
$user->save();
return Factory::response([]);
}
}
<?php
/**
*
*/
namespace Minds\Controllers\api\v2\payments\stripe;
use Minds\Api\Factory;
use Minds\Common\Cookie;
use Minds\Core\Di\Di;
use Minds\Core\Config;
use Minds\Core\Session;
use Minds\Interfaces;
use Minds\Core\Payments\Stripe;
class paymentmethods implements Interfaces\Api
{
public function get($pages)
{
$user = Session::getLoggedInUser();
$paymentMethodsManager = new Stripe\PaymentMethods\Manager();
$paymentMethods = $paymentMethodsManager->getList([
'limit' => 12,
'user_guid' => $user->getGuid(),
]);
return Factory::response([
'paymentmethods' => Factory::exportable($paymentMethods),
]);
}
public function post($pages)
{
$user = Session::getLoggedInUser();
switch ($pages[0]) {
case 'apply':
$intent = new Stripe\Intents\SetupIntent();
$intent->setId($_POST['intent_id']);
$intentManager = new Stripe\Intents\Manager();
$paymentMethodsManager = new Stripe\PaymentMethods\Manager();
$customersManager = new Stripe\Customers\Manager();
// Get the intent
$intent = $intentManager->get($_POST['intent_id']);
// Grab our customerId
$customer = $customersManager->getFromUserGuid($user->getGuid());
// Build a payment method
$paymentMethod = new Stripe\PaymentMethods\PaymentMethod();
$paymentMethod->setId($intent->getPaymentMethod())
->setUserGuid($user->getGuid());
if ($customer) {
$paymentMethod->setCustomerId($customer->getId());
}
// Save the payment method
$paymentMethodsManager->add($paymentMethod);
break;
}
return Factory::response([]);
}
public function put($pages)
{
return Factory::response([]);
}
public function delete($pages)
{
$paymentMethodsManager = new Stripe\PaymentMethods\Manager();
$paymentMethodsManager->delete($pages[0]);
return Factory::response([]);
}
}
<?php
/**
* Minds Wire Api endpoint.
*
* @version 2
*
* @author Mark Harding
*/
namespace Minds\Controllers\api\v2;
use Minds\Api\Factory;
use Minds\Core;
use Minds\Core\Util\BigNumber;
use Minds\Core\Wire\Exceptions\WalletNotSetupException;
use Minds\Entities;
use Minds\Interfaces;
class wire implements Interfaces\Api
{
public function get($pages)
{
$response = [];
return Factory::response($response);
}
/**
* Send a wire to someone.
*
* @param array $pages
*
* API:: /v1/wire/:guid
*/
public function post($pages)
{
Factory::isLoggedIn();
$response = [];
if (!isset($pages[0])) {
return Factory::response(['status' => 'error', 'message' => ':guid must be passed in uri']);
}
$entity = Entities\Factory::build($pages[0]);
if (!$entity) {
return Factory::response(['status' => 'error', 'message' => 'Entity not found']);
}
$user = $entity->type == 'user' ? $entity : $entity->getOwnerEntity();
if (Core\Session::getLoggedInUserGuid() === $user->guid) {
return Factory::response(['status' => 'error', 'message' => 'You cannot send a wire to yourself!']);
}
if (Core\Security\ACL\Block::_()->isBlocked(Core\Session::getLoggedInUserGuid(), $user->guid)) {
return Factory::response(['status' => 'error', 'message' => 'You cannot send a wire to a user who has blocked you.']);
}
$amount = BigNumber::_($_POST['amount']);
$recurring = isset($_POST['recurring']) ? $_POST['recurring'] : false;
if (!$amount) {
return Factory::response(['status' => 'error', 'message' => 'you must send an amount']);
}
if ($amount->lt(0)) {
return Factory::response(['status' => 'error', 'message' => 'amount must be a positive number']);
}
$manager = Core\Di\Di::_()->get('Wire\Manager');
$digits = 18;
if ($_POST['method'] === 'usd') {
$digits = 2;
}
try {
$manager
->setAmount((string) BigNumber::toPlain($amount, $digits))
->setRecurring($recurring)
->setSender(Core\Session::getLoggedInUser())
->setEntity($entity)
->setPayload((array) $_POST['payload']);
$result = $manager->create();
if (!$result) {
throw new \Exception('Something failed');
}
} catch (WalletNotSetupException $e) {
$wireQueue = (Queue\Client::Build())
->setQueue('WireNotification')
->send([
'entity' => serialize($entity),
'walletNotSetupException' => true,
]);
$response['status'] = 'error';
$response['message'] = $e->getMessage();
} catch (\Exception $e) {
$response['status'] = 'error';
$response['message'] = $e->getMessage();
}
return Factory::response($response);
}
public function put($pages)
{
}
public function delete($pages)
{
}
}
......@@ -57,5 +57,9 @@ class PaymentsProvider extends Provider
$this->di->bind('Stripe\Connect\Manager', function ($di) {
return new Stripe\Connect\Manager();
}, ['useFactory'=>true]);
$this->di->bind('Stripe\Intents\Manager', function ($di) {
return new Stripe\Intents\Manager();
}, ['useFactory'=>true]);
}
}
......@@ -3,6 +3,8 @@
namespace Minds\Core\Payments\Stripe\Customers;
use Minds\Core\Di\Di;
use Minds\Core\Payments\Stripe\Instances\CustomerInstance;
use Minds\Core\Payments\Stripe\Instances\PaymentMethodInstance;
class Manager
{
......@@ -10,9 +12,15 @@ class Manager
/** @var Lookup $lookup */
private $lookup;
public function __construct($lookup = null)
private $customerInstance;
private $paymentMethodInstance;
public function __construct($lookup = null, $customerInstance = null, $paymentMethodInstance = null)
{
$this->lookup = $lookup ?: Di::_()->get('Database\Cassandra\Lookup');
$this->customerInstance = $customerInstance ?? new CustomerInstance();
$this->paymentMethodInstance = $paymentMethodInstance ?? new PaymentMethodInstance();
}
/**
......@@ -47,18 +55,20 @@ class Manager
* @param Customer $customer
* @return boolean
*/
public function add(Customer $customer)
public function add(Customer $customer) : Customer
{
$customer = \Stripe\Customer::create([
'source' => $customer->getPaymentSource(),
'email' => $customer->getUser()->getEmail(),
]);
$stripeCustomer = $this->customerInstance->create([
'payment_method' => $customer->getPaymentMethod(),
]);
$this->lu->set("{$customer->getUserGuid()}:payments", [
'customer_id' => (string) $customer->id
'customer_id' => (string) $stripeCustomer->id
]);
return (bool) $customer->id;
$customer->setId($stripeCustomer->id);
return $customer;
}
}
\ No newline at end of file
}
<?php
namespace Minds\Core\Payments\Stripe\Instances;
use Minds\Common\StaticToInstance;
use Minds\Core\Config\Config;
use Minds\Core\Di\Di;
/**
* @method AccountInstance create()
* @method AccountInstance retrieve()
*/
class CustomerInstance extends StaticToInstance
{
public function __construct(Config $config = null)
{
$config = $config ?? Di::_()->get('Config');
\Stripe\Stripe::setApiKey($config->get('payments')['stripe']['api_key']);
$this->setClass(new \Stripe\Customer);
}
}
<?php
namespace Minds\Core\Payments\Stripe\Instances;
use Minds\Common\StaticToInstance;
use Minds\Core\Config\Config;
use Minds\Core\Di\Di;
/**
* @method AccountInstance create()
* @method AccountInstance retrieve()
*/
class PaymentIntentInstance extends StaticToInstance
{
public function __construct(Config $config = null)
{
$config = $config ?? Di::_()->get('Config');
\Stripe\Stripe::setApiKey($config->get('payments')['stripe']['api_key']);
$this->setClass(new \Stripe\PaymentIntent);
}
}
<?php
namespace Minds\Core\Payments\Stripe\Instances;
use Minds\Common\StaticToInstance;
use Minds\Core\Config\Config;
use Minds\Core\Di\Di;
/**
* @method AccountInstance create()
* @method AccountInstance retrieve()
*/
class PaymentMethodInstance extends StaticToInstance
{
public function __construct(Config $config = null)
{
$config = $config ?? Di::_()->get('Config');
\Stripe\Stripe::setApiKey($config->get('payments')['stripe']['api_key']);
$this->setClass(new \Stripe\PaymentMethod);
}
}
<?php
namespace Minds\Core\Payments\Stripe\Instances;
use Minds\Common\StaticToInstance;
use Minds\Core\Config\Config;
use Minds\Core\Di\Di;
/**
* @method AccountInstance create()
* @method AccountInstance retrieve()
*/
class SetupIntentInstance extends StaticToInstance
{
public function __construct(Config $config = null)
{
$config = $config ?? Di::_()->get('Config');
\Stripe\Stripe::setApiKey($config->get('payments')['stripe']['api_key']);
$this->setClass(new \Stripe\SetupIntent);
}
}
......@@ -11,34 +11,19 @@ class Intent
use MagicAttributes;
/** @var string $id */
private $id;
/** @var int $amount */
private $amount = 0;
/** @var int $quantity */
private $quantity = 1;
/** @var string $currency */
private $currency = 'usd';
/** @var int $serviceFeePct */
private $serviceFeePct = 0;
protected $id;
/** @var string $customerId */
private $customerId;
protected $customerId;
/** @var string $paymentMethod */
protected $paymentMethod;
/** @var string $stripeAccountId */
private $stripeAccountId;
protected $stripeAccountId;
/**
* Return the service
* @return int
*/
public function getServiceFee(): int
{
return $this->amount * ($this->serviceFeePct / 100);
}
/** @var string $clientSecret */
protected $clientSecret;
/**
* Expose to the public apis
......@@ -48,6 +33,8 @@ class Intent
public function export($extend = [])
{
return [
'id' => $this->id,
'client_secret' => $this->clientSecret,
];
}
......
......@@ -2,32 +2,105 @@
namespace Minds\Core\Payments\Stripe\Intents;
use Minds\Core\Payments\Stripe\Customers\Manager as CustomersManager;
use Minds\Core\Payments\Stripe\Instances\PaymentIntentInstance;
use Minds\Core\Payments\Stripe\Instances\SetupIntentInstance;
class Manager
{
/** @var CustomersManager $customersManager */
private $customersManager;
/** @var PaymentIntentInstance $paymentIntentInstance */
private $paymentIntentInstance;
/** @var SetupIntentInstance $setupIntentInstance */
private $setupIntentInstance;
public function __construct(
CustomersManager $customersManager = null,
PaymentIntentInstance $paymentIntentInstance = null,
SetupIntentInstance $setupIntentInstance = null
)
{
$this->customersManager = $customersManager ?? new CustomersManager;
$this->paymentIntentInstance = $paymentIntentInstance ?: new PaymentIntentInstance();
$this->setupIntentInstance = $setupIntentInstance ?: new SetupIntentInstance();
}
/**
* Add a payment intent to stripe
* @param Intent $intent
* @return string
*/
public function add(Intent $intent): string
public function add(Intent $intent): Intent
{
switch (true) {
case $intent instanceof PaymentIntent:
return $this->addPayment($intent);
break;
case $intent instanceof SetupIntent:
return $this->addSetup($intent);
break;
default:
throw new \Exception('Intent not supported or not an intent');
}
}
private function addPayment(PaymentIntent $intent) : PaymentIntent
{
$customer = $this->customersManager->getFromUserGuid($intent->getUserGuid());
$params = [
'amount' => $intent->getAmount(),
'currency' => $intent->getCurrency(),
'setup_future_usage' => 'off_session',
'payment_method_types' => [
'card',
],
'application_fee_amount' => $intent->getServiceFee(),
'customer' => $customer->getId(),
'payment_method' => $intent->getPaymentMethod(),
'off_session' => $intent->isOffSession(),
'confirm' => $intent->isConfirm(),
'on_behalf_of' => $intent->getStripeAccountId(),
'transfer_data' => [
'destination' => $order->getStripeAccountId(),
'destination' => $intent->getStripeAccountId(),
],
];
$stripeIntent = \Stripe\PaymentIntent::create($params);
if ($intent->getServiceFee()) {
$params['application_fee_amount'] = $intent->getServiceFee();
}
$stripeIntent = $this->paymentIntentInstance->create($params);
$intent->setId($stripeIntent->id);
return $intent;
}
private function addSetup(SetupIntent $intent) : SetupIntent
{
$params = [
'usage' => 'off_session',
];
$stripeIntent = $this->setupIntentInstance->create($params);
$intent->setId($stripeIntent->id)
->setClientSecret($stripeIntent->client_secret);
return $intent;
}
public function get($id)
{
$stripeIntent = $this->setupIntentInstance->retrieve($id);
$intent = new SetupIntent();
$intent->setId($id)
->setPaymentMethod($stripeIntent->payment_method);
return $stripeIntent->id;
return $intent;
}
}
\ No newline at end of file
}
<?php
/**
* Stripe Payment Intent
*/
namespace Minds\Core\Payments\Stripe\Intents;
use Minds\Traits\MagicAttributes;
class PaymentIntent extends Intent
{
use MagicAttributes;
/** @var int $amount */
private $amount = 0;
/** @var int $quantity */
private $quantity = 1;
/** @var string $currency */
private $currency = 'usd';
/** @var boolean $confirm */
private $confirm = false;
/** @var boolean $offSession */
private $offSession = false;
/** @var int $serviceFeePct */
private $serviceFeePct = 0;
/**
* Return the service
* @return int
*/
public function getServiceFee(): int
{
return $this->amount * ($this->serviceFeePct / 100);
}
/**
* Expose to the public apis
* @param array $extend
* @return array
*/
public function export($extend = [])
{
return [
];
}
}
<?php
/**
* Stripe Payment Intent
*/
namespace Minds\Core\Payments\Stripe\Intents;
use Minds\Traits\MagicAttributes;
class SetupIntent extends Intent
{
use MagicAttributes;
/**
* Expose to the public apis
* @param array $extend
* @return array
*/
public function export($extend = [])
{
return parent::export($extend);
}
}
<?php
namespace Minds\Core\Payments\Stripe\PaymentMethods;
use Minds\Common\Repository\Response;
use Minds\Core\Di\Di;
use Minds\Core\Payments\Stripe\Customers\Manager as CustomersManager;
use Minds\Core\Payments\Stripe\Customers\Customer;
use Minds\Core\Payments\Stripe\Instances\PaymentMethodInstance;
class Manager
{
/** @var Lookup $lookup */
private $lookup;
/** @var CustomersManager $customersManager */
private $customersManager;
/** @var PaymentMethodInstance $paymentMethodInstance */
private $paymentMethodInstance;
public function __construct(
$lookup = null,
$customersManager = null,
$paymentMethodInstance = null)
{
$this->lookup = $lookup ?: Di::_()->get('Database\Cassandra\Lookup');
$this->customersManager = $customersManager ?? new CustomersManager;
$this->paymentMethodInstance = $paymentMethodInstance ?? new PaymentMethodInstance();
}
/**
* Return a list of payment method
* @param array $opts
* @return Response
*/
public function getList(array $opts = []) : Response
{
if (!$opts['user_guid']) {
throw new \Exception('User_guid not specified');
}
$customer = $this->customersManager->getFromUserGuid($opts['user_guid']);
$stripePaymentMethods = $this->paymentMethodInstance->all([
'customer' => $customer->getId(),
'type' => 'card',
]);
$response = new Response();
foreach ($stripePaymentMethods->data as $stripePaymentMethod) {
$paymentMethod = new PaymentMethod();
$paymentMethod->setId($stripePaymentMethod->id)
->setCardBrand($stripePaymentMethod->card->brand)
->setCardCountry($stripePaymentMethod->card->country)
->setCardExpires($stripePaymentMethod->card->exp_month . '/' . $stripePaymentMethod->card->exp_year)
->setCardLast4($stripePaymentMethod->card->last4);
$response[] = $paymentMethod;
}
return $response;
}
/**
* Add a payment method to stripe
* @param Customer $customer
* @return boolean
*/
public function add(PaymentMethod $paymentMethod) : PaymentMethod
{
if ($paymentMethod->getCustomerId()) {
$stripePaymentMethod = $this->paymentMethodInstance->retrieve($paymentMethod->getId());
$stripePaymentMethod->attach([ 'customer' => $paymentMethod->getCustomerId() ]);
} else {
$customer = new Customer();
$customer->setPaymentMethod($paymentMethod->getId())
->setUserGuid($paymentMethod->getUserGuid());
$customer = $this->customersManager->add($customer);
$paymentMethod->setCustomerId($customer->getId());
}
return $paymentMethod;
}
/**
* Delete a payment method
* @param string $id
* @return bool
*/
public function delete($id) : bool
{
$stripePaymentMethod = $this->paymentMethodInstance->retrieve($id);
return (bool) $stripePaymentMethod->detach();
}
}
<?php
/**
* Stripe PaymentMethod
*/
namespace Minds\Core\Payments\Stripe\PaymentMethods;
use Minds\Traits\MagicAttributes;
class PaymentMethod
{
use MagicAttributes;
/** @var string $id */
private $id;
/** @var string $customerId */
private $customerId;
/** @var string $userGuid */
private $userGuid;
/** @var string $cardBrand */
private $cardBrand;
/** @var string $cardExpires */
private $cardExpires;
/** @var string $cardCountry */
private $cardCountry;
/** @var int $cardLast4 */
private $cardLast4;
/**
* Expose to the public apis
* @param array $extend
* @return array
*/
public function export($extend = [])
{
return [
'id' => $this->id,
'user_guid' => (string) $this->userGuid,
'user' => $this->user ? $this->user->export() : null,
'card_brand' => $this->cardBrand,
'card_country' => $this->cardCountry,
'card_expires' => $this->cardExpires,
'card_last4' => $this->cardLast4,
];
}
}
......@@ -321,7 +321,7 @@ class Stripe implements SubscriptionPaymentServiceInterface
);
$params = [
'type' => 'charge',
'type' => 'payment',
'limit' => $hasFilter ? 100 : (int) $options[$limit]
];
......@@ -552,7 +552,7 @@ class Stripe implements SubscriptionPaymentServiceInterface
} catch (\Exception $e) {
return false;
}
$customer->setPaymentMethods($result->sources->data);
return $customer;
......
<?php
namespace Minds\Core\Wire\Delegates;
use Minds\Core\Di\Di;
use Minds\Core\Wire\Counter;
use Minds\Core\Wire\Wire;
class CacheDelegate
{
/** @var Data\cache\Redis */
private $cache;
public function __construct($cache = null)
{
$this->cache = $cache ?: Di::_()->get('Cache');
}
/**
* OnAdd, clear the cache
* @param Wire $wire
* @return void
*/
public function onAdd(Wire $wire): void
{
$this->cache->destroy(Counter::getIndexName($wire->getEntity()->guid, null, 'tokens', null, true));
}
}
<?php
namespace Minds\Core\Wire\Delegates;
use Minds\Core\Wire\Wire;
use Minds\Core\Queue;
class NotificationDelegate
{
/** @var Queue\Client $queue */
protected $queue;
public function __construct($queue = null)
{
$this->queue = $queue ?: Queue\Client::build();
}
/**
* OnAdd, send a notification
* @param Wire $wire
* @return void
*/
public function onAdd(Wire $wire) : void
{
$this->queue->setQueue('WireNotification')
->send(
[
'wire' => serialize($wire),
'entity' => serialize($wire->getEntity()),
]
);
}
}
<?php
namespace Minds\Core\Wire\Delegates;
use Minds\Core\Di\Di;
use Minds\Core\Wire\Wire;
use Minds\Core\Payments\Subscriptions\Subscription;
class RecurringDelegate
{
/** @var $subscriptionsManager */
private $subscriptionsManager;
public function __construct($subscriptionsManager = null)
{
$this->subscriptionsManager = $subscriptionsManager ?? Di::_()->get('Payments\Subscriptions\Manager');
}
/**
* OnAdd, make subscription
* @param Wire $wire
* @return void
*/
public function onAdd(Wire $wire): void
{
$urn = "urn:subscription:" . implode('-', [
$wire->getAddress(), //offchain or onchain wallet or usd
$wire->getSender()->getGuid(),
$wire->getReceiver()->getGuid(),
]);
$subscription = (new Subscription())
->setId($urn)
->setPlanId('wire')
->setPaymentMethod($wire->getMethod())
->setAmount($wire->getAmount())
->setUser($wire->getSender())
->setEntity($wire->getReceiver());
$this->subscriptionsManager->setSubscription($subscription);
$this->subscriptionsManager->create();
}
}
......@@ -17,18 +17,14 @@ use Minds\Core\Wire\Subscriptions\Manager as SubscriptionsManager;
use Minds\Common\Urn;
use Minds\Entities;
use Minds\Entities\User;
use Minds\Core\Payments\Stripe\Intents\PaymentIntent;
use Minds\Core\Payments\Stripe\Intents\Manager as StripeIntentsManager;
class Manager
{
/** @var Data\cache\Redis */
protected $cache;
/** @var Repository */
protected $repository;
/** @var SubscriptionsManager $subscriptionsManager */
protected $subscriptionsManager;
/** @var Core\Blockchain\Transactions\Manager */
protected $txManager;
......@@ -56,9 +52,6 @@ class Manager
/** @var Core\Config */
protected $config;
/** @var Core\Queue\Client */
protected $queue;
/** @var Core\Blockchain\Services\Ethereum */
protected $client;
......@@ -67,44 +60,53 @@ class Manager
/** @var Core\Blockchain\Wallets\OffChain\Cap $cap */
protected $cap;
/** @var Core\Events\EventsDispatcher */
protected $dispatcher;
/** @var Delegates\Plus $plusDelegate */
protected $plusDelegate;
/** @var Delegates\RecurringDelegate $recurringDelegate */
protected $recurringDelegate;
/** @var Delegates\NotificationDelegate $notificationDelegate */
protected $notificationDelegate;
/** @var Delegates\CacheDelete $cacheDelegate */
protected $cacheDelegate;
/** @var Core\Blockchain\Wallets\OffChain\Transactions */
protected $offchainTxs;
/** @var StripeIntentsManager $stripeIntentsManager */
protected $stripeIntentsManager;
public function __construct(
$cache = null,
$repository = null,
$subscriptionsManager = null,
$txManager = null,
$txRepo = null,
$config = null,
$queue = null,
$client = null,
$token = null,
$cap = null,
$dispatcher = null,
$plusDelegate = null,
$offchainTxs = null
$recurringDelegate = null,
$notificationDelegate = null,
$cacheDelegate = null,
$offchainTxs = null,
$stripeIntentsManager = null
) {
$this->cache = $cache ?: Di::_()->get('Cache');
$this->repository = $repository ?: Di::_()->get('Wire\Repository');
$this->subscriptionsManager = $subscriptionsManager ?: Di::_()->get('Wire\Subscriptions\Manager');
$this->txManager = $txManager ?: Di::_()->get('Blockchain\Transactions\Manager');
$this->txRepo = $txRepo ?: Di::_()->get('Blockchain\Transactions\Repository');
$this->config = $config ?: Di::_()->get('Config');
$this->queue = $queue ?: Core\Queue\Client::build();
$this->client = $client ?: Di::_()->get('Blockchain\Services\Ethereum');
$this->token = $token ?: Di::_()->get('Blockchain\Token');
$this->cap = $cap ?: Di::_()->get('Blockchain\Wallets\OffChain\Cap');
$this->dispatcher = $dispatcher ?: Di::_()->get('EventsDispatcher');
$this->plusDelegate = $plusDelegate ?: new Delegates\Plus();
$this->recurringDelegate = $recurringDelegate ?: new Delegates\RecurringDelegate();
$this->notificationDelegate = $notificationDelegate ?: new Delegates\NotificationDelegate();
$this->cacheDelegate = $cacheDelegate ?: new Delegates\CacheDelegate();
$this->offchainTxs = $offchainTxs ?: new Core\Blockchain\Wallets\OffChain\Transactions();
$this->stripeIntentsManager = $stripeIntentsManager ?? Di::_()->get('Stripe\Intents\Manager');
}
/**
......@@ -183,13 +185,6 @@ class Manager
throw new WalletNotSetupException();
}
if ($this->recurring) {
$this->subscriptionsManager
->setAmount($this->amount)
->setSender($this->sender)
->setReceiver($this->receiver);
}
$wire = new Wire();
$wire
->setSender($this->sender)
......@@ -220,11 +215,7 @@ class Manager
]);
$this->txManager->add($transaction);
if ($this->recurring) {
$this->subscriptionsManager
->setAddress($this->payload['address'])
->create();
}
$wire->setAddress($this->payload['address']);
break;
case 'offchain':
......@@ -255,22 +246,17 @@ class Manager
// Save the wire to the Repository
$this->repository->add($wire);
$wire->setAddress('offchain');
// Notify plus
$this->plusDelegate
->onWire($wire, 'offchain');
// Send notification
$this->sendNotification($wire);
$this->notificationDelegate->onAdd($wire);
// Clear caches
$this->clearWireCache($wire);
// Register the next subscription, if needed
if ($this->recurring) {
$this->subscriptionsManager
->setAddress('offchain')
->create();
}
$this->cacheDelegate->onAdd($wire);
break;
case 'erc20':
......@@ -279,35 +265,37 @@ class Manager
case 'eth':
throw new \Exception("Not implemented ETH yet");
break;
case 'money':
$intent = new StripePaymentIntent();
case 'usd':
$intent = new PaymentIntent();
$intent
->setUserGuid($this->sender->getGuid())
->setAmount($this->amount)
->setDestination($this->receiver->getStripeAccount());
->setPaymentMethod($this->payload['paymentMethodId'])
->setOffSession(true)
->setConfirm(true)
->setStripeAccountId($this->receiver->getMerchant()['id']);
// Charge stripe
$this->stripeManager->charge($intent);
// Register the next subscription, if needed
if ($this->recurring) {
$this->subscriptionsManager
->setMethod('money')
->setAddress('stripe')
->create();
}
$this->stripeIntentsManager->add($intent);
$wire->setAddress('stripe')
->setMethod('usd');
// Save the wire to the Repository
$this->repository->add($wire);
// Send notification
$this->sendNotification($wire);
$this->notificationDelegate->onAdd($wire);
// Clear caches
$this->clearWireCache($wire);
$this->cacheDelegate->onAdd($wire);
break;
}
if ($this->recurring) {
$this->recurringDelegate->onAdd($wire);
}
return true;
}
......@@ -342,29 +330,12 @@ class Manager
$this->plusDelegate
->onWire($wire, $data['receiver_address']);
$this->sendNotification($wire);
$this->notificationDelegate->onAdd($wire);
$this->clearWireCache($wire);
$this->cacheDelegate->onAdd($wire);
return $success;
}
/**
* @param Wire $wire
*/
protected function clearWireCache(Wire $wire)
{
$this->cache->destroy(Counter::getIndexName($wire->getEntity()->guid, null, 'tokens', null, true));
}
protected function sendNotification(Wire $wire = null)
{
$this->queue->setQueue('WireNotification')
->send(
[
'wire' => serialize($wire),
'entity' => serialize($wire->getEntity()),
]
);
}
}
......@@ -29,9 +29,12 @@ class Wire
/** @var bool **/
private $recurring = false;
/** @var method **/
/** @var string $method */
private $method = 'tokens';
/** @var string $address */
private $address;
/** @var int $timestamp **/
private $timestamp;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment