@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.js CHANGED
@@ -471,22 +471,23 @@ function encodeFormData(data) {
471
471
  function normalizeRpcUrl2(url) {
472
472
  return url.replace("0.0.0.0", "localhost").replace("127.0.0.1", "localhost");
473
473
  }
474
- async function makeRequestWith402Handling(url, method, body, secretKeyHex, rpcUrl, headers = {}) {
474
+ async function makeRequestWith402Handling(url, method, body, secretKeyHex, rpcUrl, headers = {}, useJson = false) {
475
475
  console.log(`[MoneyMQ] Making ${method} request to ${url}`);
476
- const formData = body ? encodeFormData(body) : void 0;
476
+ const contentType = useJson ? "application/json" : "application/x-www-form-urlencoded";
477
+ const requestBody = body ? useJson ? JSON.stringify(body) : encodeFormData(body) : void 0;
477
478
  let response = await fetch(url, {
478
479
  method,
479
480
  headers: {
480
- "Content-Type": "application/x-www-form-urlencoded",
481
+ "Content-Type": contentType,
481
482
  ...headers
482
483
  },
483
- body: formData
484
+ body: requestBody
484
485
  });
485
486
  let data = await response.json();
486
487
  console.log(`[MoneyMQ] Response status: ${response.status}`, data);
487
488
  if (response.status === 402) {
488
489
  console.log("[MoneyMQ] \u{1F4B3} 402 Payment Required - processing payment...");
489
- const paymentRequirements = data?.payment_requirements || data?.error?.payment_requirements || [];
490
+ const paymentRequirements = data?.accepts || data?.payment_requirements || data?.error?.payment_requirements || [];
490
491
  if (paymentRequirements.length === 0) {
491
492
  console.warn("[MoneyMQ] \u26A0\uFE0F No payment requirements found in 402 response");
492
493
  throw new Error("Payment required but no payment requirements provided");
@@ -514,11 +515,11 @@ async function makeRequestWith402Handling(url, method, body, secretKeyHex, rpcUr
514
515
  response = await fetch(url, {
515
516
  method,
516
517
  headers: {
517
- "Content-Type": "application/x-www-form-urlencoded",
518
+ "Content-Type": contentType,
518
519
  "X-Payment": paymentHeaderValue,
519
520
  ...headers
520
521
  },
521
- body: formData
522
+ body: requestBody
522
523
  });
523
524
  data = await response.json();
524
525
  console.log(`[MoneyMQ] Retry response status: ${response.status}`, data);
@@ -531,29 +532,44 @@ async function makeRequestWith402Handling(url, method, body, secretKeyHex, rpcUr
531
532
  return data;
532
533
  }
533
534
  async function createSandboxPayment(apiUrl, rpcUrl, amount, currency, recipient, senderAddress, secretKeyHex, lineItems) {
534
- console.log("[MoneyMQ] Creating sandbox payment...", { amount, currency, recipient, senderAddress });
535
- const description = lineItems && lineItems.length > 0 ? `Purchase - ${lineItems.map((item) => item.product.name).join(", ")}` : "Payment";
536
- const paymentIntent = await makeRequestWith402Handling(
537
- `${apiUrl}/catalog/v1/payment_intents`,
535
+ console.log("[MoneyMQ] Creating checkout session...", { amount, currency, recipient, senderAddress });
536
+ const checkoutLineItems = lineItems?.map((item) => ({
537
+ price_data: {
538
+ currency: item.price.currency.toLowerCase(),
539
+ unit_amount: item.price.unit_amount,
540
+ product_data: {
541
+ name: item.product.name,
542
+ description: item.product.description || void 0,
543
+ metadata: {
544
+ product_id: item.product.id
545
+ }
546
+ }
547
+ },
548
+ quantity: item.quantity
549
+ })) || [];
550
+ const checkoutSession = await makeRequestWith402Handling(
551
+ `${apiUrl}/catalog/v1/checkout/sessions`,
538
552
  "POST",
539
553
  {
540
- amount: Math.round(amount * 100),
541
- // Convert to cents (Stripe-style)
542
- currency: currency.toLowerCase(),
554
+ line_items: checkoutLineItems,
543
555
  customer: senderAddress,
544
- description,
545
556
  metadata: {
546
557
  sender_address: senderAddress,
547
558
  recipient_address: recipient
548
- }
559
+ },
560
+ mode: "payment"
549
561
  },
550
562
  secretKeyHex,
551
- rpcUrl
563
+ rpcUrl,
564
+ {},
565
+ true
566
+ // useJson
552
567
  );
553
- console.log("[MoneyMQ] Payment intent created:", paymentIntent);
554
- console.log("[MoneyMQ] Confirming payment intent:", paymentIntent.id);
568
+ console.log("[MoneyMQ] Checkout session created:", checkoutSession);
569
+ const paymentIntentId = checkoutSession.payment_intent;
570
+ console.log("[MoneyMQ] Confirming payment intent:", paymentIntentId);
555
571
  const confirmedIntent = await makeRequestWith402Handling(
556
- `${apiUrl}/catalog/v1/payment_intents/${paymentIntent.id}/confirm`,
572
+ `${apiUrl}/catalog/v1/payment_intents/${paymentIntentId}/confirm`,
557
573
  "POST",
558
574
  {},
559
575
  secretKeyHex,