OSSPhilippines / paymongo

A lightweight Node.js client for Paymongo API

Home Page:https://paymongo.ossph.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error encountered while attaching to PaymentIntent

AdrianDucao opened this issue · comments

I'm new in using Paymongo API and while i'm trying to attach paymentintent while testing every property has value. but i don't know what i missed, since based from the API documentation in order to Accept card payments: create PaymentIntent, collect the card info, create PaymentMethod then attach to PaymentIntent. what i noticed is that the Docu has client_key along side payment_method, should i add that or? Thanks in advance can't seem to find any materials about paymongo API as a reference.

{
  code: 'parameter_blank',
  detail: 'The value for payment_method cannot be blank.',
  source: { pointer: 'payment_method', attribute: 'payment_method' },
  message: 'The value for payment_method cannot be blank.'
}

Hello! Thank you for submitting an issue.

Hello @AdrianDucao let me get back to you about this the soonest I can. Thanks. For the meantime, can you share your code here? Thanks.

I'm not sure if i'm doing this right this is my first time using nodejs for a project but here's the code

//Payment
router.post('/checkout',(req, res)=>{   

    const { card, month, year, cvc, line1, line2, city, province, country, postal, name, email, phone} = req.body;
    
    mo = parseInt(month);
    yr = parseInt(year);

    //Payment Method
    const createPaymentMethod = async () => {
        return paymongo.paymentMethods.create({
            data: {
              attributes: {
                type: 'card',
                details: {
                  card_number: card,
                  exp_month: mo,
                  exp_year: yr,
                  cvc: cvc,
                }
              }
            }
          },
          
          )
        .then(paymentMethods => {
            return paymentMethods.data.id;
        })
        .catch(err => {
            return err;
        });
    }
    
    const getPaymentMethodId = async () => {
        var id = await createPaymentMethod();
        console.log(id);
    }

    const methodId = getPaymentMethodId();

    //Payment Intent ID
    const createPaymentIntent = async () => {
        return paymongo.paymentIntents.create({
            data: {
              attributes: {
                amount: 10000,
                currency: 'PHP',
                payment_method_allowed: ['card']
              }
            }
          })
        .then(paymentIntents => {
            return paymentIntents.data.id;
        })
        .catch(err => {
            return err;
        });
    }
    
    const getPaymentIntentId = async () => {
        var id = await createPaymentIntent();
        console.log(id);
    }

    const intentId = getPaymentIntentId();

    //Attaching Payment Method to Intent
    const attachPaymentIntent = async () => {
        return paymongo.paymentIntents.attach(intentId,{
            data: {
              attributes: {
                payment_method: methodId
              }
            }
          })
        .then(attachPaymentIntent => {
            return attachPaymentIntent;
        })
        .catch(err => {
            return err;
        });
    }
    
    const getAttachedPaymentIntent = async () => {
        var id = await attachPaymentIntent();
        console.log(id);
    }
    
    getAttachedPaymentIntent();

})

@jofftiquez I think i found a solution, it's just that i'm not familiar with async function and that might have contributed to the issue, code below works but i still get an error.

const attachPaymentIntent = async () => {
        
        const method = await getPaymentMethodId();
        const intent = await getPaymentIntentId();

        return paymongo.paymentIntents.attach(intent,{
            data: {
              attributes: {
                payment_method: method
              }
            }
          })
        .then(attachPaymentIntent => {
            return attachPaymentIntent;
        })
        .catch(err => {
            return err;
        });
    }
    
    const getAttachedPaymentIntent = async () => {
        var id = await attachPaymentIntent();
        console.log(id);
    }

    getAttachedPaymentIntent();

Error

{
  code: 'invalid_payload_format',
  detail: 'One or more required attributes are missing from attributes.',
  message: 'One or more required attributes are missing from attributes.'
}

@jofftiquez I think i was able to figure it out, my bad, it's about me being noob on node kept doing console.log instead of return.

Hello @AdrianDucao I'm glad you figured it out. Sorry for the late reply. If you have more questions don't hesitate to post an issue. Just add a label "Question" :) Cheers.