moltspay 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -30344,6 +30344,16 @@ var PAYMENT_HEADER = "x-payment";
30344
30344
  var PAYMENT_RESPONSE_HEADER = "x-payment-response";
30345
30345
  var FACILITATOR_TESTNET = "https://www.x402.org/facilitator";
30346
30346
  var FACILITATOR_MAINNET = "https://api.cdp.coinbase.com/platform/v2/x402";
30347
+ var USDC_ADDRESSES = {
30348
+ "eip155:8453": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
30349
+ // Base mainnet
30350
+ "eip155:84532": "0x036CbD53842c5426634e7929541eC2318f3dCF7e"
30351
+ // Base Sepolia
30352
+ };
30353
+ var USDC_DOMAIN = {
30354
+ name: "USD Coin",
30355
+ version: "2"
30356
+ };
30347
30357
  function loadEnvFiles() {
30348
30358
  try {
30349
30359
  const dotenv = __require("dotenv");
@@ -30581,22 +30591,16 @@ var MoltsPayServer = class {
30581
30591
  }, responseHeaders);
30582
30592
  }
30583
30593
  /**
30584
- * Return 402 with x402 payment requirements
30594
+ * Return 402 with x402 payment requirements (v2 format)
30585
30595
  */
30586
30596
  sendPaymentRequired(config, res) {
30587
- const amountInUnits = Math.floor(config.price * 1e6).toString();
30588
- const requirements = [{
30589
- scheme: "exact",
30590
- network: this.networkId,
30591
- maxAmountRequired: amountInUnits,
30592
- resource: this.manifest.provider.wallet,
30593
- description: `${config.name} - $${config.price} ${config.currency}`,
30594
- extra: JSON.stringify({
30595
- facilitator: this.cdpConfig.useMainnet ? "cdp" : "x402.org",
30596
- mainnet: this.cdpConfig.useMainnet
30597
- })
30598
- }];
30599
- const encoded = Buffer.from(JSON.stringify(requirements)).toString("base64");
30597
+ const requirements = this.buildPaymentRequirements(config);
30598
+ requirements.description = `${config.name} - $${config.price} ${config.currency}`;
30599
+ const paymentRequired = {
30600
+ x402Version: X402_VERSION,
30601
+ accepts: [requirements]
30602
+ };
30603
+ const encoded = Buffer.from(JSON.stringify(paymentRequired)).toString("base64");
30600
30604
  res.writeHead(402, {
30601
30605
  "Content-Type": "application/json",
30602
30606
  [PAYMENT_REQUIRED_HEADER]: encoded
@@ -30604,7 +30608,7 @@ var MoltsPayServer = class {
30604
30608
  res.end(JSON.stringify({
30605
30609
  error: "Payment required",
30606
30610
  message: `Service requires $${config.price} ${config.currency}`,
30607
- x402: requirements[0]
30611
+ x402: paymentRequired
30608
30612
  }, null, 2));
30609
30613
  }
30610
30614
  /**
@@ -30622,23 +30626,34 @@ var MoltsPayServer = class {
30622
30626
  }
30623
30627
  return { valid: true };
30624
30628
  }
30629
+ /**
30630
+ * Build complete payment requirements for facilitator
30631
+ */
30632
+ buildPaymentRequirements(config) {
30633
+ const amountInUnits = Math.floor(config.price * 1e6).toString();
30634
+ const usdcAddress = USDC_ADDRESSES[this.networkId];
30635
+ return {
30636
+ scheme: "exact",
30637
+ network: this.networkId,
30638
+ maxAmountRequired: amountInUnits,
30639
+ amount: amountInUnits,
30640
+ asset: usdcAddress,
30641
+ payTo: this.manifest.provider.wallet,
30642
+ maxTimeoutSeconds: 300,
30643
+ extra: USDC_DOMAIN
30644
+ };
30645
+ }
30625
30646
  /**
30626
30647
  * Verify payment with facilitator (testnet or CDP)
30627
30648
  */
30628
30649
  async verifyWithFacilitator(payment, config) {
30629
30650
  try {
30630
- const amountInUnits = Math.floor(config.price * 1e6).toString();
30631
- const requirements = {
30632
- scheme: "exact",
30633
- network: this.networkId,
30634
- maxAmountRequired: amountInUnits,
30635
- resource: this.manifest.provider.wallet,
30636
- payTo: this.manifest.provider.wallet
30637
- };
30651
+ const requirements = this.buildPaymentRequirements(config);
30638
30652
  const requestBody = {
30639
30653
  paymentPayload: payment,
30640
30654
  paymentRequirements: requirements
30641
30655
  };
30656
+ console.log("[MoltsPay] Verify request:", JSON.stringify(requestBody, null, 2));
30642
30657
  let headers = { "Content-Type": "application/json" };
30643
30658
  if (this.cdpConfig.useMainnet) {
30644
30659
  const authHeaders = await getCDPAuthHeaders(
@@ -30654,6 +30669,7 @@ var MoltsPayServer = class {
30654
30669
  body: JSON.stringify(requestBody)
30655
30670
  });
30656
30671
  const result = await response.json();
30672
+ console.log("[MoltsPay] Verify response:", JSON.stringify(result, null, 2));
30657
30673
  if (!response.ok || !result.isValid) {
30658
30674
  return { valid: false, error: result.invalidReason || result.error || "Verification failed" };
30659
30675
  }
@@ -30666,14 +30682,7 @@ var MoltsPayServer = class {
30666
30682
  * Settle payment with facilitator (execute on-chain transfer)
30667
30683
  */
30668
30684
  async settleWithFacilitator(payment, config) {
30669
- const amountInUnits = Math.floor(config.price * 1e6).toString();
30670
- const requirements = {
30671
- scheme: "exact",
30672
- network: this.networkId,
30673
- maxAmountRequired: amountInUnits,
30674
- resource: this.manifest.provider.wallet,
30675
- payTo: this.manifest.provider.wallet
30676
- };
30685
+ const requirements = this.buildPaymentRequirements(config);
30677
30686
  const requestBody = {
30678
30687
  paymentPayload: payment,
30679
30688
  paymentRequirements: requirements