PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 4, 2022

[FIXED] How to cancel a Paypal Order after user approvation? (API v2)

 July 04, 2022     asp.net, javascript, paypal     No comments   

Issue

I'm actually trying to create an application that use Papal API v2.

I succesfully create the oreder and get money on sandbox. I use JS Paypal method onApprove to call order.capture to get the order and create the subscription on my systems. But if there are some issue on my serverside code, how i can block the order on paypal API to stop get money from user if something going bad?

        paypal.Buttons({
            locale: 'it_IT',
            style: {
                shape: 'pill',
                color: 'blue',
                layout: 'vertical',
                label: 'buynow',
                fundingicons: 'true',
            },
            createOrder: function (data, actions) {
                return actions.order.create(my_PlanOrderDef);
            },
            onApprove: function (data, actions) {
                console.log(data)
                console.log(actions)
                return actions.order.capture().then(function (details) {
                    if (!CloseProcess(details)) {
                        console.log("FAIL! HERE SOMETHING GOING BAD ON MY LOGIC, AND I NEED TO CANCEL THE ORDER PAYMENT");
                        return false;
                    }
                });
            },
            onError: function (err) {
                console.log(err);
            }
        }).render('#btnPaypalContainer');

CloseProcess method just call my serverside logic to create a subscription on my system. I probably miss something in paypal v2 logics so apologize me, any idea to accomplish this task?

Thank You


Solution

When an order is created, it is valid for 72 hours.

When an order becomes approved, it is valid for 3 hours.

There is no method to cancel an order.

If you don't wish to use an order, discard its id and do not capture it.


To change to a server-side integration for order creation and capture, follow the Set up standard payments guide and make 2 routes on your server, one for 'Create Order' and one for 'Capture Order', documented here. Both routes should return only JSON data (no HTML or text). Inside the 2nd route, when the capture API is successful you should store its resulting payment details in your database (particularly purchase_units[0].payments.captures[0].id, which is the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) immediately before forwarding your return JSON to the frontend caller.

Pair those 2 routes with the frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server



Answered By - Preston PHX
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing