Release v3.4.0: coupon payment references and split coupon recording - #43
Merged
Merged
Conversation
Coupon tags go in the high 20 bytes the order id leaves free, so a split purchase gets two references that still point at the same order.
Covers card, full coupon, and a coupon split with card, an external wallet or the device wallet, reading each reference back to its order.
Given the coupon and remainder references and the coupon's cents, the SDK records the remainder line, then the coupon line, skipping any line already spent so a retry after a partial failure is safe.
Coupon lines now record their cents as the token amount, and a new case retries a split whose first line already landed.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Coupon payments: tagged payment references and split recording
What this does
Lets the backend record coupon purchases onchain without any contract change. A coupon purchase is recorded as
FiatinUSD, and its payment reference carries a tag in the high bytes that the order id leaves free. A coupon that covers only part of the price is recorded as two lines of the same order, and the SDK can now record both in one call.Payment reference tags
The backend's reference is the order's Mongo ObjectId, left-padded to 32 bytes, so the high 20 bytes are always zero. The SDK writes a tag there:
StandardCoupon0xfee0ffCouponPart0xfee0ffc0deRemainder0xfee0ffba1a5ce00xfee0ff, so coupon purchases are visible on a block explorer.Standard, so nothing already onchain changes.New on
KokioAdmin.utils:tagPaymentReference(reference, kind): writes the tag. Refuses a reference that is already tagged, not 32 bytes, or has an empty order part.parsePaymentReference(reference): returns{ kind, reference }, the untagged reference included. Refuses a tag the SDK did not write.PaymentReferenceKindis exported fromkokio-sdk/admin.Recording a split coupon order
admin.registry.recordSettledPurchasekeeps its current form and gains a second one:detailscarries the full price and how the user paid the rest.assetandtokenAmountare what they paid. The SDK then:CouponPartandRemainder, and that the coupon covers more than 0 and less than the full price.Fiat,USD, the coupon's cents astokenAmount).If both lines are already recorded, the contract refuses the call with
PaymentReferenceAlreadyUsed, the same error a retried single reference gets.This form is for coupon + card and coupon + external wallet. For coupon + device wallet the user's own purchase records the remainder line, and the backend records the coupon line with a single reference.
New errors
Both are exported from
kokio-sdk/admin:InvalidPaymentReferenceError: a reference that cannot be tagged or read back, or split references that do not belong together.CouponSplitOutOfRangeError: a coupon that covers none or all of the price.Tests
couponPayment(fork and live) that records a bundle every way it can be paid: card, full coupon, and a coupon split with card, external wallet or device wallet, plus a retried split. Each reference is read back to its order. Run withnpm run test:consumer:fork.Compatibility
No breaking changes. Every existing call works as before, and the single-reference
recordSettledPurchasedoes not check tags.Version: 3.3.0 → 3.4.0