Skip to content

Commit

Permalink
Merge pull request #127 from harryharris7/kyc-check
Browse files Browse the repository at this point in the history
KYC build errors fixed
  • Loading branch information
millin-stripe authored Jun 6, 2023
2 parents 1556c8a + d2c7c24 commit dbae8b2
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 105 deletions.
173 changes: 93 additions & 80 deletions src/Command/SellerMonitorKYCStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SellerMonitorKYCStatusCommand extends Command implements LoggerAwareInterf
use LoggerAwareTrait;

protected static $defaultName = 'connector:dispatch:monitor-kyc-status';

public const CURRENTLY_DUE = 'currently_due';
public const PENDING_VERIFICATION = 'pending_verification';
public const DISABLED_REASON = 'disabled_reason';
Expand All @@ -39,7 +39,11 @@ class SellerMonitorKYCStatusCommand extends Command implements LoggerAwareInterf
* @var AccountMappingRepository
*/
private $accountMappingRepository;

public function setAccountMappingRepository(AccountMappingRepository $accountMappingRepository)
{
$this->accountMappingRepository = $accountMappingRepository;
}

/**
* @var ConfigService
*/
Expand All @@ -50,29 +54,46 @@ class SellerMonitorKYCStatusCommand extends Command implements LoggerAwareInterf
*/
private $miraklClient;


/**
* @var StripeClient
*/
private $stripeClient;


public function setStripeClient(StripeClient $stripeClient)
{
$this->stripeClient = $stripeClient;
}

private $mirakl_shop_kyc_disable;

/**
* @var MailerInterface
*/
private $mailer;

public function setMailer(MailerInterface $mailer)
{
$this->mailer = $mailer;
}

/**
* @var string
*/
private $technicalEmailFrom;

public function setTechnicalEmailFrom(string $email)
{
$this->technicalEmailFrom = $email;
}

/**
* @var string
*/
private $technicalEmail;

public function setTechnicalEmail(string $email)
{
$this->technicalEmail = $email;
}

/**
* @var string
*/
Expand All @@ -86,8 +107,9 @@ public function __construct(
StripeClient $stripeClient,
bool $miraklShopKycDisable,
MailerInterface $mailer,
string $technicalEmailFrom, string $technicalEmail,string $customFieldCode

string $technicalEmailFrom,
string $technicalEmail,
string $customFieldCode
) {
$this->bus = $bus;
$this->configService = $configService;
Expand All @@ -106,50 +128,45 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$this->logger->info('starting');
echo "process start";

$stripeAccts = $this->stripeClient->retrieveAllAccounts();
// echo count($stripeAccts);

// echo count($stripeAccts);

$accountsInDB = $this->accountMappingRepository->findAll();


$mirakl_update_shop_kyc_reqs=[];

$kycRequiredAccountDets = [];

foreach ($stripeAccts as $st_acc) {

$kyc_disabled_status = $this->getKYCStatus($st_acc);

// echo "\n-----".$st_acc->id." ".$kyc_disabled_status."mmmmm";

$dbAccount = $this->findAccount($st_acc->id,$accountsInDB);

if($kyc_disabled_status != '' && $dbAccount!=null ){


// echo "\n-----".$st_acc->id." ".$kyc_disabled_status."mmmmm";

$dbAccount = $this->findAccount($st_acc->id, $accountsInDB);

if ($kyc_disabled_status != '' && $dbAccount!=null) {
$dbAccount->setIgnored(true);
$this->accountMappingRepository->persistAndFlush($dbAccount);
$account_link = $this->stripeClient->createAccountLink($st_acc->id,'https://dashboard.stripe.com','https://dashboard.stripe.com');

$account_link = $this->stripeClient->createAccountLink($st_acc->id, 'https://dashboard.stripe.com', 'https://dashboard.stripe.com');


$mirakl_update_shop_kyc_reqs[]=['shop_id' => $dbAccount->getMiraklShopId(),
'kyc' => ['status' => $kyc_disabled_status,'reason' => "STRIPE CONNECT ACCOUNT DISABLEMENT" ],
'shop_additional_fields' => [['code' => $this->customFieldCode, 'value' => $account_link->url]]
];

$kycRequiredAccountDets[] = ['id'=>$dbAccount->getId(),'shop_id' => $dbAccount->getMiraklShopId(),
'connect_account_id'=> $st_acc->id,'reason'=> $kyc_disabled_status ];

}
else if($kyc_disabled_status == '' && $dbAccount!=null && $dbAccount->getIgnored() ){

} elseif ($kyc_disabled_status == '' && $dbAccount!=null && $dbAccount->getIgnored()) {
$dbAccount->setIgnored(false);
$this->accountMappingRepository->persistAndFlush($dbAccount);

$account_link = $this->stripeClient->createLoginLink($st_acc->id);

$mirakl_update_shop_kyc_reqs[]=['shop_id' => $dbAccount->getMiraklShopId(),
'kyc' => ['status' => 'APPROVED','reason' => "KYC aaproved" ],
'shop_additional_fields' => [['code' => $this->customFieldCode, 'value' => $account_link->url]]
Expand All @@ -158,73 +175,69 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
}

echo $this->mirakl_shop_kyc_disable;
if($this->mirakl_shop_kyc_disable && count($mirakl_update_shop_kyc_reqs) > 0 ){


if ($this->mirakl_shop_kyc_disable && count($mirakl_update_shop_kyc_reqs) > 0) {
$this->miraklClient->updateShopKycStatusWithReason($mirakl_update_shop_kyc_reqs);
}
// print_r($kycRequiredAccountDets);
if(count($kycRequiredAccountDets)>0){
$email = (new TemplatedEmail())
->from($this->technicalEmailFrom)
->to($this->technicalEmail)
->subject('[Stripe-Mirakl] KYC Required')
->htmlTemplate('emails/kycRequired.html.twig')
->context([
'kycRequiredAccounts' => $kycRequiredAccountDets
]);

// print_r($kycRequiredAccountDets);

if (count($kycRequiredAccountDets)>0) {
$email = (new TemplatedEmail())
->from($this->technicalEmailFrom)
->to($this->technicalEmail)
->subject('[Stripe-Mirakl] KYC Required')
->htmlTemplate('emails/kycRequired.html.twig')
->context([
'kycRequiredAccounts' => $kycRequiredAccountDets
]);
$this->mailer->send($email);
}




$this->logger->info('job succeeded');
echo "process end";
return 0;
}
private function findAccount($stripeId,$accountsInDB){



private function findAccount($stripeId, $accountsInDB)
{
foreach ($accountsInDB as $acc) {
if($stripeId == (string)$acc->getStripeAccountId())
if ($stripeId == (string)$acc->getStripeAccountId()) {
return $acc;
}
}
return null;
}


private function getKYCStatus(Account $stripeAccount): string
{
$requirements = $stripeAccount->requirements;

if (count($requirements[self::CURRENTLY_DUE]) > 0) {
return self::KYC_STATUS_PENDING_SUBMISSION;
}

if (count($requirements[self::PENDING_VERIFICATION]) > 0) {
return self::KYC_STATUS_PENDING_APPROVAL;
}

if (
$requirements[self::DISABLED_REASON] !== ''
&& strpos($requirements[self::DISABLED_REASON], 'rejected') === 0
) {
return self::KYC_STATUS_REFUSED;
}

if ($requirements[self::DISABLED_REASON] !== '' && $requirements[self::DISABLED_REASON] !== null) {
return self::KYC_STATUS_PENDING_APPROVAL;
}

return '';
}



) {
return self::KYC_STATUS_REFUSED;
}


}
if ($requirements[self::DISABLED_REASON] !== '' && $requirements[self::DISABLED_REASON] !== null) {
return self::KYC_STATUS_PENDING_APPROVAL;
}

return '';
}
}
11 changes: 6 additions & 5 deletions src/Factory/StripeTransferFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ public function updateFromOrder(StripeTransfer $transfer, MiraklOrder $order, Mi

// Shop must have a Stripe account
try {

//account mapping for the original shop
$shop_accountMapping = $this->getAccountMapping($order->getShopId());

if($isForTax)
$taxAccountMapping = $this->getAccountMappingByAccountId($this->stripeTaxAccount);;


if ($isForTax) {
$taxAccountMapping = $this->getAccountMappingByAccountId($this->stripeTaxAccount);
};

if (!$isForTax) {
$accountMapping = $shop_accountMapping;
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/Handler/ProcessRefundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __invoke(ProcessRefundMessage $message)
$refund->setStatusReason(null);
} catch (ApiErrorException $e) {
$this->logger->error(
sprintf('Could not create refund in Stripe: %s.', $e->getMessage()),
sprintf('Could not create refund in Stripe: %s.', $e->getMessage()),
[
'miraklRefundId' => $refund->getMiraklRefundId(),
'miraklOrderId' => $refund->getMiraklOrderId()
Expand All @@ -99,13 +99,13 @@ public function __invoke(ProcessRefundMessage $message)
$this->logger->error(
sprintf('Timeout processing refund: %s.', $e->getMessage()),
[
'stripeRefundId' => $refund->getStripeRefundId(),
'stripeRefundId' => $refund->getStripeRefundId(),
'miraklRefundId' => $refund->getMiraklRefundId(),
'miraklOrderId' => $refund->getMiraklOrderId()
]
);
$refund->setStatus(StripeRefund::REFUND_FAILED);

$refund->setStatus(StripeRefund::REFUND_FAILED);
$refund->setStatusReason(substr($e->getMessage(), 0, 1024));
} catch (ClientException $e) {
$message = $e->getResponse()->getContent(false);
Expand Down
3 changes: 1 addition & 2 deletions src/Service/MiraklClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,11 @@ public function updateShopKycStatus(int $shopId, string $status)
'kyc' => ['status' => $status]
]]]);
}

// S07
public function updateShopKycStatusWithReason($updateShopsReqs)
{
$this->put('/api/shops', ['shops' => $updateShopsReqs]);

}

public function getTransactionsForInvoce(string $invoiceId)
Expand Down
25 changes: 11 additions & 14 deletions src/Service/StripeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Stripe\Transfer;
use Stripe\TransferReversal;
use Stripe\Webhook;
use function PHPUnit\Framework\isEmpty;

/**
* @codeCoverageIgnore
Expand Down Expand Up @@ -66,29 +65,27 @@ public function retrieveAccount(string $accountId): Account
{
return Account::retrieve($accountId);
}

// returns Account []
public function retrieveAllAccounts() : array
public function retrieveAllAccounts(): array
{

$hasmore = false;
$connect_accounts = array();
$lastConnectId='';

$limit = 50;
do{
do {
$params = $lastConnectId=='' ? ['limit' => $limit] : ['limit' => $limit,'starting_after' => $lastConnectId];
$response = Account::all($params);
$connect_accounts = array_merge($connect_accounts,$response['data']);
// echo "\n";
// echo count ($connect_accounts).' ';
$connect_accounts = array_merge($connect_accounts, $response['data']);
// echo "\n";
// echo count ($connect_accounts).' ';
$hasmore = $response['has_more'];
$lastConnectId = end($connect_accounts)['id'];
// echo $hasmore.' ';
// echo $lastConnectId.' ';

}while($hasmore);

// echo $hasmore.' ';
// echo $lastConnectId.' ';
} while ($hasmore);

return $connect_accounts;
}

Expand Down
Loading

0 comments on commit dbae8b2

Please sign in to comment.