Today, We want to share with you Paypal Recurring Payments PHP REST API Example.In this post we will show you wordpress plugin require another plugin, hear for paypal recurring payment integration in codeigniter we will give you demo and example for implement.In this post, we will learn about PHP Setting up subscriptions and recurring payments for your business with an example paypal payment gateway integration in php source code download.
Paypal Recurring Payments PHP REST API Example
There are the Following The simple About Paypal Recurring Payments PHP REST API Example Full Information With Example and payment gateway in php source code.
As I will cover this Post with live Working example to develop Paypal Recurring Payments for Installment Plan in PHP, so the Recurring payments using Paypal Merchant SDK or Paypal REST API is used for this example is following below.
Handling Recurring Payments in PHP Source code
app_name/index.php
prepare(" SELECT plan_id FROM user_transactions_dtl WHERE hash = :hash "); $plan_id->execute([ 'hash' => $_SESSION['tamil_hash'] ]); $plan_id = $plan_id->fetchObject()->plan_id; $token = $_GET['token']; $agreement = new \PayPal\Api\Agreement(); try { // Execute agreement $agreement->execute($token, $devApiData); //Update transaction $updateTransaction = $db->prepare(" UPDATE user_transactions_dtl SET complete = 1 WHERE plan_id = :plan_id "); $updateTransaction->execute([ 'plan_id' => $plan_id ]); // Set the user as broker $setMember = $db->prepare(" UPDATE users SET broker = 1 WHERE id = :broker_id "); $setMember->execute([ 'broker_id' => $_SESSION['broker_id'] ]); //Unset paypal Hash unset($_SESSION['tamil_hash']); header('Location: ../app_name/paypal/complete.php'); } catch (PayPal\Exception\PayPalConnectionException $ex) { header("Location: ../app_name/paypal/error.php"); echo $ex->getCode(); echo $ex->getData(); die($ex); } catch (Exception $ex) { header("Location: ../app_name/paypal/error.php"); die($ex); } } else { echo "user canceled agreement"; header('Location: ../app_name/paypal/cancelled.php'); } exit; } if (! empty($_POST["subscribe"])) { require_once "./src/createSubscriptionPlan.php"; } ?>how to setup recurring payments in paypal GoldenRead Golden.$45 / month
app_name/composer.json
{ "require": { "paypal/rest-api-sdk-php": "*" } }
run bellow Commands
cd/app_name> composer update
app_name/src/init_model.php
setConfig([ 'mode' => 'sandbox', 'http.ConnectionTimeOut' => 30, 'log.LogEnabled' => false, 'log.FileName' => '', 'log.LogLevel' => 'FINE', 'validation.level' => 'log' ]);*/ $db = new PDO('mysql:host=localhost;dbname=paypal_site', 'root', ''); $user = $db->prepare("select * from users where id= :broker_id"); $user->execute(['broker_id' => $_SESSION['broker_id']]); $user = $user->fetchObject();
app_name/src/createSubscriptionPlan.php
setName($_POST["plan_name"]) ->setDescription($_POST["plan_description"]) ->setType('FIXED'); // Set billing plan definitions $paymentDefinition = new PaymentDefinition(); $paymentDefinition->setName('Regular Payments') ->setType('REGULAR') ->setFrequency('DAY') ->setFrequencyInterval('1') ->setCycles('3') ->setAmount(new Currency(array( 'value' => 3, 'currency' => 'USD' ))); // Set charge models $chargeModel = new ChargeModel(); $chargeModel->setType('SHIPPING')->setAmount(new Currency(array( 'value' => 1, 'currency' => 'USD' ))); $paymentDefinition->setChargeModels(array( $chargeModel )); // Set merchant preferences $merchantPreferences = new MerchantPreferences(); $merchantPreferences->setReturnUrl('http://localhost/app_name/index.php?status=success') ->setCancelUrl('http://localhost/app_name/index.php?status=cancel') ->setAutoBillAmount('yes') ->setInitialFailAmountAction('CONTINUE') ->setMaxFailAttempts('0') ->setSetupFee(new Currency(array( 'value' => 1, 'currency' => 'USD' ))); $plan->setPaymentDefinitions(array( $paymentDefinition )); $plan->setMerchantPreferences($merchantPreferences); try { $livePlan = $plan->create($devApiData); //Generate and store hash $hash = md5($livePlan->getId()); $_SESSION['tamil_hash'] = $hash; //transaction storage $store = $db->prepare(" INSERT INTO user_transactions_dtl (broker_id, plan_id, hash, complete) VALUES (:broker_id, :plan_id, :hash, 0) "); $store->execute([ 'broker_id' => $_SESSION['broker_id'], 'plan_id' => $livePlan->getId(), 'hash' => $hash ]); try { $patch = new Patch(); $value = new PayPalModel('{"state":"ACTIVE"}'); $patch->setOp('replace') ->setPath('/') ->setValue($value); $patchRequest = new PatchRequest(); $patchRequest->addPatch($patch); $livePlan->update($patchRequest, $devApiData); $patchedPlan = Plan::get($livePlan->getId(), $devApiData); require_once "createSubscriptionAgreement.php"; } catch (PayPal\Exception\PayPalConnectionException $ex) { echo $ex->getCode(); echo $ex->getData(); die($ex); } catch (Exception $ex) { die($ex); } } catch (PayPal\Exception\PayPalConnectionException $ex) { echo $ex->getCode(); echo $ex->getData(); die($ex); } catch (Exception $ex) { die($ex); } } ?>
app_name/src/createSubscriptionAgreement.php
setName('Pakainfo Golden Plan Subscription Agreement') ->setDescription('Pakainfo Golden Plan Subscription Billing Agreement') ->setStartDate($startDate); //here choose or Set plan id $plan = new Plan(); $plan->setId($patchedPlan->getId()); $agreement->setPlan($plan); // here Add payer type $payer = new Payer(); $payer->setPaymentMethod('paypal'); $agreement->setPayer($payer); //simple Adding shipping details $shippingAddress = new ShippingAddress(); $shippingAddress->setLine1('9, swati park') ->setCity('Rajkot') ->setState('IN') ->setPostalCode('360001') ->setCountryCode('US'); $agreement->setShippingAddress($shippingAddress); try { // Create a simple main user agreement $agreement = $agreement->create($devApiData); // Extract success approval URL to redirect user $approvalUrl = $agreement->getApprovalLink(); header("Location: " . $approvalUrl); exit(); } catch (PayPal\Exception\PayPalConnectionException $ex) { echo $ex->getCode(); echo $ex->getData(); die($ex); } catch (Exception $ex) { die($ex); } ?>
app_name/paypal/cancelled.php
Recurring payment in paypal every month in php You Cancelled.
app_name/paypal/complete.php
Setting up PayPal Recurring Payments in PHP Payment complete. Go Home
app_name/paypal/error.php
PayPal recurring payments API Something Went Wrong.
MySQL Database structure
Table structure for table `trans_details`
CREATE TABLE `trans_details` ( `id` int(10) UNSIGNED NOT NULL, `broker_id` int(11) NOT NULL, `plan_id` varchar(255) NOT NULL, `hash` varchar(255) NOT NULL, `complete` tinyint(1) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Indexes for table `trans_details`
ALTER TABLE `trans_details` ADD PRIMARY KEY (`id`);
Table structure for table `users_dtl`
CREATE TABLE `users_dtl` ( `id` int(10) UNSIGNED NOT NULL, `username` varchar(20) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `broker` tinyint(1) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Dumping data for table `users_dtl`
INSERT INTO `users_dtl` (`id`, `username`, `email`, `broker`) VALUES (1, 'modi124', 'modi124@domain_name.com', 0);
Indexes for table `users_dtl`
ALTER TABLE `users_dtl` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for table `users_dtl` -- ALTER TABLE `users_dtl` MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; COMMIT;
Web Programming Tutorials Example with Demo
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about How to Manage Recurring Payments using PayPal Subscriptions in PHP.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.