PayPal API transaction refused on refund - php

This code does not work:
$sale = $this->get_sale($saleId);
$amt = $sale->getAmount();
$refund = new Refund();
$refund->setAmount($amt);
$refundSale = new Sale();
$refundSale->setId($saleId);
$refundedSale = $refundSale->refund($refund, $this->apiContext);
I tried not providing an Amount object to initiate a full refund, per https://developer.paypal.com/docs/integration/direct/refund-payment/.
Yet this doesn't work either:
$sale = $this->get_sale($saleId);
$refund = new Refund();
$refundSale = new Sale();
$refundSale->setId($saleId);
$refundedSale = $refundSale->refund($refund, $this->apiContext);
Can anyone determine what I'm doing wrong? The API response doesn't give a lot of detail as to what is going on, just error code 400 and "The request was refused.{0}".

Based on the debug_id, the transaction has already been fully refunded, so you get such error message. You can create a new transaction, then try to refund again for testing.

Related

Paypal Invoice after Payment

I'm a slight bit confused about the Paypal API. I'm using Paypal Payments to have a customer pay on the spot with the PHP SDK.
$apiContext = new ApiContext(
new OAuthTokenCredential(
$this->paypalid,
$this->paypalsecret
)
);
$payer = $_GET['payerId'];
$paymentid = $_GET['paymentId'];
$payment = Payment::get($paymentid, $apiContext);
$transaction = $payment->getTransactions();
$execution = new PaymentExecution();
$execution->setPayerId($payer)
->setTransactions($transaction);
try{
$result = $payment->execute($execution, $apiContext);
} catch (Exception $ex) {
echo "Couldn't process payment";
exit;
}
I would like to generate an invoice based on the payment done for tax reasons. Currently all this does is send a notification to both the seller/buyer concerning the transaction being successful. Paypal's API seems to heavily imply that invoices are used to process payments, which in this case, the payment is already done; I don't want the customer to pay twice.
I imagine that I need to set the status property on an Invoice object to "PAID" and set the amount paid equal to the total amount present on the payment, and then use the send method along with the apiContext.
Am I wrong?

Searching for Paypal records by paymentId at Paypal account

I have a site that uses the Paypal Rest API SDK and successfully capture the paymentId after the transaction.
I capture it from the return_url's $_SERVER['QUERY_STRING'] using PHP.
I was hoping my Client would be able to then go into their Paypal account and search for records using these PaymentID's. It doesn't seem the case. There is a TransactionID but it does not match the PaymentID.
Any idea how to either:
Search for the record in a Paypal Account using the PaymentID (using the Paypal Account interface record tools - not api programming)
Grab the transactionID after the transaction (the return URL only gives me PaymentID, Token, and PayerID.)
I've realized that I will need to take the PaymentID and use the SDK to retrieve more detailed payment information.
$apiContext = $this->getApiContext();
$payment = new Payment();
$payment = $payment->get( $paymentId, $apiContext );
$execution = new PaymentExecution();
$execution->setPayerId( $_GET['PayerID'] );
// ### Retrieve payment
// Retrieve the payment object by calling the
// static `get` method
// on the Payment class by passing a valid
// Payment ID
// (See bootstrap.php for more on `ApiContext`)
try {
$payment->execute( $execution, $apiContext );
if ( $payment ) {
$obj = json_decode( $payment );
//GET SOME STUFF
$paypal_payment_id = $obj->{'id'};
$status = $obj->{'state'};
//DO SOMETHING WITH IT
}
} catch ( Exception $ex ) {
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
//ResultPrinter::printError("Get Payment", "Payment", null, null, $ex);
log_message( 'error', 'error=' . $ex );
//exit(1);
}

PayPal first redirect crash, second one redirect the user successfully

these days, I implemented the PayPal-Account-Payment Payflow into my checkout. At this point everything works perfect. But sometimes I get an error when I try to redirect my customers to complete the order on paypal.com. When I refresh the page and submit all data again it works.
Here is my current PHP-Code:
$sdkConfig = array(
"mode" => "LIVE"
);
$cred = new \PayPal\Auth\OAuthTokenCredential(<**>, <**>, $sdkConfig);
$apiContext = new \PayPal\Rest\ApiContext($cred, 'Request'.time());
$apiContext->setConfig($sdkConfig);
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod("paypal");
$amount = new \PayPal\Api\Amount();
$amount->setCurrency("EUR");
$amount->setTotal($this->total);
$transaction = new \PayPal\Api\Transaction();
$transaction->setDescription("Complete your order.");
$transaction->setAmount($this->amount);
$transaction->setItemList($this->itemList);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("https://example.com/order/success/");
$redirectUrls->setCancelUrl("https://example.com/order/cancel/");
$payment = new \PayPal\Api\Payment();
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$payment->create($apiContext);
$redirectUrl = $payment->links[1]["href"];
header("Location: ".$redirectUrl);
The error I receive sometimes at the first call:
Fatal error: Cannot use object of type PayPal\Api\Links as array in /htdocs/lib/Checkout.php on line 503
The paypal sample code does something different to get the url form the payment object:
$payment->create($apiContext);
$approvalUrl = $payment->getApprovalLink();
Guess your error has something todo with your direct access to payment->props.

PayPal REST API through PHP SDK return "Incoming JSON request does not map to API request"

I'm trying to create and execute a payment with PayPal REST API through PHP SDK (sandbox environment) like showing below. The payment creation ($payment->create) work fine but the payment execution ($payment->execute) return "Incoming JSON request does not map to API request".
The JSON request is created by the SDK, then what can be the problem?
Thanks in advance.
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item = new Item();
$item->setName('Any name')
->setCurrency('EUR')
->setQuantity(1)
->setPrice(0.99);
$itemList = new ItemList();
$itemList->setItems(array($item));
$details = new Details();
$details->setTax(0)
->setSubtotal(0.99);
$amount = new Amount();
$amount->setCurrency('EUR')
->setTotal(0.99)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription('Any description')
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(BASE_URL.'/payment/?success=true')
->setCancelUrl(BASE_URL.'/payment/?success=false');
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
try {
$payment->create($apiContext);
$execution = new PaymentExecution();
$result = $payment->execute($execution, $apiContext);
} catch (Exception $ex) {
//Function for extract the error message,
//the error message can be showing with
//a simple var_dump($ex)
$exception = self::getException($ex);
}
Am not a PHP dev (.Net), so based on reading the above, check into this section in your code:
$execution = new PaymentExecution();
$result = $payment->execute($execution, $apiContext);
You are sending a new PaymentExecution without the payer_id and the Payment.Id which you would obtain after the user approves your paypal Payment request that you created.
That said, I don't see that part (though as above, not a PHP dev) so I could be wrong. The steps are:
Create the Payment
Go into the Approval Flow -> user is redirected to Paypal and approves the Payment you created in #1 -> and is redirected back to your site (the returnUrl)
a. the PayerID will be in the querystring in this process
b. the paymentId will also be in the querystring in this process
After the user is redirected back to your site from PayPal (your returnUrl) - Execute a new Payment, set it's id to the one you obtained (#2b), sending the PaymentExecution with its PayerID set to the what you you obtained (#2a)
In c# (you'll need to convert this to PHP):
var _payment = new Payment { id = the_payment_id_you_obtained };
var _payExec = new PaymentExecution { payer_id = the_payer_id_you_obtained };
var _response = _payment.Execute(context, _payExec);
Hth...
It was enough put
$payment->setIntent('authorize')
instead of
$payment->setIntent('sale')
and eliminate the execution
$execution = new PaymentExecution(); $result = $payment->execute($execution, $apiContext);
Then, after
$payment->create
I used the "href" from
$payment->links
to do the redirect. Everything went perfectly. Thank you all.

paypal php rest api. response codes?

This whole paypal process has been one nightmare after the next. I've managed to complete the application all except for one small problem. the response object from paypal Payment::execute() has no documentation. My front end is client side, so i really have no way of knowing what this response array contains.
here's what i've got so far:
$paymentId = $this->ppconf; //stores paymentId
if(empty($paymentId)){Throw New \Exception('missing payment id'); }
$payment = Payment::get($paymentId, $apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($z);
$response = $payment->execute($execution, $apiContext); // i need to know what this response array looks like and what the response codes are, so i can generate my $result array for my application.
$state = $response->getState();
$failures = ['failed','canceled','expired'];
if(in_array($state,$failures)){
$result = ['type'=>'error'];
}else{
$result = ['type'=>'success'];
$this->finished = true;
}
return $result;
As per the docs: https://developer.paypal.com/webapps/developer/docs/api/#execute-an-approved-paypal-payment
Returns a payment object for the completed PayPal payment.
So presumably an instance of an PayPal\Api\Payment Object.
You should be able to grab the state property of the payment using the following:
$response->getState();
Returning one of the following strings:
created, approved, failed, canceled, expired

Categories