How do I send a refund request? - php

Suppose we have PayPal sandbox account, with created Application for REST API calls.
In our application we use PayPal Pay Buttons, to redirect customer to PayPal site. Customer pays and returning back to site, where we checking status and setting external reference to transaction.
For this operations we're using not Application's credentials, but accounts - email, password and username. After this we're accepting IPN messages from PayPal to track status, using the same credentials.
Then we have a button Refund, pressing this button will make a request to REST API to PayPal with Application's access information (clientId and clientSecret). This part built on top of PayPal PHP SDK.
When I try to execute a call - PayPal returning 404 error. I suppose the payment is not visible to Application.
Better vision of what credentials and where are used:
PayPal account:
Live:
email
password
username
Test:
email-test
password-test
username-test
Application:
Live:
clientId
clientSecret
Test:
clientIdTest
clientSecretTest
For button I use: email-test, username-test, password-test
Then I get $transactionId. And using it in code:
$api = new ApiContext(new OAuthTokenCredential($clientIdTest, $clientSecretTest));
$ppAmount = new Amount();
$ppAmount->setCurrency('USD')
->setTotal(33.15);
$ppRefund = new Refund();
$ppRefund->setAmount($ppAmount);
$ppSale = new Sale();
$ppSale->setId($transactionId);
$ppSale->refund($ppRefund, $apiContenxt);
And I get 404. As you can see I use test credentials for buttons and for REST call, but for some reason getting 404.
Thanks in advance.

As far as I know only payments made via REST API can be refunded using the REST API, meaning if you created the payment via a button or a different API you cannot use REST refund.
REST refund expects a sale_id (which is generated when you "execute" a REST payment) in the Sale entity.

Related

PayPal Webhook Notification Billing Subscription PHP

I've implemented PayPal Billing subscription using PayPal-PHP-SDK, everything is working fine as expected, the user can cancel the subscription from the application with this code.
$agreementStateDescriptor = new AgreementStateDescriptor();
$agreementStateDescriptor->setNote("Suspending the Agreement.");
$agreement->suspend($agreementStateDescriptor, $apiContext);
But if the user cancel the subscription from his PayPal account, my application will not be notified for it, how it can be addressed? is there any webhook as Stripe or something.
https://github.com/paypal/PayPal-PHP-SDK/
Thanks
you need to create a Paypal webhook for your app.
Set an URL (accessible URL) i.e. yourdomain/paypal/resolver
then register event BILLING.SUBSCRIPTION.CANCELLED.
Once the user cancel the agreement from Paypal, the webhook for BILLING.SUBSCRIPTION.CANCELLED event is activated and send a request to your site.
FYI, the samples provided from this link http://paypal.github.io/PayPal-PHP-SDK/sample/ are pretty much useful

When is the best time to create a PayPal Webhook?

I am using the PayPal API to create a Webhook, and not the Developer site. When is the best time to do this? Do I create the webhook before the payment is created, after the payment is created, or after the payment is executed? According to the API documentation, the webhook needs to be created only once in the lifetime of the app. What happens if a new webhook is created each time the app is used?
What I have:
processPayment: the payment details are defined, the payment is created, the user is redirected to PayPal.
executePayment: the user is returned from PayPal, the payment is executed.
On notify:
verifyPayment: calls a function to validate Webhook signature.
Post-processing in the Site's database.
P.S. Where/how is the webhook Id generated?
Webhook needs to be created before making a payment transaction if you want to get notified of the payment. If there are multiple Webhooks configured on your app and if you have same set of events subscribed on both, you would receive notifications on both Webhooks.
Thanks!

Magento 2: How to change Payment Action Method on the fly in Paypal Express

I have already created Paypal/Developer/Sandbox Account. Already got API Credentials.
Downloaded PHP SDK http://paypal.github.io/PayPal-PHP-SDK/
Referring: https://developer.paypal.com/docs/api/
https://devtools-paypal.com/guide/expresscheckout/php?interactive=ON&env=sandbox
I have already done Sandbox One Transaction with Authorize Method using my PHP Script.
What i have to achieve. I need to get Authorize Payments, Convert "Authorize" payments to "Capture/Sale" Method & Notify Customer & Admin As well.
How to get Authorization Id?
By referring above document i need to use 3 Web Services
Get details of an authorized payment
Capture an authorized payment
Get details of a captured payment
Already have Transaction ID of "Authorize" Payment.
Please provide the steps to follow & if any example already available then please share
Need to integrate this functionality in Magento 2
[UPDATE]
I have changed my API Sandbox Credentials in PHP SDK in PayPal-PHP-SDK-master\sample\bootstrap.php file.
From sample/payments/AuthorizePayment.php i will get Authorized Payment with ID: PAY-0TV39791XXX02404VK7XD32I & it works fine.
When i input my Transaction Id it gives
{"name":"INVALID_RESOURCE_ID","message":"Requested resource ID was not
found.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#INVALID_RESOURCE_ID","debug_id":"1f2b08ce88633"}
My Sandbox Transaction Id is 3AA32794KF48247AA. So question is how to convert your Transaction Id in to Payment Id?
https://magento.stackexchange.com/questions/138579/magento-2-change-paypal-action-method-on-the-fly
Below is the reply from Paypal
Transaction id can be generated after REST API execute payment,
Classic API [NVP/SOAP] DoEC API or normal button payment completion.
It will never work to reverse the search to find the Payment Id.
If you have paymentId, you can get details of authorized payment in this way:
$payment = Payment::get( $paymentId, $apiContext );
For all propeties of payment class take a look this - Payment.php
To change Sandbox/API Credentials edit the bootstrap.php in your project.
UPDATE
You don't need to use AuthorizePayment.php. Istead, do the following:
Create payment and get the url for authorization (your website side).
Print the url for authorization (your website side).
Cutomer follow the authorization url and go to PayPal and authorize the payment (PayPal side).
PayPal returns customer to your website with the paymentId (PayPal side).
Grab the paymentId and execute the payment (your website side).

paypal webhook notification not received - sandbox mode

I'm trying to get notified via webhooks when a simple payment is made, however I don't receive any request from paypal on the specified URL.
Here is what I've done:
I have 2 sandbox accounts: Facilitator and Buyer
Create an app (sanbox mode) for the facilitator account
Add a webhook URL in that app
Login into sanbox.paypal.com with the sandbox facilitator username and password
Generate a buy now button with a test product
Paste the button code on my page and make a purchase with the sanbox Buyer account
In developer dashboard the payment is received if I go to Sandbox -> Notifications: "Notification of Payment Received from test buyer", but the webhook page on my server is not called. I know this because the php script creates a txt file every time the page is called.
Also if I go to Developer Account -> Webhook Notifications and select the app I have this message: You don't have any events for the selected application
NOTE: If I add the URL in the webhook simulator everything works great.
Am I doing something wrong? Do I have to connect the app with the buy now button?
Thanks
Webhooks is only used for REST payments. However for Paypal buttons created in PP account, you need to use Paypal IPN. Here is the guide for IPN and IPN script for your reference.

A way to verify PayPal Account Status is Verfied using PHP

Basically I want my users to associate PayPal account in their accounts for identify verification purposes. I am developing a marketplace site and wanted to integrate paypal as my identity verification procedure.
Is there a way to verify email address, first name and last name using PHP like using $_GET variables or $_POST variables? If there is can you show me how?
I tried search and I get Adaptive Accounts GetVerifiedStatus API. But that totally new to me, I don't know where and how to start.
Your best bet here is to make them start, but not complete, an Express Checkout. A normal Express Checkout (referencing Classic but REST works the same way) looks like this
SetExpressCheckout - Tell PayPal you want to authorize $1.00 and have them log in and authorize the payment
GetExpressCheckoutDetails - Take the token from step 1 and get their PayPal account details. Included in this is PAYERSTATUS, which will tell you if their PayPal account is verified or not
DoExpressCheckout - You won't do this, but this is where you finish the process and get funds. Since the data you want is in Step 2, just ignore it and the session will expire.
You could implement GetVerifiedStatus API to determine whether the specified Paypal account's status is verified or unverified.
To use that API, you need to submit the application request to Paypal for approval first. Submission portal link here.
Then use the GetVerifiedStatus API call directly, refer to API request fields,link.
Sample SDK download link.

Categories