• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-mirakl-connector / 45615511204

25 Nov 2026 12:37PM UTC coverage: 90.812% (-3.7%) from 94.485%
45615511204

push

github

web-flow
Merge pull request #163 from stripe/bugfix/C-3638/tax_split_calculation_asymmetry

Include refunded taxes in tax-excluded Stripe refunds and reconcile t…

276 of 422 new or added lines in 8 files covered. (65.4%)

2718 of 2993 relevant lines covered (90.81%)

12.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.85
/src/Repository/StripeTransferRepository.php
1
<?php
2

3
namespace App\Repository;
4

5
use App\Entity\StripeTransfer;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7
use Doctrine\DBAL\LockMode;
8
use Doctrine\Persistence\ManagerRegistry;
9

10
/**
11
 * @method StripeTransfer|null find($id, $lockMode = null, $lockVersion = null)
12
 * @method StripeTransfer|null findOneBy(array $criteria, array $orderBy = null)
13
 * @method StripeTransfer[]    findAll()
14
 * @method StripeTransfer[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
15
 */
16
class StripeTransferRepository extends ServiceEntityRepository
17
{
18
    public function __construct(ManagerRegistry $registry)
138 ✔
19
    {
20
        parent::__construct($registry, StripeTransfer::class);
138 ✔
21
    }
22

23
    public function persistAndFlush(StripeTransfer $stripeTransfer): StripeTransfer
56 ✔
24
    {
25
        $this->persist($stripeTransfer);
56 ✔
26
        $this->flush();
56 ✔
27

28
        return $stripeTransfer;
56 ✔
29
    }
30

31
    public function persist(StripeTransfer $stripeTransfer): StripeTransfer
71 ✔
32
    {
33
        $this->getEntityManager()->persist($stripeTransfer);
71 ✔
34

35
        return $stripeTransfer;
71 ✔
36
    }
37

38
    public function flush(): void
72 ✔
39
    {
40
        $this->getEntityManager()->flush();
72 ✔
41
    }
42

43
    /**
44
     * @template TResult
45
     * @param callable(): TResult $operation
46
     * @return TResult
47
     */
48
    public function wrapInTransaction(callable $operation): mixed
17 ✔
49
    {
50
        return $this->getEntityManager()->wrapInTransaction($operation);
17 ✔
51
    }
52

53
    private function mapTransfersByMiraklId(array $transfers): array
53 ✔
54
    {
55
        $map = [];
53 ✔
56
        foreach ($transfers as $transfer) {
53 ✔
57
            $map[$transfer->getMiraklId()] = $transfer;
47 ✔
58
        }
59

60
        return $map;
53 ✔
61
    }
62

63
    private function mapTransfersByMiraklIdAndType(array $transfers): array
8 ✔
64
    {
65
        $map = [];
8 ✔
66
        foreach ($transfers as $transfer) {
8 ✔
67
            if (!isset($map[$transfer->getMiraklId()])) {
4 ✔
68
                $map[$transfer->getMiraklId()] = [];
4 ✔
69
            }
70

71
            $map[$transfer->getMiraklId()][$transfer->getType()] = $transfer;
4 ✔
72
        }
73

74
        return $map;
8 ✔
75
    }
76

77
    public function findTransfersByOrderIds(array $orderIds): array
50 ✔
78
    {
79
        return $this->mapTransfersByMiraklId($this->findBy([
50 ✔
80
            'type' => StripeTransfer::getOrderTypes(),
50 ✔
81
            'miraklId' => $orderIds,
50 ✔
82
        ]));
50 ✔
83
    }
84

85
    public function findTransfersByRefundIds(array $refundIds): array
17 ✔
86
    {
87
        return $this->mapTransfersByMiraklId($this->findBy([
17 ✔
88
            'type' => StripeTransfer::TRANSFER_REFUND,
17 ✔
89
            'miraklId' => $refundIds,
17 ✔
90
        ]));
17 ✔
91
    }
92

93
    public function getReservedRefundAmountForTransaction(string $transactionId, string $excludeMiraklId): int
23 ✔
94
    {
95
        $amount = 0;
23 ✔
96
        foreach ($this->findBy([
23 ✔
97
            'type' => StripeTransfer::TRANSFER_REFUND,
23 ✔
98
            'transactionId' => $transactionId,
23 ✔
99
            'status' => [StripeTransfer::TRANSFER_PENDING, StripeTransfer::TRANSFER_CREATED],
23 ✔
100
        ]) as $transfer) {
23 ✔
101
            if ($transfer->getMiraklId() !== $excludeMiraklId) {
2 ✔
102
                $amount += $transfer->getAmount() ?? 0;
1 ✔
103
            }
104
        }
105

106
        return $amount;
23 ✔
107
    }
108

109
    /**
110
     * Lock the original order transfers while refund reversals are reserved.
111
     * The caller must already be inside a transaction.
112
     */
113
    public function lockOrderTransfersByMiraklIds(array $miraklIds): void
17 ✔
114
    {
115
        if (!$miraklIds) {
17 ✔
NEW
116
            return;
×
117
        }
118

119
        $this->createQueryBuilder('transfer')
17 ✔
120
            ->where('transfer.type IN (:types)')
17 ✔
121
            ->andWhere('transfer.miraklId IN (:miraklIds)')
17 ✔
122
            ->setParameter('types', StripeTransfer::getOrderTypes())
17 ✔
123
            ->setParameter('miraklIds', $miraklIds)
17 ✔
124
            ->orderBy('transfer.id', 'ASC')
17 ✔
125
            ->getQuery()
17 ✔
126
            ->setLockMode(LockMode::PESSIMISTIC_WRITE)
17 ✔
127
            ->getResult();
17 ✔
128
    }
129

130
    public function findRetriableProductOrderTransfers(): array
11 ✔
131
    {
132
        return $this->mapTransfersByMiraklId($this->findBy([
11 ✔
133
            'type' => StripeTransfer::TRANSFER_PRODUCT_ORDER,
11 ✔
134
            'status' => StripeTransfer::getRetriableStatus(),
11 ✔
135
        ]));
11 ✔
136
    }
137

138
    public function findRetriableServiceOrderTransfers(): array
9 ✔
139
    {
140
        return $this->mapTransfersByMiraklId($this->findBy([
9 ✔
141
            'type' => StripeTransfer::TRANSFER_SERVICE_ORDER,
9 ✔
142
            'status' => StripeTransfer::getRetriableStatus(),
9 ✔
143
        ]));
9 ✔
144
    }
145

146
    public function findRetriableRefundTransfers(): array
14 ✔
147
    {
148
        return $this->mapTransfersByMiraklId($this->findBy([
14 ✔
149
            'type' => StripeTransfer::TRANSFER_REFUND,
14 ✔
150
            'status' => StripeTransfer::getRetriableStatus(),
14 ✔
151
        ]));
14 ✔
152
    }
153

154
    /**
155
     * Refund reversals in these states are needed to reconcile tax shortfalls:
156
     * completed component reversals and pending shortfall reversals awaiting dispatch.
157
     *
158
     * @return StripeTransfer[]
159
     */
160
    public function findRefundTransfersForTaxShortfallReconciliation(): array
1 ✔
161
    {
162
        return $this->findBy([
1 ✔
163
            'type' => StripeTransfer::TRANSFER_REFUND,
1 ✔
164
            'status' => [StripeTransfer::TRANSFER_CREATED, StripeTransfer::TRANSFER_PENDING],
1 ✔
165
        ]);
1 ✔
166
    }
167

168
    public function findRetriableInvoiceTransfers(): array
6 ✔
169
    {
170
        return $this->mapTransfersByMiraklIdAndType($this->findBy([
6 ✔
171
            'type' => StripeTransfer::getInvoiceTypes(),
6 ✔
172
            'status' => StripeTransfer::getRetriableStatus(),
6 ✔
173
        ]));
6 ✔
174
    }
175

176
    public function findTransfersByInvoiceIds(array $invoiceIds): array
6 ✔
177
    {
178
        return $this->mapTransfersByMiraklIdAndType($this->findBy([
6 ✔
179
            'type' => StripeTransfer::getInvoiceTypes(),
6 ✔
180
            'miraklId' => $invoiceIds,
6 ✔
181
        ]));
6 ✔
182
    }
183
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc