/** * Suspend billing agreement with paypal * @param object $createdAgreement Agreement * @param $agreementStateDescription * @return bool * @internal param string $agreementStateDescriptor */ function suspend($createdAgreement, $agreementStateDescription) { //Create an Agreement State Descriptor, explaining the reason to suspend. $agreementStateDescriptor = new AgreementStateDescriptor(); $agreementStateDescriptor->setNote($agreementStateDescription); $createdAgreement->suspend($agreementStateDescriptor, $this->getAdapter()->getApiContext()); // Lets get the updated Agreement Object return Agreement::get($createdAgreement->getId(), $this->getAdapter()->getApiContext()); }
/** * Update existing billing agreement at paypal * @param $agreementId * @param string $description * @return bool * @internal param object $createdAgreement Agreement */ function cancel($agreementId, $description = "Cancelling the agreement") { $agreement = Agreement::get($agreementId, $this->getAdapter()->getApiContext()); $agreementStateDescriptor = new AgreementStateDescriptor(); $agreementStateDescriptor->setNote($description); try { $agreement->cancel($agreementStateDescriptor, $this->getAdapter()->getApiContext()); } catch (\Exception $ex) { //@todo add some logging } return true; }
/** * Update existing billing agreement at paypal * @param $agreementId * @param array $params * @return bool * @internal param object $createdAgreement Agreement */ function update($agreementId, $params) { $patch = new Patch(); $patch->setOp('replace')->setPath('/')->setValue($params); $patchRequest = new PatchRequest(); $patchRequest->addPatch($patch); try { $agreement = Agreement::get($agreementId, $this->getAdapter()->getApiContext()); $agreement->update($patchRequest, $this->getAdapter()->getApiContext()); } catch (\Exception $ex) { //@todo add some logging } return true; }
/** * 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(); }
<?php // # Reactivate an agreement // // This sample code demonstrate how you can reactivate a billing agreement, as documented here at: // https://developer.paypal.com/webapps/developer/docs/api/#suspend-an-agreement // API used: /v1/payments/billing-agreements/<Agreement-Id>/suspend // Retrieving the Agreement object from Suspend Agreement Sample to demonstrate the List /** @var Agreement $suspendedAgreement */ $suspendedAgreement = (require 'SuspendBillingAgreement.php'); use PayPal\Api\Agreement; use PayPal\Api\AgreementStateDescriptor; //Create an Agreement State Descriptor, explaining the reason to suspend. $agreementStateDescriptor = new AgreementStateDescriptor(); $agreementStateDescriptor->setNote("Reactivating the agreement"); try { $suspendedAgreement->reActivate($agreementStateDescriptor, $apiContext); // Lets get the updated Agreement Object $agreement = Agreement::get($suspendedAgreement->getId(), $apiContext); } catch (Exception $ex) { ResultPrinter::printResult("Reactivate the Agreement", "Agreement", $agreement->getId(), $suspendedAgreement, $ex); exit(1); } ResultPrinter::printResult("Reactivate the Agreement", "Agreement", $agreement->getId(), $suspendedAgreement, $agreement); return $agreement;
// ## 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) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Executed an Agreement", "Agreement", $agreement->getId(), $_GET['token'], $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Executed an Agreement", "Agreement", $agreement->getId(), $_GET['token'], $agreement); // ## Get Agreement // Make a get call to retrieve the executed agreement details try { $agreement = \PayPal\Api\Agreement::get($agreement->getId(), $apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Get Agreement", "Agreement", null, null, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Get Agreement", "Agreement", $agreement->getId(), null, $agreement); } else { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("User Cancelled the Approval", null); }
/** * @dataProvider mockProvider * @param Agreement $obj */ public function testTransactions($obj, $mockApiContext) { $mockPayPalRestCall = $this->getMockBuilder('\\PayPal\\Transport\\PayPalRestCall')->disableOriginalConstructor()->getMock(); $mockPayPalRestCall->expects($this->any())->method('execute')->will($this->returnValue(AgreementTransactionsTest::getJson())); $result = $obj->searchTransactions("agreementId", array(), $mockApiContext, $mockPayPalRestCall); $this->assertNotNull($result); }
<?php // # Search Billing Transactions Sample // // This sample code demonstrate how you can search all billing transactions, as documented here at: // https://developer.paypal.com/webapps/developer/docs/api/#search-for-transactions // API used: GET /v1/payments/billing-agreements/<Agreement-Id>/transactions? start-date=yyyy-mm-dd&end-date=yyyy-mm-dd // Retrieving the Agreement object from Get Billing Agreement. This may not be necessary if you are trying to search for transactions of already created Agreement. /** @var Agreement $agreement */ $agreement = (require 'GetBillingAgreement.php'); // Replace this with your AgreementId to search transactions based on your agreement. $agreementId = $agreement->getId(); use PayPal\Api\Agreement; // Adding Params to search transaction within a given time frame. $params = array('start_date' => date('Y-m-d', strtotime('-15 years')), 'end_date' => date('Y-m-d', strtotime('+5 days'))); try { $result = Agreement::searchTransactions($agreementId, $params, $apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Search for Transactions", "AgreementTransaction", $agreementId, null, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Search for Transactions", "AgreementTransaction", $agreementId, $params, $result); return $agreement;
/** * @depends testGet * @param $agreement Agreement * @return Agreement */ public function testGetTransactions($agreement) { $params = array('start_date' => date('Y-m-d', strtotime('-15 years')), 'end_date' => date('Y-m-d', strtotime('+5 days'))); $result = Agreement::searchTransactions($agreement->getId(), $params, $this->apiContext, $this->mockPayPalRestCall); $this->assertNotNull($result); $this->assertTrue(is_array($result->getAgreementTransactionList())); $this->assertTrue(sizeof($result->getAgreementTransactionList()) > 0); $list = $result->getAgreementTransactionList(); $first = $list[0]; $this->assertEquals($first->getTransactionId(), $agreement->getId()); }
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); }
/** * Getting the paypal customers for the user * @param paypal api_context * * @return an array with the subscriptions */ public static function getCustomers($apiContext) { // retrieving the subscriptions // !!! Currently unavailable !!! // subscription_ids = getSubscriptions() $subscriptionIds = array("I-F231FUFEPYG8", "I-WFTN8BULD984", "I-YSRV6BDEPBLG"); // initializing customer array $customers = array(); // counter (just for testing purposes) $i = 0; // going through the ids foreach ($subscriptionIds as $subId) { $i++; // just for testing // trying to get the agreements one by one try { // getting the agreement $agreement = Agreement::get($subId, $apiContext); // transforming agreement to our format $formatted_agreement = array('id' => $subId, 'start' => strtotime($agreement->getStartDate()), 'status' => strtolower($agreement->getState()), 'plan' => array('id' => self::generatePlanId($agreement->getPlan()))); // getting the payer of the agreement $payer = $agreement->getPayer(); // this is not working yet because the API sucks at the moment // ... but it'll get better // getting the payer_info //$payer_info = $payer->getPayerInfo(); // getting the user id //$user_email = $payer_info->getEmail(); // right now just adding something that makes sense $user_email = $i % 2; $found = false; // finding out whether or not we know this customer foreach ($customers as $index => $customer_i) { // matching email if ($customer_i['email'] == $user_email) { // found $found = $index; } } if ($found === false) { // customer not found // pushing new customer to array array_push($customers, array('zombie' => false, 'email' => $user_email, 'subscriptions' => array('total_count' => 1, 'data' => array($formatted_agreement)))); // user's first subscription } else { // we already added this customer // adding agreement to the existing ones array_push($customers[$found]['subscriptions']['data'], $formatted_agreement); // adding total count $customers[$found]['subscriptions']['total_count']++; } } catch (PayPal\Exception\PayPalConnectionException $ex) { // an error occoured echo '<pre>'; print_r(json_decode($ex->getData())); exit(1); } } // returning object return $customers; }
/** * Retrieve details for a particular billing agreement by passing the ID of the agreement to the request URI. * * @param string $agreementId * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials. * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls * @return Agreement */ public static function get($agreementId, $apiContext = null, $restCall = null) { ArgumentValidator::validate($agreementId, 'agreementId'); $payLoad = ""; $json = self::executeCall("/v1/payments/billing-agreements/{$agreementId}", "GET", $payLoad, null, $apiContext, $restCall); $ret = new Agreement(); $ret->fromJson($json); return $ret; }
/** * Suspend billing agreement with paypal * @param $agreementId * @return bool * @internal param object $createdAgreement Agreement * @internal param string $agreementStateDescriptor */ function get($agreementId) { return $agreement = Agreement::get($agreementId, $this->getAdapter()->getApiContext()); }
/** * Execute agreement * @param $token * @return Agreement */ function execute($token) { $agreement = new Agreement(); return $agreement->execute($token, $this->getAdapter()->getApiContext()); }
// Retrieving the Agreement object from Create Agreement Sample to demonstrate the List /** @var Agreement $createdAgreement */ $agreement_id = 'P-57V38520G74040029VY7DG6A'; use PayPal\Api\Agreement; use PayPal\Api\Patch; use PayPal\Api\PatchRequest; $patch = new Patch(); $patch->setOp('replace')->setPath('/')->setValue(json_decode('{ "description": "New Description", "shipping_address": { "line1": "2065 Hamilton Ave", "city": "San Jose", "state": "CA", "postal_code": "95125", "country_code": "US" } }')); $patchRequest = new PatchRequest(); $patchRequest->addPatch($patch); try { $createdAgreement->update($patchRequest, $apiContext); // Lets get the updated Agreement Object $agreement = Agreement::get($agreement_id, $apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Updated the Agreement with new Description and Updated Shipping Address", "Agreement", null, $patchRequest, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Updated the Agreement with new Description and Updated Shipping Address", "Agreement", $agreement->getId(), $patchRequest, $agreement); return $agreement;
"email": "*****@*****.**" }, "funding_instruments": [ { "credit_card": { "type": "visa", "number": "4417119669820331", "expire_month": "12", "expire_year": "2017", "cvv2": "128" } } ] } }*/ $agreement = new Agreement(); $agreement->setName('DPRP')->setDescription('Payment with credit Card')->setStartDate('2015-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($createdPlan->getId()); $agreement->setPlan($plan); // Add Payer $payer = new Payer(); $payer->setPaymentMethod('credit_card')->setPayerInfo(new PayerInfo(array('email' => '*****@*****.**'))); // Add Credit Card to Funding Instruments $creditCard = new CreditCard(); $creditCard->setType('visa')->setNumber('4491759698858890')->setExpireMonth('12')->setExpireYear('2017')->setCvv2('128'); $fundingInstrument = new FundingInstrument(); $fundingInstrument->setCreditCard($creditCard); $payer->setFundingInstruments(array($fundingInstrument));
/** @var bool $is_0_dollar */ if (isset($is_0_dollar) && $is_0_dollar) { // $plan_id = 'P-42H24137XS035340AVY2SXJY'; // $plan_id = 'P-9HD4220805509340JP6WL4SI';//0 dollar local $plan_id = 'P-0AV72437SF127301MP7PIPUI'; $agree_desc = 'ThinkThinly monthly subscription 0 dollar'; } else { $plan_id = 'P-2XM09435HG6440939WL5BD4A'; // $plan_id = 'P-2XM09435HG6440939WL5BD4A';//old plan $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
"start_date": "2015-06-17T9:45:04Z", "plan": { "id": "P-1WJ68935LL406420PUTENA2I" }, "payer": { "payment_method": "paypal" }, "shipping_address": { "line1": "111 First Street", "city": "Saratoga", "state": "CA", "postal_code": "95070", "country_code": "US" } }*/ $agreement = new Agreement(); $agreement->setName('Base Agreement')->setDescription('Basic Agreement')->setStartDate('2015-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($createdPlan->getId()); $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); // For Sample Purposes Only.
<?php // # Suspend an agreement // // This sample code demonstrate how you can suspend a billing agreement, as documented here at: // https://developer.paypal.com/webapps/developer/docs/api/#suspend-an-agreement // API used: /v1/payments/billing-agreements/<Agreement-Id>/suspend // Retrieving the Agreement object from Create Agreement Sample to demonstrate the List /** @var Agreement $createdAgreement */ $createdAgreement = (require 'CreateBillingAgreementWithCreditCard.php'); use PayPal\Api\Agreement; use PayPal\Api\AgreementStateDescriptor; //Create an Agreement State Descriptor, explaining the reason to suspend. $agreementStateDescriptor = new AgreementStateDescriptor(); $agreementStateDescriptor->setNote("Suspending the agreement"); try { $createdAgreement->suspend($agreementStateDescriptor, $apiContext); // Lets get the updated Agreement Object $agreement = Agreement::get($createdAgreement->getId(), $apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Suspended the Agreement", "Agreement", null, $agreementStateDescriptor, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Suspended the Agreement", "Agreement", $agreement->getId(), $agreementStateDescriptor, $agreement); return $agreement;
/** * @depends testGet * @param $agreement Agreement * @return Agreement */ public function testGetTransactions($agreement) { $this->markTestSkipped('Skipped as the fix is on the way.'); $result = Agreement::transactions($agreement->getId(), null, $this->mockPayPalRestCall); $this->assertNotNull($result); }