php - Processing Recurring payments with Credit Card using PayPal Merchant SDK and Rest API -


similar questions have been asked many times , have gone through them still can't find solution of problem.

i trying process recurring payments through credit cards using paypal rest api. billing plan created , activated successfully. also, billing agreement created correctly. have no idea how execute billing agreement. below code far.

<?php  use paypal\api\payer; use paypal\api\amount; use paypal\api\transaction; use \paypal\api\payment; use \paypal\api\payerinfo; use \paypal\api\details; use \paypal\api\item; use \paypal\api\shippingaddress; use \paypal\api\itemlist; use \paypal\exception; use \paypal\api\creditcard; use \paypal\api\fundinginstrument; use \paypal\exception\paypalconnectionexception; use \paypal\api\plan; use \paypal\api\paymentdefinition; use \paypal\api\currency; use \paypal\api\chargemodel; use \paypal\api\merchantpreferences; use \paypal\api\patchrequest; use \paypal\api\patch; use \paypal\api\agreement; use \paypal\api\address;   class paypal_direct_payment extends paypal_abstract {       public function createbillingplan($cartsummary, $productname, $transactionid, $apicontext){          $billingplandefaultvalues = $this->getbillingplandefaultvalues();          $billingplan = new plan();         $billingplan->setname('payment plan '.$productname);         $billingplan->setdescription($cartsummary->paymentplantitle);         $billingplan->settype($billingplandefaultvalues->type);          $paymentdefinition = new paymentdefinition();         $paymentdefinition->setname('charge '.$productname);         $paymentdefinition->settype('regular');         $paymentdefinition->setfrequencyinterval($billingplandefaultvalues->interval);         $paymentdefinition->setfrequency($billingplandefaultvalues->frequency);         $paymentdefinition->setcycles($billingplandefaultvalues->cycle);          $amount = new currency();         $amount->setcurrency($this->getcurrency());         $amount->setvalue($cartsummary->singleinstallmentcost);          $paymentdefinition->setamount($amount);          $shippingamount = new currency();         $shippingamount->setcurrency($this->getcurrency());         // shipping cost taken out in initial payment (setup_fees)         $shippingamount->setvalue(0);         //$shippingamount->setvalue($cartsummary->shippingcost);          $chargemodelshipping = new chargemodel();         $chargemodelshipping->settype('shipping');         $chargemodelshipping->setamount($shippingamount);          $taxamount = new currency();         $taxamount->setcurrency($this->getcurrency());         $taxamount->setvalue($cartsummary->vat);          $chargemodeltax = new chargemodel();         $chargemodeltax->settype('tax');         $chargemodeltax->setamount($taxamount);          $paymentdefinition->setchargemodels(array($chargemodelshipping, $chargemodeltax));          $billingplan->setpaymentdefinitions(array($paymentdefinition));          $merchantpreferences = new merchantpreferences();          $setupfeesamount = new currency();         $setupfeesamount->setcurrency($this->getcurrency());         $setupfeesamount->setvalue($cartsummary->firstinstallmentcost);          /* paypal passes token in return url. token unique each request. pass transection id in return url. */         $returnurl = $this->getrecurringexpresspaymentreturnurl();         $returnurl = str_replace(':id', $transactionid, $returnurl);         $returnurl = str_replace(':hash', om_model_abstract::generaterequesthash($transactionid), $returnurl);          $merchantpreferences->setsetupfee($setupfeesamount);         $merchantpreferences->setcancelurl($this->getcancelurl());         $merchantpreferences->setreturnurl($returnurl);         $merchantpreferences->setmaxfailattempts($billingplandefaultvalues->maxfailedbillingattempts);         $merchantpreferences->setautobillamount($billingplandefaultvalues->autobillamount);         $merchantpreferences->setinitialfailamountaction($billingplandefaultvalues->initialfailamountaction);          $billingplan->setmerchantpreferences($merchantpreferences);          return $billingplan->create($apicontext);      }      public function activatebillingplan($planid, $apicontext){          $patch = new patch();         $patch->setop('replace');         $patch->setpath('/');         $patch->setvalue(array('state' => 'active'));          $patchrequest = new patchrequest();         $patchrequest->setpatches(array($patch));          $plan = new plan();         $plan->setid($planid);         return $plan->update($patchrequest, $apicontext);      }      public function getbillingplan($planid, $apicontext = null){         if(empty($apicontext)){             $apicontext = $this->getapicontext();         }         $plan = new plan();         return $plan->get($planid, $apicontext);     }      public function createbillingagreement($planid, $shippingaddress, $billingaddress, $productname, $cartsummary, $carddetails, $apicontext) {          $billingplandefaultvalues = $this->getbillingplandefaultvalues();          $billingagreement = new agreement();         $billingagreement->setname('billing agreement '.$productname);         $billingagreement->setdescription($cartsummary->paymentplantitle);          $startdate = new zend_date();         $startdate->addday($billingplandefaultvalues->startdateinterval);         $billingagreement->setstartdate($startdate->get(zend_date::iso_8601));          $payerinfo  = new payerinfo();         $payerinfo->setfirstname($billingaddress->firstname);         $payerinfo->setlastname($billingaddress->lastname);         $payerinfo->setemail($billingaddress->emailaddress);          /* fields not supported yet */         //$payerinfo->setemail($cart->address->billing['billing_email']);         //$payerinfo->setphone($cart->address->billing['billing_contactno']);          /* malformed_request error when using field */         //$payerinfo->setcountrycode($cart->address->billing['billing_countrycode']);          $cardname = $carddetails->cardname;         $cardnumber = $carddetails->cardnumber;         $cardtype = strtolower($carddetails->cardtype);         $cardexpirymonth = $carddetails->cardexpirymonth;         $cardexpiryyear = $carddetails->cardexpiryyear;         $cardsecuritycode = $carddetails->cardsecuritycode;          $nameparser = new om_model_name();         $name = $nameparser->parse_name($cardname);         $card = new creditcard();         $card->settype($cardtype);         $card->setnumber($cardnumber);         $card->setexpiremonth($cardexpirymonth);         $card->setexpireyear($cardexpiryyear);         $card->setcvv2($cardsecuritycode);         $card->setfirstname($name['fname']);         $card->setlastname($name['lname']);          $fundinginstrument = new fundinginstrument();         $fundinginstrument->setcreditcard($card);          $payer = new payer();         $payer->setpaymentmethod("credit_card");         $payer->setfundinginstruments(array($fundinginstrument));         $payer->setpayerinfo($payerinfo);          $billingagreement->setpayer($payer);          $shippingaddresspaypal = new address();         $shippingaddresspaypal->setline1($shippingaddress->addressline1);         $shippingaddresspaypal->setline2($shippingaddress->addressline2. ' ' .$shippingaddress->addressline3);         $shippingaddresspaypal->setcity($shippingaddress->city);         $shippingaddresspaypal->setcountrycode($shippingaddress->getcountry()->code);         $shippingaddresspaypal->setpostalcode($shippingaddress->postcode);         $shippingaddresspaypal->setstate($shippingaddress->county);         $shippingaddresspaypal->setphone($shippingaddress->contactnumber);          $billingagreement->setshippingaddress($shippingaddresspaypal);          $plan = new plan();         $plan->setid($planid);          $billingagreement->setplan($plan);          return $billingagreement->create($apicontext);      }      public function executerecurringpayments($transaction, $carddetails){          $shippingaddress = $transaction->getshippingaddress();         $billingaddress = $transaction->getbillingaddress();         $cart = $transaction->getcart();         $cartitems = $cart->getcartitems();         $cartsummary = $cart->getcartsummary();         /* product name used in billing plan / agreement titles. if there multiple products use name of first product. */         $productname = $cartitems[0]->getproduct()->title;          try {              $apicontext = $this->getapicontext();              /* create billing plan */             $billingplan = $this->createbillingplan($cartsummary, $productname, $transaction->id, $apicontext);              /* active billing plan */             $this->activatebillingplan($billingplan->id, $apicontext);              /* billing plan */             $billingplan = $this->getbillingplan($billingplan->id, $apicontext);               /* create billing agreement */             $billingagreement = $this->createbillingagreement($billingplan->id, $shippingaddress, $billingaddress, $productname, $cartsummary, $carddetails,  $apicontext);          /* how execute billing agreement here? */           } catch (paypalconnectionexception $pce) {             $this->handelexception($pce);         }      }  } 

the agreement object have execute method method requires token. token available if use paypal account make recurring payment. documentation on paypal's site outdated , not find sample code either. hope can me this.

you don't have execute or activate billing plan when using using credit_card payment method.

just calling $billingagreement->create($apicontext); create , execute billing agreement.

you can login paypal merchant account , go recurring payments , active profiles. sure you'll find profiles code created under section.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -