From f652cc20d655c6e26bbfc57ff43b6e308218b3a1 Mon Sep 17 00:00:00 2001 From: Jordy Heemskerk Date: Wed, 4 Apr 2018 23:39:07 +0200 Subject: [PATCH 1/3] Request money on Sandbox if balance zero. (bunq/tinker_php#14) --- src/BunqLib.php | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/BunqLib.php b/src/BunqLib.php index b6a95d4..df1008e 100644 --- a/src/BunqLib.php +++ b/src/BunqLib.php @@ -19,6 +19,7 @@ use bunq\Model\Generated\Object\Pointer; use bunq\Util\BunqEnumApiEnvironmentType; use bunq\Util\InstallationUtil; +use Habitat\Environment\Environment; /** * Some simple helper methods to get you started in seconds. @@ -66,12 +67,23 @@ class BunqLib const PREG_MATCH_SUCCESS = 1; const REGEX_E164_PHONE = '/^\+\d{3,15}$/'; + /** + * Spending money request constants. + */ + const REQUEST_SPENDING_MONEY_DESCRIPTION = "Requesting some spending money."; + const REQUEST_SPENDING_MONEY_RECIPIENT = "sugardaddy@bunq.com"; + const REQUEST_SPENDING_MONEY_AMOUNT = "500.0"; + + /** + * Zero balance constant. + */ + const BALANCE_ZERO = 0.0; + /** * @var UserCompany|UserPerson|UserLight */ protected $user; - /** * @var BunqEnumApiEnvironmentType */ @@ -85,6 +97,7 @@ public function __construct(BunqEnumApiEnvironmentType $bunqEnumApiEnvironmentTy $this->environment = $bunqEnumApiEnvironmentType; $this->setupContext(); $this->setupCurrentUser(); + $this->requestSpendingMoneyIfNeeded(); } /** @@ -464,4 +477,22 @@ public function getCurrentUser() { return $this->user; } + + /** + * Requests money if the balance of the primary MA is equal to or less than zero. + */ + private function requestSpendingMoneyIfNeeded() + { + if (BunqEnumApiEnvironmentType::SANDBOX()->equals($this->environment) + && (floatval(BunqContext::getUserContext()->getPrimaryMonetaryAccount()->getBalance()->getValue()) + <= self::BALANCE_ZERO) + ) { + RequestInquiry::create( + new Amount(self::REQUEST_SPENDING_MONEY_AMOUNT, self::CURRENCY_TYPE_EUR), + new Pointer(self::POINTER_TYPE_EMAIL, self::REQUEST_SPENDING_MONEY_RECIPIENT), + self::REQUEST_SPENDING_MONEY_DESCRIPTION, + false + ); + } + } } From 83733cefa2efd1bfc2b01c2191505d94d6419db7 Mon Sep 17 00:00:00 2001 From: Jordy Heemskerk Date: Thu, 12 Apr 2018 11:16:55 +0200 Subject: [PATCH 2/3] Update user context after requesting money, and extracted method. (bunq/tinker_php#14) --- src/BunqLib.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/BunqLib.php b/src/BunqLib.php index df1008e..a20e7ea 100644 --- a/src/BunqLib.php +++ b/src/BunqLib.php @@ -19,7 +19,6 @@ use bunq\Model\Generated\Object\Pointer; use bunq\Util\BunqEnumApiEnvironmentType; use bunq\Util\InstallationUtil; -use Habitat\Environment\Environment; /** * Some simple helper methods to get you started in seconds. @@ -73,6 +72,7 @@ class BunqLib const REQUEST_SPENDING_MONEY_DESCRIPTION = "Requesting some spending money."; const REQUEST_SPENDING_MONEY_RECIPIENT = "sugardaddy@bunq.com"; const REQUEST_SPENDING_MONEY_AMOUNT = "500.0"; + const REQUEST_SPENDING_MONEY_WAIT_TIME_SECONDS = 1; /** * Zero balance constant. @@ -483,16 +483,25 @@ public function getCurrentUser() */ private function requestSpendingMoneyIfNeeded() { - if (BunqEnumApiEnvironmentType::SANDBOX()->equals($this->environment) - && (floatval(BunqContext::getUserContext()->getPrimaryMonetaryAccount()->getBalance()->getValue()) - <= self::BALANCE_ZERO) - ) { + if ($this->shouldRequestSpendingMoney()) { RequestInquiry::create( new Amount(self::REQUEST_SPENDING_MONEY_AMOUNT, self::CURRENCY_TYPE_EUR), new Pointer(self::POINTER_TYPE_EMAIL, self::REQUEST_SPENDING_MONEY_RECIPIENT), self::REQUEST_SPENDING_MONEY_DESCRIPTION, false ); + sleep(self::REQUEST_SPENDING_MONEY_WAIT_TIME_SECONDS); + BunqContext::getUserContext()->refreshUserContext(); } } + + /** + * @return bool + */ + private function shouldRequestSpendingMoney(): bool + { + return BunqEnumApiEnvironmentType::SANDBOX()->equals($this->environment) + && (floatval(BunqContext::getUserContext()->getPrimaryMonetaryAccount()->getBalance()->getValue()) + <= self::BALANCE_ZERO); + } } From ac5977a40b37cb3858a52deec6596d6328bb1410 Mon Sep 17 00:00:00 2001 From: Jordy Heemskerk Date: Thu, 12 Apr 2018 14:20:20 +0200 Subject: [PATCH 3/3] Removed non existant function call, until SDK update. (bunq/tinker_php#14) --- src/BunqLib.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/BunqLib.php b/src/BunqLib.php index a20e7ea..e0d3157 100644 --- a/src/BunqLib.php +++ b/src/BunqLib.php @@ -491,7 +491,6 @@ private function requestSpendingMoneyIfNeeded() false ); sleep(self::REQUEST_SPENDING_MONEY_WAIT_TIME_SECONDS); - BunqContext::getUserContext()->refreshUserContext(); } }