web/website/node/shop-seal-integration.mjs
2026-11-27 23:43:05 +00:00

19 lines
1.7 KiB
JavaScript

// Task 05: provider verification must occur upstream; this service never trusts a client payment flag.
import {purchaseSealDecision} from './seal-entitlements.mjs';
import {grantSealEntitlement,revokeSealEntitlement} from './seal-entitlement-store.mjs';
export async function applyVerifiedShopSeal(db,payment){
if(!payment||payment.provenance!=='server-verified')throw new TypeError('Verified server-side payment required');
const decision=purchaseSealDecision({tier:payment.tier,paymentStatus:payment.status,verified:payment.verified===true,accountId:payment.accountId,orderId:payment.orderId});
if(!decision)return {granted:false,reason:'ineligible-payment'};
const grant=await grantSealEntitlement(db,{accountId:decision.accountId,seal:decision.seal,sourceKind:'purchase',sourceId:decision.sourceId,details:{provider:payment.provider||null,transactionId:payment.transactionId||null}});
return {granted:grant.created,entitlementId:grant.id,seal:decision.seal};
}
// Refunds are tied to the original purchase source; do not revoke unrelated awards.
export async function reverseVerifiedShopSeal(db,payment){
if(!payment||payment.provenance!=='server-verified'||!['refunded','chargeback'].includes(payment.status)||!payment.orderId||!payment.accountId)throw new TypeError('Verified reversal required');
const row=await db.prepare("SELECT id,account_id FROM seal_entitlements WHERE source_kind='purchase' AND source_id=? AND revoked_at IS NULL").bind(payment.orderId).first();
if(!row)return {revoked:false};
if(row.account_id!==payment.accountId)throw new Error('Purchase account mismatch');
const result=await revokeSealEntitlement(db,{entitlementId:row.id,actorId:payment.systemActorId,reason:payment.status});
return {revoked:result.revoked};
}