Payment process

I got a little confused about how to handle a subscription + payment using Stripe,

Here is what I got:

HTML:

<form id="req" action="/thank-you" method="post">

<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_RrE21sY9Xbtwq9ZvbKPpp0LJ"
data-name="Wizard.Build Hosting Package"
data-description=""
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="usd">
</script>
...

Thank you page:

require_once('lib-stripe/init.php');

// create api key
\Stripe\Stripe::setApiKey("sk_TESTING");

// create customer
$customerO =\Stripe\Customer::create(array(
  "email" => $_POST["e"]
));
$customer = $customerO->jsonSerialize();

//var_dump($customer);

//create subscription (Package Bronze, Silver or Gold)
$subscriptionO = \Stripe\Subscription::create(array(
  "customer" => $customer["id"],
  "items" => array(
    array(
        "plan" => $_POST["pack"],
    ),
  )
));
$subscription = $subscriptionO->jsonSerialize();

//var_dump($subscription);

//create invoice (3rdparty cost)
$invoiceO = \Stripe\Invoice::create(array(
    "customer" => $customer["id"],
    "amount" =>$p3price,
    "currency" => "usd",
    "description" => "3rdp Cost",
    "subscription" => $subscription["id"]
));
$invoice = $invoiceO->jsonSerialize();

//var_dump($invoice);

I clearly lack how the whole process works ...

I will fill in the email field, but how can I request a setup fee + subscription that reappears every month in this pop-up form?

Ideally, the workflow is as follows:

My page with the form: User fills in the name, email address, item name [this metadata is needed], product price, package name [this metadata is needed], package price

, "" , + , , ,

1- + , .

URL- , , .. - , URL-,

, , !

+4
3

, , , , , .

  • , Stripe Stripe API ( PHP )

    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $plan = \Stripe\Plan::create([
      'product' => {'name' => 'Plan name'},
      'nickname' => 'Plan code',
      'interval' => 'month',
      'currency' => 'usd',
      'amount' => 10,
    ]);
    
  • Stripe JS/PHP .. . ( PHP )

    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $customer = \Stripe\Customer::create([
      'email' => 'customer@example.com',
    ]);
    

    json ,

    {
      "id": "<UNIQUE ID>",
      ...
    }
    

  • . ( PHP )

    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $subscription = \Stripe\Subscription::create([
      'customer' => '<UNIQUE ID>',
      'items' => [['plan' => '<PLAN ID>']],
    ]);
    

Stripe

+3

, , . , , , , .

$plan = \Stripe\Plan::create([
  'product' => {'name' => 'Basic Product'},
  'nickname' => 'Basic Monthly',
  'interval' => 'month',
  'currency' => 'usd',
  'amount' => 0,
]); 
+1

API Stripe Charge ( , ..) , ( ),

( ), "", " ". , "" .

-, , "" , , ( , ) .

, (, ), , , , ( , / ..), ( , , api docs)

, , api docs, , .

Dashboard url: https://dashboard.stripe.com/ ( " ", )

API ref: https://stripe.com/docs/api#create_customer https://stripe.com/docs/api#create_charge https://stripe.com/docs/api#create_subscription ( api, )

, NodeJS, php , stripe frontend docs

    stripe.customers.create({
      description: "NR user twitter: " + req.body.twusername + ", job title being paid for: " + req.body.jobRawTitle,
      source: req.body.token,
      email: req.body.email
    }, function(err, customer) {
      if (err) {
          // bad things
          console.log("DEBUG payment charge error: " + JSON.stringify(err));
      } else {
        //update user with given Email
        // Charge the user card:
        return stripe.charges.create({
          amount: 29900, //the last 2 0s are cents
          currency: "usd",
          customer: customer.id,
          description: "Jobs 1 month paid for nextreality job titled:" + req.body.jobRawTitle
        }, function(err, charge) {
          if (err) {
              // bad things
              console.log("DEBUG payment charge error: " + JSON.stringify(err) );
               console.log("charge: "+ charge);
          } else {
              // successful charge
              return stripe.subscriptions.create({
                customer: customer.id,
                items: [
                  {
                    plan: "jobs_monthly",
                  },
                ],
              }, function(err, subscription) {
                // asynchronously called
                console.log("DEBUG payment subscription error: " + err + ' subs:' + JSON.stringify(subscription) );
                return Response(req, res, {});

              });
          }
        });
      }
    });
+1
source

Source: https://habr.com/ru/post/1693109/


All Articles