How can i create a subscription plan on a connecte

2019-06-03 18:11发布

问题:

I am trying to create a subscription plan on a stripe connect managed account. I tried the following code:

Parse.Cloud.define("createSubscription", function (request, response) {
 Parse.Cloud.httpRequest({
    method:"POST",
    url: "https://" + "sk_test_****************" + ':@' + "api.stripe.com/v1" + "/accounts/" + 'acct_**********' + "/plans/",  
    headers: {
    'Authorization': 'Basic ********************'
  },
  body: {
    'amount': 2000,
    'interval': 'month',
    'name': 'JPGB Plan',
    'currency': 'usd',
    'id':'first Plan',
  },
        success: function(httpResponse) {
        response.success(httpResponse.text);
        },
        error: function(httpResponse) {
        response.error('Request failed with response code' + httpResponse.status);
        }
    });                 
});

But this failed with a 404 (the requested resource doesn't exist) error.

This is how I did it.

  Parse.Cloud.define("createAccountPlan", function (request, response) {
 Parse.Cloud.httpRequest({
    method:"POST",
    url: "https://" + "sk_test_****************" + ':@' + "api.stripe.com/v1/plans",  
    headers: {
    'Stripe-Account': request.params.accountId
    },
    body: {
        'amount': request.params.amount,
        'interval': 'day',
        'interval_count':request.params.intervalCount,
        'name': request.params.planName,
        'currency': 'usd',
        'id':request.params.planId,
    },
    success: function(httpResponse) {
    response.success(httpResponse.text);
    },
    error: function(httpResponse) {
    response.error('Request failed with response code' + httpResponse.status);
    }
});                 
});

回答1:

What i think you should do is not to execute a direct http request to stripe REST API but to use strip node-js SDK that will do it and more for you.

In order to achieve it in parse server you need to do the following steps:

  1. inside your parse-server project enter the following command
npm install stripe

this will install stripe into your parse-server project

  1. In your cloud code require the stripe node SDK

var stripe = require('stripe')(' your stripe API key ');

  1. Call to the create subscription function which available under the stripe object that you required

    stripe.subscriptions.create({
      customer: "{YOUR_CUSTOMER_ID}",
      plan: "{PLAN_IDENTIFIER}"
    }, function(err, subscription) {
        // asynchronously called
      }
    );

Then if you need additional service call to stripe you can do it in the same way.

All the services that you can run with stripe can be found in here

It's always better to use SDK's when you can because SDK's make your life much more easy, are handling all the things for you behind the scenes and usually are maintained by the company who provided them (in this case its stripe)



回答2:

From the Stripe connect docs:

Authentication via the Stripe-Account header

The first, preferred, authentication option is to use your—the platform account’s—secret key and pass a Stripe-Account header identifying the connected account for which the request is being made.

(demo showing making a customer)

All of Stripe’s libraries support this style of authentication on a per-request basis

Stripe docs are a little subtle here, but this means you can use the same technique to make a subscription for a customer on a connected account. You can also use it to make the products and plans for that connected account too. and anything else you want to do on behalf of the connected customer:

(async function(){
  let subscription = await stripe.subscriptions.create({
    customer: "someCustomerID",
    plan: "planID"
  },{
    stripe_account: "connectedStripeAccountID"
  });
})();