public function createContract($actor, $form) { $em = $this->getDoctrine()->getManager(); //contract $contract = new Contract(); $contract->setFinished(new DateTime('+1 year')); $contract->setActor($actor); $contract->setUrl('http://localhost/aviso-legal'); $actor->addContract($contract); $em->persist($contract); $agree = new Agreement(); $agree->setPlan($form->getNormData()->plan); $agree->setStatus('Created'); $agree->setPaymentMethod('credit_card'); $uid = uniqid(); $agree->setName('Test agreement ' . $uid); $agree->setDescription('Description of test agreement ' . $uid); $agree->setContract($contract); $contract->setAgreement($agree); $em->persist($agree); $em->flush(); //4548812049400004//visa//12//2017//123 //error $errors = $this->get('validator')->validate($agree); if (count($errors) == 0) { $answer = $this->get('checkout_manager')->createPaypalAgreement($agree, array("number" => $form->getNormData()->cardNo, "type" => $form->getNormData()->cardType, "expire_month" => $form->getNormData()->expirationDate->format('m'), "expire_year" => $form->getNormData()->expirationDate->format('Y'), "cvv2" => $form->getNormData()->CVV, "first_name" => $form->getNormData()->firstname, "last_name" => $form->getNormData()->lastname)); return $answer; } else { throw $this->createNotFoundException('Error: ' . json_decode($errors)); } }
/** * @code * phpunit -v --filter testCreatePlan -c app vendor/sebardo/ecommerce/EcommerceBundle/Tests/Entity/PlanTest.php * @endcode * */ public function testCreatePlan() { $client = static::createClient(); $plan = new Plan(); $plan->setName('Trial Plan ' . uniqid()); $plan->setDescription('Monthly plan for getting something.'); $plan->setFrequency('MONTH'); $plan->setFrequencyInterval('1'); $plan->setCycles('11'); $plan->setAmount('19.99'); $plan->setTrialFrequency('MONTH'); $plan->setTrialFrequencyInterval('1'); $plan->setTrialCycles('1'); $plan->setTrialAmount('0'); //error $errors = $client->getContainer()->get('validator')->validate($plan); $this->assertEquals(count($errors), 0); //create $checkoutManager = $client->getContainer()->get('checkout_manager'); $payPalPlan = $checkoutManager->createPaypalPlan($plan); $this->assertEquals($payPalPlan['state'], 'CREATED'); //active and get $payPalPlan = $checkoutManager->activePaypalPlan($plan); $this->assertEquals($payPalPlan['state'], 'ACTIVE'); //create test actor $em = $client->getContainer()->get('doctrine')->getManager(); $actor = new Actor(); $uid = uniqid(); $actor->setEmail('username' . $uid); $actor->setUsername('username' . $uid . '@email.com'); $actor->setName('Test actor ' . $uid); $actor->setCity('City test'); $actor->setAddress('Address 123.'); $country = $em->getRepository('CoreBundle:Country')->find('es'); $actor->setCountry($country); //crypt password $factory = $client->getContainer()->get('security.encoder_factory'); $encoder = $factory->getEncoder(new Actor()); $encodePassword = $encoder->encodePassword($uid, $actor->getSalt()); $actor->setPassword($encodePassword); $em->persist($actor); //contract $contract = new Contract(); $contract->setFinished(new DateTime('+1 year')); $contract->setActor($actor); $contract->setUrl('http://localhost/aviso-legal'); $actor->addContract($contract); $em->persist($contract); $agree = new Agreement(); $agree->setPlan($plan); $agree->setStatus('Created'); $agree->setName('Test agreement ' . uniqid()); $agree->setDescription('Description of test agreement.'); $agree->setPaymentMethod('credit_card'); $agree->setContract($contract); $contract->setAgreement($agree); $em->persist($agree); $em->flush(); //error $errors = $client->getContainer()->get('validator')->validate($agree); $this->assertEquals(count($errors), 0); //agreement $payPalAgreement = $checkoutManager->createPaypalAgreement($agree, array("number" => "4548812049400004", "type" => "visa", "expire_month" => 12, "expire_year" => 2017, "cvv2" => 123, "first_name" => "Betsy", "last_name" => "Buyer")); // print_r($payPalAgreement); $this->assertEquals($payPalAgreement['state'], 'Active'); }
public function createPaypalAgreement(Agreement $agreement, $credtCard = null) { $this->paypalToken(); $paymentMehod = array(); if ($agreement->getPaymentMethod() == 'paypal') { $paymentMehod = array("payment_method" => "paypal"); } elseif ($agreement->getPaymentMethod() == 'credit_card') { if (is_null($credtCard)) { throw new \Exception('credit card values must be send'); } $paymentMehod = array("payment_method" => "credit_card", "funding_instruments" => array(array("credit_card" => $credtCard))); } $agreementPayPal = array("name" => $agreement->getName(), "description" => $agreement->getDescription(), "start_date" => date('Y-m-d') . "T" . date('H:i:s') . "Z", "plan" => array("id" => $agreement->getPlan()->getPaypalId()), "payer" => $paymentMehod); $host = $this->paypalFactory->getHost(); $url = $host . '/v1/payments/billing-agreements'; $json = json_encode($agreementPayPal); $answer = $this->paypalCall('POST', $url, $json); /** * The payment are efective at final of cycle of service * so this "status" is just for agreement status * any transaction must be created except setup_fee setted in the plan **/ //if some error in response if (isset($answer['status']) && $answer['status'] == 'error') { $agreement->setStatus($answer['status'] . ':' . json_encode($answer)); } elseif ($agreement->getPaymentMethod() == 'paypal') { if ($this->hasApprovalLink($answer)) { //STATUS_PENDING_APPROVAL $agreement->setStatus($answer['state']); } else { //put status pending if paypal payment methods to redirect to approve $agreement->setStatus($answer['state'] . '-pending'); } } elseif ($agreement->getPaymentMethod() == 'credit_card') { if ($answer['state'] == 'Active') { //we need to move this worlflow transaction $agreement->setStatus($answer['state']); $agreement->setPaypalId($answer['id']); $agreement->setOutstandingAmount($answer['agreement_details']['outstanding_balance']['value']); $agreement->setCyclesRemaining($answer['agreement_details']['cycles_remaining']); $agreement->setNextBillingDate($answer['agreement_details']['next_billing_date']); $agreement->setFinalPaymentDate($answer['agreement_details']['final_payment_date']); $agreement->setFailedPaymentCount($answer['agreement_details']['failed_payment_count']); } } $this->manager->flush(); $this->session->set('agreement', json_encode($answer)); $this->session->save(); //If setupAmount is diferent than 0 //get first completed transaction agreement //and create transaction if ($agreement->getPlan()->getSetupAmount() != 0) { $transactions = $this->searchPaypalAgreementTransactions($agreement); foreach ($transactions['agreement_transaction_list'] as $transaction) { //Created: this is the first paypal transacttion //Expired: this is the last paypal transacttion //Failed: when transaction not billing if ($transaction['status'] == 'Completed') { $transaction = $this->createSale($agreement, $transaction['amount']['value'], $transaction['fee_amount']['value'], $transaction['net_amount']['value'], json_encode($transaction)); } } } return $answer; }