/** * Create billing agreement with paypal * @param $createdPlan * @param $agreementName * @param $agreementDescription * @param string $startTime * @return null|string Redirect url */ function create($createdPlan, $agreementName, $agreementDescription, $startTime = '') { if (empty($startTime)) { $time = time() + 30 * (24 * 3600); $startTime = date('Y-m-d\\TH:i:s\\Z', $time); } $agreement = new Agreement(); $agreement->setName($agreementName)->setDescription($agreementDescription)->setStartDate($startTime); // Add Plan ID $plan = new Plan(); $plan->setId($createdPlan['id']); $agreement->setPlan($plan); // Add Payer $payer = new Payer(); $payer->setPaymentMethod('paypal'); $agreement->setPayer($payer); // Please note that as the agreement has not yet activated, we wont be receiving the ID just yet. $agreement = $agreement->create($this->getAdapter()->getApiContext()); // ### Get redirect url // The API response provides the url that you must redirect // the buyer to. Retrieve the url from the $agreement->getApprovalLink() // method return $agreement->getApprovalLink(); }
$agree_desc = 'ThinkThinly monthly subscription'; } use PayPal\Api\Agreement; use PayPal\Api\Payer; use PayPal\Api\Plan; use PayPal\Api\ShippingAddress; $agreement = new Agreement(); $now = new DateTime(); $now->add(DateInterval::createFromDateString('10 hour')); $agreement->setName('Base Agreement')->setDescription($agree_desc)->setStartDate($now->format('Y-m-d') . 'T' . $now->format('G:i:s') . 'Z'); // ->setStartDate('2019-06-17T9:45:04Z'); // Add Plan ID // Please note that the plan Id should be only set in this case. $plan = new Plan(); $plan->setId($plan_id); $agreement->setPlan($plan); // Add Payer $payer = new Payer(); $payer->setPaymentMethod('paypal'); $agreement->setPayer($payer); $agreement->setDescription($agree_desc . ". ID: {$user_id}"); // Add Shipping Address //$shippingAddress = new ShippingAddress(); //$shippingAddress->setLine1('111 First Street') // ->setCity('Saratoga') // ->setState('CA') // ->setPostalCode('95070') // ->setCountryCode('US'); //$agreement->setShippingAddress($shippingAddress); //$merchant_override_pref = new \PayPal\Api\MerchantPreferences(); //$merchant_override_pref->setReturnUrl(getBaseUrl(). "pp/billing/UpdateBillingAgreement.php?user_id=$user_id");
public function createBillingAgreement($planId, $shippingAddress, $billingAddress, $productName, $cartSummary, $cardDetails, $apiContext) { $billingPlanDefaultValues = $this->getBillingPlanDefaultValues(); $billingAgreement = new Agreement(); $billingAgreement->setName('Billing Agreement For ' . $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']); /* Get a MALFORMED_REQUEST error when using this 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); }