I know there are qiestions asked on this topic already, but i could not find a solution. I am absolutely new at paypal api and my task is to create a function to cancel billing agreement.
Do not even know how to form and make function call api. Tried to find and example, but everywhere i end up viewing technical reference page which does not help me as newbie...
Anyone could draft and example please?
So far i got to this stage, but still won't work.
if(!empty($agreement->paypal_agreement_id) && $agreement->paypal_agreement_state == 'Active')
{
/*
* Cancel a billing agreement
*/
$apiContext = new ApiContext(new OAuthTokenCredential($this->clientid, $this->clientsecret));
$createdAgreement = $agreement->paypal_agreement_id;
$patch = new Patch();
$patch->setOp('replace')
->setPath('/')
->setValue(json_decode('{
"billingagreementstatus": "Canceled"
}'));
$patchRequest = new PatchRequest();
$patchRequest->addPatch($patch);
try {
// Uzkomentinta testatimui - netrinti. Mindaugas
$createdAgreement->update($patchRequest, $apiContext);
$agreement = Agreement::get($createdAgreement, $apiContext);
} catch (Exception $ex) {
ResultPrinter::printError("Updated the Agreement with new Description and Updated Shipping Address", "Agreement", null, $patchRequest, $ex);
exit(1);
}
} else {
return ['.reportmsg' => 'Payment agreements do not exist.'];
}
Anyone can help?
Related
I have two questions, first about the error, Laravel 8.20.1 + Paypal SDK v1.
App Routes:
Route::get('/agreement/execute', [PaymentController::class, 'agreementExecute']);
Route::post('/agreement/create', [PaymentController::class, 'subscribeMonthly']);
Admin Routes:
Route::prefix('paypal')->name('plan.')->group(function () {
Route::get('/monthly/create', [PrivatePaypal::class, 'createMonthly'])
Route::get('/list', [PrivatePaypal::class, 'showPlans'])
Route::get('/plan/{plan}', [PrivatePaypal::class, 'plan'])
Route::get('/delete', [PrivatePaypal::class, 'deletePlan'])
});
Created plan:
Created plan image
Plan code:
public function createMonthly() {
$plan = new \PayPal\Api\Plan();
$plan->setName('Monthly')
->setDescription('Activate partnership for one month.')
->setType('INFINITE'); // or FIXED: The plan has a fixed number of payment cycles
$paymentDefinition = new \PayPal\Api\PaymentDefinition();
$paymentDefinition->setName('Monthly Payments')
->setType('REGULAR') // or TRIAL
->setFrequency('Month') // or WEEK, DAY, YEAR, MONTH
->setFrequencyInterval("1") // The interval at which the customer is charged. Value cannot be greater than 12 months
->setAmount(new \PayPal\Api\Currency(array('value' => 20, 'currency' => 'USD')));
// ->setCycles("12")
$merchantPreferences = new \PayPal\Api\MerchantPreferences();
$merchantPreferences->setReturnUrl(route('account.agreement',['success'=>'true']))
->setCancelUrl(route('account.agreement',['success'=>'false']))
->setAutoBillAmount("yes")
->setInitialFailAmountAction("CONTINUE")
->setMaxFailAttempts("0")
->setSetupFee(new \PayPal\Api\Currency(array('value' => config('settings.price_monthly'), 'currency' => 'USD')))
;
$plan->setPaymentDefinitions(array($paymentDefinition));
$plan->setMerchantPreferences($merchantPreferences);
try {
$createdPlan = $plan->create($this->apiContext);
} catch(\Exception $ex) {
print_r($ex->getMessage());
die();
}
// dd($createdPlan);
$this->activatePlan($createdPlan);
}
After I send data with the form
<form action="https://site.test/agreement/create" method="post">
<input type="hidden" name="_token" value="XSM2gxx0Cqs5dlloYScQfl2GdeGqrz4lkWLfm42a">
<input type="hidden" name="_method" value="POST">
<input type="hidden" name="id" value="P-0UV961714R317531UT5H72WI">
<button class="button compact">Activate</button>
</form>
After succesful redirect to paypal with all data, i click accept (sandbox) and after that i get succesfull rerict back, redirect functions:
if (!empty($request->input('success')))
{
$success = $request->input('success');
if ($success && !empty($request->input('token')))
{
$token = $request->input('token');
$agreement = new \PayPal\Api\Agreement();
try {
// Execute agreement
$agreement->execute($token, $this->apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
print_r($ex->getMessage());
echo $ex->getCode();
echo $ex->getData();
print_r($ex->getData());
die($ex);
} catch (Exception $ex) {
// die($ex);
}
And when $agreement->execute runs I get errors with no detailed information.
Form subscribe function:
public function subscribeMonthly(Request $request) {
$id = $request->id;
$agreement = new \PayPal\Api\Agreement();
$agreement->setName('Monthly subscription')
->setDescription('Activate partnership for one month.')
->setStartDate(Carbon::now()->addMonth()->toIso8601String());
$plan = new \PayPal\Api\Plan();
$plan->setId($id);
$agreement->setPlan($plan);
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
try {
$agreement = $agreement->create($this->apiContext);
$approvalUrl = $agreement->getApprovalLink();
} catch(\Exception $ex) {
print_r($ex->getMessage());
die();
}
return redirect($approvalUrl);
}
Error is : PayPal\Exception\PayPalConnectionException
Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-1LE052463N345662M/agreement-execute.
https://am.test/account/agreement?ba_token=BA-9X735270PX851462W&success=true&token=EC-1LE052463N345662M
I reviewed a lot of tutorials, re-read the code in PayPal guides several times. I'm new to this and can't figure out what the reason is, it just doesn't work for me. I do everything one to one.
No subscriptions are created in the buyer account or by the administrator, everything is empty.
And the second question, Paypal writes that v1 is deprecated. How can I use the second version with a checkout v2 for subscriptions and where can I find detailed guides with the Laravel about this question.
It's hard for me to fully understand API and code not created by myself, I create a big project on my own, but stuck a few days with that PayPal error. Thank you for reading so much of what I have written, I hope for your support.
The PayPal-PHP-SDK is deprecated, and not compatible with the current version of PayPal Subscriptions. You should not be using it for anything.
Instead, integrate directly with the necessary Product, Plan, and subscription management API calls described in that Subscriptions documentation. (Plus webhooks, if desired--to be notified of future subscription events)
You can also manage Products and Plans manually in the receiver account:
Sandbox: https://www.sandbox.paypal.com/billing/plans
Live: https://www.paypal.com/billing/plans
I'm building paypal subscription system and i'm using web-hooks for notifying system on subscription created, active, etc.
However, it's required that user must return to success url and site execute $agreement->execute($token, $apiContext)); to make it work.
Let's say for some reason user never get back to return url then you will never execute payment and user will never get their subscription.
I've looked around on paypal documentation and couldn't find any solution.
Here's my code:
Subscribe.php:
$agreement = new Agreement();
$agreement->setName('Basic Plan')
->setDescription('Some info')
->setStartDate($date);
$plan = new Plan();
$plan->setId('PLAN_ID');
$agreement->setPlan($plan);
// Add Payer
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
// Add Shipping Address
$shippingAddress = new ShippingAddress();
$shippingAddress->setLine1('111 First Street')
->setCity('Saratoga')
->setState('CA')
->setPostalCode('95070')
->setCountryCode('US');
$agreement->setShippingAddress($shippingAddress);
// ### Create Agreement
try {
$agreement = $agreement->create($apiContext);
$agreement->getApprovalLink()
// method
$approvalUrl = $agreement->getApprovalLink();
redirect($approvalUrl);
} catch (Exception $ex) {
print_r($ex->getData());
}
index.php
if (isset($_GET['status']) && $_GET['status'] == 'success') {
$token = $_GET['token'];
$agreement = new \PayPal\Api\Agreement();
try {
// ## Execute Agreement
// Execute the agreement by passing in the token
echo "<pre>";
print_r($agreement->execute($token, $apiContext));
} catch (Exception $ex) {
exit(1);
}
} else {
echo "User Cancelled the Approval";
}
I have an issue with Paypal REST Api billing (recurring payments).
I did exactly as in paypal documentation but when I click payment button it opens blank page (no errors in the error log).
Here is what I do:
Front End Button
<form action="WEBSITE/subscribe/paypal/paypal_agreement.php" method="POST">
<button type="submit" style="margin-top:10px;border: 0; background: transparent">
<img src="WEBSITE/wp-content/uploads/2017/05/paypal.png" style = "width:170px;" alt="submit" />
</button>
</form>
paypal_agreement.php (copy/paste from paypal docs)
<?php
require_once("../../paypal/vendor/autoload.php");
$createdPlan = require 'paypal_createplan.php';
use PayPal\Api\Agreement;
use PayPal\Api\Payer;
use PayPal\Api\Plan;
use PayPal\Api\ShippingAddress;
$ppstartdate = date('c', time()+210);
$agreement = new Agreement();
$agreement->setName('Title Agreement')
->setDescription('Description Agreement')
->setStartDate($ppstartdate);
// Add Plan ID
// Please note that the plan Id should be only set in this case.
$plan = new Plan();
$plan->setId($createdPlan->getId());
$agreement->setPlan($plan);
// Add Payer
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
// ### Create Agreement
try {
// Please note that as the agreement has not yet activated, we wont be receiving the ID just yet.
$agreement = $agreement->create($apiContext);
// ### Get redirect url
// The API response provides the url that you must redirect
// the buyer to. Retrieve the url from the $agreement->getApprovalLink()
// method
$approvalUrl = $agreement->getApprovalLink();
header("Location: ".$approvalUrl);
} catch (Exception $ex) {
exit(1);
}
return $agreement;
?>
paypal_createplan.php (copy/paste from paypal docs)
<?php
require_once("../../paypal/vendor/autoload.php");
use PayPal\Api\Currency;
use PayPal\Api\MerchantPreferences;
use PayPal\Api\PaymentDefinition;
use PayPal\Api\Plan;
// Create a new instance of Plan object
$plan = new Plan();
// # Basic Information
// Fill up the basic information that is required for the plan
$plan->setName('Title Plan')
->setDescription('Description Subscription Plan')
->setType('fixed');
// # Payment definitions for this billing plan.
$paymentDefinition = new PaymentDefinition();
// The possible values for such setters are mentioned in the setter method documentation.
// Just open the class file. e.g. lib/PayPal/Api/PaymentDefinition.php and look for setFrequency method.
// You should be able to see the acceptable values in the comments.
$paymentDefinition->setName('Regular Payments')
->setType('REGULAR')
->setFrequency('Month')
->setFrequencyInterval("1")
->setCycles("6")
->setAmount(new Currency(array('value' => 94.99, 'currency' => 'USD')));
$merchantPreferences = new MerchantPreferences();
$baseUrl = "https://websitelink.com";
$merchantPreferences->setReturnUrl("$baseUrl/subscribe/paypal/execute.php?success=true")
->setCancelUrl("$baseUrl/subscribe/paypal/execute.php?success=false")
->setAutoBillAmount("yes")
->setInitialFailAmountAction("CONTINUE")
->setMaxFailAttempts("0")
->setSetupFee(new Currency(array('value' => 0, 'currency' => 'USD')));
// ### Create Plan
try {
$output = $plan->create($apiContext);
} catch (Exception $ex) {
exit(1);
}
return $output;
?>
execute.php (copy/paste from paypal docs)
<?php
// #Execute Agreement
// This is the second part of CreateAgreement Sample.
// Use this call to execute an agreement after the buyer approves it
require_once("../../paypal/vendor/autoload.php");
// ## Approval Status
// Determine if the user accepted or denied the request
if (isset($_GET['success']) && $_GET['success'] == 'true') {
$token = $_GET['token'];
$agreement = new \PayPal\Api\Agreement();
try {
// ## Execute Agreement
// Execute the agreement by passing in the token
$agreement->execute($token, $apiContext);
} catch (Exception $ex) {
exit(1);
}
// ## Get Agreement
// Make a get call to retrieve the executed agreement details
try {
$agreement = \PayPal\Api\Agreement::get($agreement->getId(), $apiContext);
//done
header('Location: https://websitelink.com/subscribe/subscribe.php');
} catch (Exception $ex) {
exit(1);
}
} else {
$_SESSION['pmsg'] = $_SESSION['pmsg'].'<h2>Subscription Failed</h2>';
}
But I couldn't find anything about authentication OAuth2 (as for example with express checkout), nothing in documentation. Maybe it doesn't work because of that?
Variable $apiContext should contain paypal app authentication & setConfig
Is this: "Variable $apiContext should contain paypal app authentication & setConfig"
the solution to this issue?: I have an issue with Paypal REST Api billing (recurring payments). I did exactly as in paypal documentation but when I click payment button it opens blank page (no errors in the error log).
I am having a similiar issue. Pls confirm. And how do we get the paypal app authentication & setConfig?
Reference
Code
public function ShowPaymentWithPaypal()
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item_1 = new Item();
$item_1->setName('Item 1') /** item name **/
->setCurrency('USD')
->setQuantity(1)
->setPrice(2); /** unit price **/
$item_list = new ItemList();
$item_list->setItems(array($item_1));
$amount = new Amount();
$amount->setCurrency('USD')
->setTotal(2);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Your transaction description');
$redirect_urls = new RedirectUrls();
$redirect_urls->setReturnUrl(\URL::route('ReturnedFromPaypal')) /** Specify return URL **/
->setCancelUrl(\URL::route('CancelledPaymentWithPaypal'));
$payment = new Payment();
$payment->setIntent('Sale')
->setPayer($payer)
->setRedirectUrls($redirect_urls)
->setTransactions(array($transaction));
/** dd($payment->create($this->_api_context));exit; **/
try {
$payment->create($this->_api_context);
} catch (\PayPal\Exception\PPConnectionException $ex) {
dd($ex);
if (\Config::get('app.debug')) {
\Session::put('error','Connection timeout');
return "Error occured";
/** echo "Exception: " . $ex->getMessage() . PHP_EOL; **/
/** $err_data = json_decode($ex->getData(), true); **/
/** exit; **/
} else {
\Session::put('error','Some error occur, sorry for inconvenient');
return "Error occured";
/** die('Some error occur, sorry for inconvenient'); **/
}
}
foreach($payment->getLinks() as $link) {
if($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
/** add payment ID to session **/
\Session::put('paypal_payment_id', $payment->getId());
if(isset($redirect_url)) {
/** redirect to paypal **/
return \Redirect::away($redirect_url);
}
\Session::put('error','Unknown error occurred');
return "Last line error";
}
What's the problem ?
When I try to do the login for doing payment using sandbox credentials, I get the below error.
We aren't able to process your payment using your PayPal account at
this time. Please go back to the merchant and try using a different
payment method.
I am following this to configure Paypal in Laravel 5.5
XHR Error Details
Am I missing something ?
It looks like a Paypal limitation.
In this link, for example, the support say that Russia laws limit paypal account to 100000 rub of transaction.
When you reach the limit, you must specify something on your activities to unlock the limit.
Maybe have you reached the limit of transactions' amount for your country?
Try to use low amount, as 0.01 and to delete old test transactions from the sandbox account (seller).
If you haven't a lot of transactions for the seller, maybe the problem is in the customer's account? Try to create a new account and use it.
I suppose in the sandbox will be a place to unlock the
I am using the PayPal SDK for PHP, I am trying to cancel an invoice, the result returned is "true", there isn't exception returned, but the invoice is not canceled. Please could you tell me if there is an error in my code?
$Invoice = new Invoice();
try {
$invoice = $Invoice->get($id_invoice, $apiContext);
$notify = new CancelNotification();
$notify->setSubject("Past due")
->setNote("Canceling invoice")
->setSendToMerchant(true)
->setSendToPayer(true);
$result = $Invoice->cancel($notify, $apiContext);
} catch (Exception $ex) {
$result = self::getException($ex);
}
return $result;
First get an invoice object like this:
$invoice = Invoice::get($invoiceId, $apiContext);
Then, you could do the following to cancel it.
// ### Cancel Notification Object
// This would send a notification to both merchant as well
// the payer about the cancellation. The information of
// merchant and payer is retrieved from the invoice details
$notify = new CancelNotification();
$notify
->setSubject("Past due")
->setNote("Canceling invoice")
->setSendToMerchant(true)
->setSendToPayer(true);
// ### Cancel Invoice
// Cancel invoice object by calling the
// static `cancel` method
// on the Invoice class by passing a valid
// notification object
// (See bootstrap.php for more on `ApiContext`)
$cancelStatus = $invoice->cancel($notify, $apiContext);
Also, to test the code, you could always run the samples, and test it out yourself, by just clicking a button.
I ran the sample to cancel an invoice, and then use the similar information provided back on get response of invoice:
"metadata": {
"created_date": "2015-02-04 13:12:33 PST",
"first_sent_date": "2015-02-04 13:12:34 PST",
"last_sent_date": "2015-02-04 13:12:34 PST",
"payer_view_url": "https://www.sandbox.paypal.com/cgi_bin/webscr?cmd=_pay-inv&viewtype=altview&id=INV2-6S46-MLLN-3FEA-VLZE"
}
Opening the URL showed me that the invoice was cancelled as shown below: