-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Payment service: fixed computing payment amounts
1. It was previously falsely assumed that total_amount_accepted for an activity or agreement is equal to the total amount of scheduled payments. That is not true in case of accepting non-binding debit notes (i.e. without due date) which increases total_amount_accepted but doesn't trigger payment. This was fixed by introducing total_amount_scheduled field to both activity and agreement model. This field is updated when a payment is scheduled and it is used to compute the payment amount. 2. Allocation's remaining amount was being updated when a payment was confirmed not when it was scheduled, thus allowing double-spending from an allocation if a new payment was scheduled before the previous one got confirmed. Now spent & remaining amounts are updated when scheduling payment to prevent double-spending. 3. Paying zero-amount invoices was causing unnecessary transaction costs. A quick fix has been applied not to schedule payments for zero amount. Although, a proper solution that disallows issuing such invoices in the first place is required. Signed-off-by: Adam Wierzbicki <[email protected]>
- Loading branch information
Showing
13 changed files
with
186 additions
and
25 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
46 changes: 46 additions & 0 deletions
46
core/payment/migrations/2021-01-28-102818_scheduled_amount/down.sql
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,46 @@ | ||
-- HACK: removing column 'total_amount_scheduled' | ||
|
||
PRAGMA foreign_keys=off; | ||
|
||
CREATE TABLE pay_agreement_tmp( | ||
id VARCHAR(50) NOT NULL, | ||
owner_id VARCHAR(50) NOT NULL, | ||
role CHAR(1) NOT NULL CHECK (role in ('R', 'P')), | ||
peer_id VARCHAR(50) NOT NULL, | ||
payee_addr VARCHAR(50) NOT NULL, | ||
payer_addr VARCHAR(50) NOT NULL, | ||
payment_platform VARCHAR(50) NOT NULL, | ||
total_amount_due VARCHAR(32) NOT NULL, | ||
total_amount_accepted VARCHAR(32) NOT NULL, | ||
total_amount_paid VARCHAR(32) NOT NULL, | ||
app_session_id VARCHAR(50) NULL, | ||
PRIMARY KEY (owner_id, id), | ||
UNIQUE (id, role) | ||
); | ||
|
||
CREATE TABLE pay_activity_tmp( | ||
id VARCHAR(50) NOT NULL, | ||
owner_id VARCHAR(50) NOT NULL, | ||
role CHAR(1) NOT NULL CHECK (role in ('R', 'P')), | ||
agreement_id VARCHAR(50) NOT NULL, | ||
total_amount_due VARCHAR(32) NOT NULL, | ||
total_amount_accepted VARCHAR(32) NOT NULL, | ||
total_amount_paid VARCHAR(32) NOT NULL, | ||
PRIMARY KEY(owner_id, id), | ||
UNIQUE (id, role), | ||
FOREIGN KEY(owner_id, agreement_id) REFERENCES pay_agreement (owner_id, id) | ||
); | ||
|
||
INSERT INTO pay_agreement_tmp(id, owner_id, role, peer_id, payee_addr, payer_addr, payment_platform, total_amount_due, total_amount_accepted, total_amount_paid, app_session_id) | ||
SELECT id, owner_id, role, peer_id, payee_addr, payer_addr, payment_platform, total_amount_due, total_amount_accepted, total_amount_paid, app_session_id FROM pay_agreement; | ||
|
||
INSERT INTO pay_activity_tmp(id, owner_id, role, agreement_id, total_amount_due, total_amount_accepted, total_amount_paid) | ||
SELECT id, owner_id, role, agreement_id, total_amount_due, total_amount_accepted, total_amount_paid FROM pay_activity; | ||
|
||
DROP TABLE pay_agreement; | ||
DROP TABLE pay_activity; | ||
|
||
ALTER TABLE pay_agreement_tmp RENAME TO pay_agreement; | ||
ALTER TABLE pay_activity_tmp RENAME TO pay_activity; | ||
|
||
PRAGMA foreign_keys=on; |
48 changes: 48 additions & 0 deletions
48
core/payment/migrations/2021-01-28-102818_scheduled_amount/up.sql
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,48 @@ | ||
-- HACK: Adding not-null column 'total_amount_scheduled' without default value | ||
|
||
PRAGMA foreign_keys=off; | ||
|
||
CREATE TABLE pay_agreement_tmp( | ||
id VARCHAR(50) NOT NULL, | ||
owner_id VARCHAR(50) NOT NULL, | ||
role CHAR(1) NOT NULL CHECK (role in ('R', 'P')), | ||
peer_id VARCHAR(50) NOT NULL, | ||
payee_addr VARCHAR(50) NOT NULL, | ||
payer_addr VARCHAR(50) NOT NULL, | ||
payment_platform VARCHAR(50) NOT NULL, | ||
total_amount_due VARCHAR(32) NOT NULL, | ||
total_amount_accepted VARCHAR(32) NOT NULL, | ||
total_amount_scheduled VARCHAR(32) NOT NULL, | ||
total_amount_paid VARCHAR(32) NOT NULL, | ||
app_session_id VARCHAR(50) NULL, | ||
PRIMARY KEY (owner_id, id), | ||
UNIQUE (id, role) | ||
); | ||
|
||
CREATE TABLE pay_activity_tmp( | ||
id VARCHAR(50) NOT NULL, | ||
owner_id VARCHAR(50) NOT NULL, | ||
role CHAR(1) NOT NULL CHECK (role in ('R', 'P')), | ||
agreement_id VARCHAR(50) NOT NULL, | ||
total_amount_due VARCHAR(32) NOT NULL, | ||
total_amount_accepted VARCHAR(32) NOT NULL, | ||
total_amount_scheduled VARCHAR(32) NOT NULL, | ||
total_amount_paid VARCHAR(32) NOT NULL, | ||
PRIMARY KEY(owner_id, id), | ||
UNIQUE (id, role), | ||
FOREIGN KEY(owner_id, agreement_id) REFERENCES pay_agreement (owner_id, id) | ||
); | ||
|
||
INSERT INTO pay_agreement_tmp(id, owner_id, role, peer_id, payee_addr, payer_addr, payment_platform, total_amount_due, total_amount_accepted, total_amount_scheduled, total_amount_paid, app_session_id) | ||
SELECT id, owner_id, role, peer_id, payee_addr, payer_addr, payment_platform, total_amount_due, total_amount_accepted, total_amount_accepted AS total_amount_scheduled, total_amount_paid, app_session_id FROM pay_agreement; | ||
|
||
INSERT INTO pay_activity_tmp(id, owner_id, role, agreement_id, total_amount_due, total_amount_accepted, total_amount_scheduled, total_amount_paid) | ||
SELECT id, owner_id, role, agreement_id, total_amount_due, total_amount_accepted, total_amount_accepted AS total_amount_scheduled, total_amount_paid FROM pay_activity; | ||
|
||
DROP TABLE pay_agreement; | ||
DROP TABLE pay_activity; | ||
|
||
ALTER TABLE pay_agreement_tmp RENAME TO pay_agreement; | ||
ALTER TABLE pay_activity_tmp RENAME TO pay_activity; | ||
|
||
PRAGMA foreign_keys=on; |
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
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