Sending data using the x-www-form-urlencoded format from a Firebase cloud function

I'm attempting to send an API request to the Stripe API from a cloud firebase function using an HTTP POST method.

The parameters required must be in a format known as 'x-www-form-urlencoded'.

const httpOptions = {
        headers: new Headers({
            Authorization: 'Bearer sk_test_***',
            'Content-Type': 'application/x-www-form-urlencoded'
        })
    };

    const params = 'amount=' + payment_intent.amount + '&currency=' + payment_intent.currency;
    const CHARGE_URL = 'https://api.stripe.com/v1/payment_intents';

    try {
        const snapshot: any = await Http.post(CHARGE_URL, params, httpOptions).toPromise();
        const intent: any = {
            id: snapshot.id,
            client_secret: snapshot.client_secret
        };
        await customerClassService.savePaymentIntent(requestId, intent);
        resp.status(200)
            .send(await Promise.all(intent));
    } catch (e) {
        console.error(e);
        resp.status(400)
            .send('An error occurred and will be resolved as soon as possible.');
    }
    

Unfortunately, I am encountering difficulties with this setup. Can anyone offer assistance?

Answer №1

Successfully implemented the solution by utilizing the request-promise package found at https://www.npmjs.com/package/request-promise

const request = require('request-promise-native');

request.post(uri, {
        headers: {
            "content-type": "application/x-www-form-urlencoded"
        },
        form: {
            "param1": "param1",
            "param2": "param2"
        }
    })
    .catch(err => {
        console.log(err);
    })
    .then(body =>{
        return body;
    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When attempting to run the command 'ng serve', an error occurs stating "Permission denied"

This morning, I encountered a problem. When I try to run the angular project by using the 'ng serve' command, an error saying 'Access is denied' pops up. The same error occurs when running grunt commands as well. Any thoughts on how to ...

Sharing Photos Through Social Media Platforms with Ionic Plugin

When generating a QR code, I would like to share it via a Social Sharing Plugin. Below is the code snippet I am using to achieve this: share() { let currentImage= "data:image/jpeg;base64,"+ this.createdCode; this.socialSharing.share("QR Image to share", ...

Is it possible to alter the color of an SVG icon when it is used in conjunction with <mat-icon> in Angular Material?

Trying to change the color of an svg icon displayed using Angular Material on hover has been a challenge for me. Here's my code snippet: <mat-icon svgIcon="menu"></mat-icon> The icon is visible, but when I attempt to add a class for chan ...

Transmit information using the buttonRenderer feature in Ag-Grid for Angular applications

Struggling to transfer data between two unrelated components by clicking on a cell in ag-Grid and passing the data to a form component. I utilized the buttonRenderer function to extract row data from ag-Grid, but I'm unsure how to pass it to the secon ...

Angular2 Dropdown Menu with Arrow Key Scrolling

When you interact with the Select Market feature in the following plunk, an Observable is filled with data in an array format. The initial item in the list is highlighted in yellow due to a 'selected' property being set. However, there is an issu ...

Issue with Bootstrap Angular dropdown menu malfunctioning within ChildRoute

Recently, I have been working on integrating admin bootstrap html with Angular. As part of this integration, I added CSS and JS files in angular.json as shown below: "styles": [ "src/styles.css", "src/assets/goo ...

NavigatedRoute and Auth-protect - problem retrieving ID from paramMap

Currently working on a website for my exam project, but encountering an issue with the AuthGuard not returning the correct ID in my code. event-details.component.ts getEvent(): void { const id = +this.route.snapshot.paramMap.get('id'); ...

Utilizing Variables in TypeScript to Enhance String Literal Types

Here are my codes: export const LOAD_USERS = 'LOAD_USERS'; export const CREATE_USER = 'CREATE_USER'; export interface ACTION { type: string, payload: any } I am trying to limit the possible values for ACTION.type to either 'L ...

Looking for a way to validate all form fields even when only one field is being used?

In Angular 8, I am facing an issue where the current validation only checks the field being modified. However, there are some fields whose validation depends on the values of other fields. Is there a way to make Angular recheck all fields for validation? ...

Testing Angular applications using Karma

After utilizing the yo angular 1 generator, it generated a landing page and some tests. However, I am facing an issue when trying to compile the code in VS as I receive an error stating that "module('fountainFooter');" is undefined. /// <refe ...

What is the best way to loop through an object while keeping track of its value types

I have a JSON file containing UI adjustments sourced from the API: interface UIAdjustmentsJSON { logoSize: number; themeColor: string; isFullScreen: boolean; } To simplify things, let's utilize a static object: const adjustments: UIAdjust ...

Using Partial function input in TypeScript

I am in need of a function that can accept partial input. The function includes a variable called style, which should only have values of outline or fill, like so: export type TrafficSignalStyle = 'outline' | 'fill' let style: TrafficSi ...

Full compatibility with Angular 2 is now available on Visual Studio 2015 with the added support of

I have some inquiries regarding Visual Studio 2015, Resharper 10, and Angular 2. Is there any syntax highlighting support for HTML markup in TypeScript files in Visual Studio 2015 or Resharper 10? For example, when using a multiline string in a componen ...

Switching between Custom tooltips and default tooltips in Chart.js and Angular

I need to show a tooltip based on certain conditions options: { tooltips: if (tooltipCondition === true) { { mode: 'index', position: 'nearest' } } else { { enabled: false, custom: function (tooltipMo ...

Exploring Angular Route Configurations: Utilizing Multiple Outlets with Path as an Array of

In my Angular9 application, I have configured hundreds of routes paths. Is there a way to use multiple outlets with a single array of string paths? Current Code: const routes: Routes = [ { path: 'Data/:EntityID/:Date', compon ...

having difficulty with installing the bootstrap framework

I'm having issues installing the ng-bootstrap in my project. I keep encountering the following error message. Despite trying to execute the commands below, I haven't had any success. npm install --legacy-peer-deps npm install latest-version He ...

Using Firebase onDisconnect with React Native

I am currently working on a React Native app and I am facing an issue where I need to update the user status to 'offline' in Firebase when the user closes the app. Here is what I have tried: import React, { Component } from 'react' ...

Using TypeScript with Vue: Safely accessing component properties in a type-safe manner

I am currently exploring Vue's setup API and there is one aspect that I am struggling with: I need to retrieve properties of a child component from a parent component. Everything seems to be functioning correctly, but I am facing difficulties with the ...

The mat-dialog component in Angular does not apply the styling of mat-button

I'm facing an issue with my Angular 9 project. Everything seems to be working fine, except when a mat-dialog is opened, the buttons inside it do not have the material-style applied. Strangely, this problem only occurs within the mat-dialog window. On ...

A guide to implementing vue-i18n in Vue class components

Take a look at this code snippet: import Vue from 'vue' import Component from 'vue-class-component' @Component export default class SomeComponent extends Vue { public someText = this.$t('some.key') } An error is being thr ...