Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

getApprovalLink is using getLink which is a foreach #766

Closed
ghost opened this issue Jan 16, 2017 · 4 comments
Closed

getApprovalLink is using getLink which is a foreach #766

ghost opened this issue Jan 16, 2017 · 4 comments
Labels

Comments

@ghost
Copy link

ghost commented Jan 16, 2017

Required Information

  • PHP Version: 7
  • PayPal-PHP-SDK Version: latest
  • Debug ID(s):

Issue Description

In the src folder: api/payment.php line: 402 you are using the following:

public function getApprovalLink()
{
    return $this->getLink(PayPalConstants::APPROVAL_URL);
}

In the common/paypalresource.php the method for that call is:

public function getLink($rel)
{
	
    foreach ($this->links as $link) {
        if ($link->getRel() == $rel) {
            return $link->getHref();
        }
    }
    return null;
}

Which causes an error because it's trying to loop through a constant. The error for this is:

ErrorException in PayPalResourceModel.php line 46:
Invalid argument supplied for foreach()

Full stack of the error if that's any help:

in PayPalResourceModel.php line 46
at HandleExceptions->handleError('2', 'Invalid argument supplied for foreach()', 'C:\xampp\htdocs\jobs\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php', '46', array('rel' => 'approval_url')) in PayPalResourceModel.php line 46
at PayPalResourceModel->getLink('approval_url') in Payment.php line 404
at Payment->getApprovalLink() in PaymentController.php line 137
at PaymentController->getPayment('1', 'job')

@jaypatel512
Copy link
Contributor

Hey @filtration !

I think the issue is that I am trying to iterate (foreach) a null object. If $this->links is null, it throws this error. I will go ahead and make the change as soon as possible.

@vannysaini
Copy link

will u pls tell me how to resolve this issues? By deafult which url should be there? please reply me asap

@vannysaini
Copy link

Hello,
my code is:
try{
$payment->create($api);
$hash = md5($payment->getId());
$_SESSION['paypal_hash'] = $hash;

$store=$db->prepare("INSERT INTO transactions_paypal(user_id, payment_id, hash, complete) VALUES (:user_id, :payment_id, :hash, 0)");
$store->execute([
	'user_id' => $_SESSION['user_id'],
	'payment_id' => $payment->getId(),
	'hash' => $hash
]);

} catch (PPConnectionException $e){
header('Location:../paypal/error.php');
}

$allLinks = $payment->getLinks();

if(isset($allLinks) && is_array($allLinks)){

foreach($allLinks as $link){
	if($link->getRel() =='approval_url'){
		$redirectUrl = $link->getHref();
	}
}

header('Location:'.$redirectUrl);

}

if i check object is null , then its fine but it goes into catch section... not coming into try part

@randstraw
Copy link

You should be able to call $payment->getApprovalLink() which will get you the redirect url.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants