public function createBillingPlan($cartSummary, $productName, $transactionId, $apiContext) { $billingPlanDefaultValues = $this->getBillingPlanDefaultValues(); $billingPlan = new Plan(); $billingPlan->setName('Payment plan for ' . $productName); $billingPlan->setDescription($cartSummary->paymentPlanTitle); $billingPlan->setType($billingPlanDefaultValues->type); $paymentDefinition = new PaymentDefinition(); $paymentDefinition->setName('Charge for ' . $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 is taken out in the 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 just passes a token in the return Url. This token is unique for each request. So pass the transection id in the 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); }
/** * Create billing plan at paypal * * @param $name * @param $description * @param $amount * @param string $returnUrl * @param string $returnCancel * @param int $setupFee * @param bool $isTrial * @param string $currency * @param string $planType * @param string $definitionName * @param string $definitionType * @param string $definitionFrequency * @param string $definitionFrequencyInterval * @param string $definitionCycles * @param string $trialDefinitionName * @param string $trialDefinitionType * @param string $trialDefinitionFrequency * @param string $trialDefinitionFrequencyInterval * @param string $trialDefinitionCycles * @param string $trialDefinitionAmount * @return string */ function create($name, $description, $amount, $returnUrl = '', $returnCancel = '', $setupFee = 0, $isTrial = false, $currency = 'USD', $planType = 'fixed', $definitionName = 'Regular Payments', $definitionType = 'REGULAR', $definitionFrequency = 'Month', $definitionFrequencyInterval = '1', $definitionCycles = '12', $trialDefinitionName = 'Trial Period', $trialDefinitionType = 'TRIAL', $trialDefinitionFrequency = '0', $trialDefinitionFrequencyInterval = '0', $trialDefinitionCycles = '0', $trialDefinitionAmount = '0') { $plan = new Plan(); $plan->setName($name)->setDescription($description)->setType($planType); $paymentDefinition = new PaymentDefinition(); $paymentDefinition->setName($definitionName)->setType($definitionType)->setFrequency($definitionFrequency)->setFrequencyInterval($definitionFrequencyInterval)->setCycles($definitionCycles)->setAmount(new Currency(array('value' => $amount, 'currency' => $currency))); $plan->setPaymentDefinitions(array($paymentDefinition)); if ($isTrial) { $paymentDefinitionTrial = new PaymentDefinition(); $paymentDefinitionTrial->setName($trialDefinitionName)->setType($trialDefinitionType)->setFrequency($trialDefinitionFrequency)->setFrequencyInterval($trialDefinitionFrequencyInterval)->setCycles($trialDefinitionCycles)->setAmount(new Currency(array('value' => $trialDefinitionAmount, 'currency' => $currency))); $plan->addPaymentDefinition($paymentDefinitionTrial); } $merchantPreferences = new MerchantPreferences(); $merchantPreferences->setReturnUrl($returnUrl)->setCancelUrl($returnCancel)->setAutoBillAmount("yes")->setInitialFailAmountAction("CONTINUE")->setMaxFailAttempts("0")->setSetupFee(new Currency(array('value' => $setupFee, 'currency' => $currency))); $plan->setPaymentDefinitions(array($paymentDefinition)); $plan->setMerchantPreferences($merchantPreferences); $result = $plan->create($this->getAdapter()->getApiContext()); if ($result) { return ['plan' => $result, 'id' => $result->getId(), 'state' => $result->getState(), 'created' => $result->getCreateTime()]; } return $result; }
use PayPal\Api\ChargeModel; 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('T-Shirt of the Month Club Plan')->setDescription('Template creation.')->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("2")->setCycles("12")->setAmount(new Currency(array('value' => 100, 'currency' => 'USD'))); // Charge Models $chargeModel = new ChargeModel(); $chargeModel->setType('SHIPPING')->setAmount(new Currency(array('value' => 10, 'currency' => 'USD'))); $paymentDefinition->setChargeModels(array($chargeModel)); $merchantPreferences = new MerchantPreferences(); $baseUrl = getBaseUrl(); // ReturnURL and CancelURL are not required and used when creating billing agreement with payment_method as "credit_card". // However, it is generally a good idea to set these values, in case you plan to create billing agreements which accepts "paypal" as payment_method. // This will keep your plan compatible with both the possible scenarios on how it is being used in agreement. $merchantPreferences->setReturnUrl("{$baseUrl}/ExecuteAgreement.php?success=true")->setCancelUrl("{$baseUrl}/ExecuteAgreement.php?success=false")->setAutoBillAmount("yes")->setInitialFailAmountAction("CONTINUE")->setMaxFailAttempts("0")->setSetupFee(new Currency(array('value' => 1, 'currency' => 'USD'))); $plan->setPaymentDefinitions(array($paymentDefinition)); $plan->setMerchantPreferences($merchantPreferences); // For Sample Purposes Only. $request = clone $plan; // ### Create Plan
$plan = new Plan(); // # Basic Information // Fill up the basic information that is required for the plan $plan->setName('ThinkThinly monthly subscription 0 dollar')->setDescription('ThinkThinly monthly subscription 0 dollar')->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("12")->setAmount(new Currency(array('value' => 4.99, 'currency' => 'USD'))); // # Trial Payment definitions for this billing plan. $trialPaymentDefinition = 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. $trialPaymentDefinition->setName('Regular Payments')->setType('TRIAL')->setFrequency('Month')->setFrequencyInterval("1")->setCycles("1200")->setAmount(new Currency(array('value' => 0, 'currency' => 'USD'))); // Charge Models //$chargeModel = new ChargeModel(); //$chargeModel->setType('SHIPPING') // ->setAmount(new Currency(array('value' => 10, 'currency' => 'USD'))); //$paymentDefinition->setChargeModels(array($chargeModel)); $merchantPreferences = new MerchantPreferences(); $baseUrl = getBaseUrl(); // ReturnURL and CancelURL are not required and used when creating billing agreement with payment_method as "credit_card". // However, it is generally a good idea to set these values, in case you plan to create billing agreements which accepts "paypal" as payment_method. // This will keep your plan compatible with both the possible scenarios on how it is being used in agreement. $merchantPreferences->setReturnUrl("{$baseUrl}/ExecuteAgreement.php?success=true")->setCancelUrl("{$baseUrl}/ExecuteAgreement.php?success=false")->setAutoBillAmount("yes")->setInitialFailAmountAction("CONTINUE")->setMaxFailAttempts("0"); // ->setSetupFee(new Currency(array('value' => 1, 'currency' => 'USD'))); //$baseUrl = getBaseUrl(); //$redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true") // ->setCancelUrl("$baseUrl/ExecutePayment.php?success=false");