@moneymq/react 0.3.2 → 0.5.0

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
@@ -428,22 +428,23 @@ function encodeFormData(data) {
428
428
  function normalizeRpcUrl2(url) {
429
429
  return url.replace("0.0.0.0", "localhost").replace("127.0.0.1", "localhost");
430
430
  }
431
- async function makeRequestWith402Handling(url, method, body, secretKeyHex, rpcUrl, headers = {}) {
431
+ async function makeRequestWith402Handling(url, method, body, secretKeyHex, rpcUrl, headers = {}, useJson = false) {
432
432
  console.log(`[MoneyMQ] Making ${method} request to ${url}`);
433
- const formData = body ? encodeFormData(body) : void 0;
433
+ const contentType = useJson ? "application/json" : "application/x-www-form-urlencoded";
434
+ const requestBody = body ? useJson ? JSON.stringify(body) : encodeFormData(body) : void 0;
434
435
  let response = await fetch(url, {
435
436
  method,
436
437
  headers: {
437
- "Content-Type": "application/x-www-form-urlencoded",
438
+ "Content-Type": contentType,
438
439
  ...headers
439
440
  },
440
- body: formData
441
+ body: requestBody
441
442
  });
442
443
  let data = await response.json();
443
444
  console.log(`[MoneyMQ] Response status: ${response.status}`, data);
444
445
  if (response.status === 402) {
445
446
  console.log("[MoneyMQ] \u{1F4B3} 402 Payment Required - processing payment...");
446
- const paymentRequirements = data?.payment_requirements || data?.error?.payment_requirements || [];
447
+ const paymentRequirements = data?.accepts || data?.payment_requirements || data?.error?.payment_requirements || [];
447
448
  if (paymentRequirements.length === 0) {
448
449
  console.warn("[MoneyMQ] \u26A0\uFE0F No payment requirements found in 402 response");
449
450
  throw new Error("Payment required but no payment requirements provided");
@@ -471,11 +472,11 @@ async function makeRequestWith402Handling(url, method, body, secretKeyHex, rpcUr
471
472
  response = await fetch(url, {
472
473
  method,
473
474
  headers: {
474
- "Content-Type": "application/x-www-form-urlencoded",
475
+ "Content-Type": contentType,
475
476
  "X-Payment": paymentHeaderValue,
476
477
  ...headers
477
478
  },
478
- body: formData
479
+ body: requestBody
479
480
  });
480
481
  data = await response.json();
481
482
  console.log(`[MoneyMQ] Retry response status: ${response.status}`, data);
@@ -488,29 +489,44 @@ async function makeRequestWith402Handling(url, method, body, secretKeyHex, rpcUr
488
489
  return data;
489
490
  }
490
491
  async function createSandboxPayment(apiUrl, rpcUrl, amount, currency, recipient, senderAddress, secretKeyHex, lineItems) {
491
- console.log("[MoneyMQ] Creating sandbox payment...", { amount, currency, recipient, senderAddress });
492
- const description = lineItems && lineItems.length > 0 ? `Purchase - ${lineItems.map((item) => item.product.name).join(", ")}` : "Payment";
493
- const paymentIntent = await makeRequestWith402Handling(
494
- `${apiUrl}/catalog/v1/payment_intents`,
492
+ console.log("[MoneyMQ] Creating checkout session...", { amount, currency, recipient, senderAddress });
493
+ const checkoutLineItems = lineItems?.map((item) => ({
494
+ price_data: {
495
+ currency: item.price.currency.toLowerCase(),
496
+ unit_amount: item.price.unit_amount,
497
+ product_data: {
498
+ name: item.product.name,
499
+ description: item.product.description || void 0,
500
+ metadata: {
501
+ product_id: item.product.id
502
+ }
503
+ }
504
+ },
505
+ quantity: item.quantity
506
+ })) || [];
507
+ const checkoutSession = await makeRequestWith402Handling(
508
+ `${apiUrl}/catalog/v1/checkout/sessions`,
495
509
  "POST",
496
510
  {
497
- amount: Math.round(amount * 100),
498
- // Convert to cents (Stripe-style)
499
- currency: currency.toLowerCase(),
511
+ line_items: checkoutLineItems,
500
512
  customer: senderAddress,
501
- description,
502
513
  metadata: {
503
514
  sender_address: senderAddress,
504
515
  recipient_address: recipient
505
- }
516
+ },
517
+ mode: "payment"
506
518
  },
507
519
  secretKeyHex,
508
- rpcUrl
520
+ rpcUrl,
521
+ {},
522
+ true
523
+ // useJson
509
524
  );
510
- console.log("[MoneyMQ] Payment intent created:", paymentIntent);
511
- console.log("[MoneyMQ] Confirming payment intent:", paymentIntent.id);
525
+ console.log("[MoneyMQ] Checkout session created:", checkoutSession);
526
+ const paymentIntentId = checkoutSession.payment_intent;
527
+ console.log("[MoneyMQ] Confirming payment intent:", paymentIntentId);
512
528
  const confirmedIntent = await makeRequestWith402Handling(
513
- `${apiUrl}/catalog/v1/payment_intents/${paymentIntent.id}/confirm`,
529
+ `${apiUrl}/catalog/v1/payment_intents/${paymentIntentId}/confirm`,
514
530
  "POST",
515
531
  {},
516
532
  secretKeyHex,