public function paymentProcess()
 {
     $rules = array('amount' => 'required|numeric|min:0', 'currency' => 'in:' . implode(',', array_keys(PaymentData::$currencyList)), 'customer_full_name' => 'required|max:128', 'cc_holder_name' => 'required|max:128', 'cc_number' => 'required||numeric|regex:/^\\d{13,19}$/', 'cc_expiration' => 'required|regex:/^\\d{2}\\/\\d{4}$/', 'cc_ccv2' => 'required|numeric|regex:/^\\d{3,4}$/');
     $messages = ['cc_holder_name.required' => 'Credit card holder name is required', 'cc_holder_name' => 'Credit card holder name can not be more than 128 letters', 'cc_number.required' => 'Credit card number is required', 'cc_number.numeric' => 'Credit card number has to be numeric', 'cc_number.regex' => 'Wrong credit card number', 'cc_expiration.required' => 'Credit card expiration date is required', 'cc_expiration.regex' => 'Wrong credit card expiration date', 'cc_ccv2.required' => 'Credit card CVV is required', 'cc_ccv2.numeric' => 'Credit card CVV has to be numeric', 'cc_ccv2.regex' => 'Wrong credit card CVV'];
     $formData = Input::all();
     $validator = Validator::make($formData, $rules, $messages);
     if ($validator->fails()) {
         return Redirect::route('index')->withErrors($validator)->withInput();
     }
     $currency = PaymentData::$currencyList[(int) $formData['currency']];
     $isAMEX = PaymentData::getCCTypeByNumber($formData['cc_number']) === 'american express';
     if ($isAMEX && $currency !== 'USD') {
         return Redirect::route('index')->withInput()->withErrors(['AMEX is possible to use only for USD']);
     }
     $expirationDate = explode('/', $formData['cc_expiration']);
     $paymentData = new PaymentData();
     $paymentData->setAmount($formData['amount'])->setCurrency($currency)->setCustomerFullName($formData['customer_full_name'])->setCCHolderName($formData['cc_holder_name'])->setCCExpirationMonth($expirationDate[0])->setCCExpirationYear($expirationDate[1])->setCCNumber($formData['cc_number'])->setCCV2($formData['cc_ccv2']);
     $paymentSystem = null;
     if ($isAMEX) {
         // if credit card type is AMEX, then use Paypal
         $paymentSystem = PGatewayFactory::create('PayPal');
     } else {
         switch ($currency) {
             //if currency is USD, EUR, or AUD, then use Paypal
             case 'USD':
             case 'EUR':
             case 'AUD':
                 $paymentSystem = PGatewayFactory::create('PayPal');
                 break;
             default:
                 //otherwise use Braintree
                 $paymentSystem = PGatewayFactory::create('BrainTree');
         }
     }
     $paymentId = $paymentSystem->pay($paymentData);
     if ($paymentId) {
         DB::table('payments_history')->insert(['ph_payment_system' => $paymentSystem->getPSName(), 'ph_payment_id' => $paymentId, 'ph_date' => (new DateTime())->format('Y-m-d H:i:s'), 'ph_amount' => $formData['amount'], 'ph_currency' => $currency, 'ph_customer_name' => $formData['customer_full_name'], 'ph_cc_number' => $formData['cc_number']]);
         return View::make('index', ['message' => 'Payment was successful', 'currencyList' => PaymentData::$currencyList]);
     } else {
         return Redirect::route('index')->withInput()->withErrors($paymentSystem->getErrors());
     }
 }
Beispiel #2
0
 public function pay(PaymentData $paymentData)
 {
     $paymentId = null;
     if ($paymentData->isValid()) {
         $holderName = explode(' ', $paymentData->getCCHolderName());
         $holderFirstName = '';
         $holderLastName = '';
         if (count($holderName > 1)) {
             $holderFirstName = $holderName[0];
             $holderName[0] = '';
             $holderName = trim(implode(' ', $holderName));
             $holderLastName = $holderName;
         } else {
             $holderFirstName = $paymentData->getCCHolderName();
         }
         // ### CreditCard
         // A resource representing a credit card that can be
         // used to fund a payment.
         $card = new CreditCard();
         $card->setType($paymentData->getCCType())->setNumber($paymentData->getCCNumber())->setExpireMonth($paymentData->getCCExpirationMonth())->setExpireYear($paymentData->getCCExpirationYear())->setCvv2($paymentData->getCCV2())->setFirstName($holderFirstName)->setLastName($holderLastName);
         // ### FundingInstrument
         // A resource representing a Payer's funding instrument.
         // For direct credit card payments, set the CreditCard
         // field on this object.
         $fi = new FundingInstrument();
         $fi->setCreditCard($card);
         // ### PayerInfo
         // Contains payers info data
         // commented because field email is required but user doesn't fill it in form
         /*
         $payerInfo = new PayerInfo();
         $payerInfo->setFirstName('Name')
             ->setLastName('Surname')
             ->setEmail('login@domain'); //this field is required!
         */
         // ### Payer
         // A resource representing a Payer that funds a payment
         // For direct credit card payments, set payment method
         // to 'credit_card' and add an array of funding instruments.
         $payer = new Payer();
         $payer->setPaymentMethod("credit_card")->setFundingInstruments(array($fi));
         //->setPayerInfo($payerInfo);
         // ### Amount
         // Lets you specify a payment amount.
         // You can also specify additional details
         // such as shipping, tax.
         $amount = new Amount();
         $amount->setCurrency($paymentData->getCurrency())->setTotal($paymentData->getAmount());
         // ### Transaction
         // A transaction defines the contract of a
         // payment - what is the payment for and who
         // is fulfilling it.
         $transaction = new Transaction();
         $transaction->setAmount($amount)->setDescription("Customer full name: " . $paymentData->getCustomerFullName())->setInvoiceNumber(uniqid());
         // ### Payment
         // A Payment Resource; create one using
         // the above types and intent set to sale 'sale'
         $payment = new Payment();
         $payment->setIntent("sale")->setPayer($payer)->setTransactions([$transaction]);
         try {
             $payment->create($this->apiContext);
             $paymentId = $payment->getId();
         } catch (\Exception $ex) {
             $this->errorMessages[] = "Error: " . $ex->getMessage() . '<br/> Details: ' . $ex->getData();
         }
     } else {
         $this->errorMessages = $paymentData->getErrorMessages();
     }
     return $paymentId;
 }
Beispiel #3
0
 public function pay(PaymentData $paymentData)
 {
     $paymentId = null;
     if ($paymentData->isValid()) {
         $customerName = explode(' ', $paymentData->getCustomerFullName());
         $customerFirstName = '';
         $customerLastName = '';
         if (count($customerName) > 1) {
             $customerFirstName = $customerName[0];
             $customerName[0] = '';
             $customerName = trim(implode(' ', $customerName));
             $customerLastName = $customerName;
         } else {
             $customerFirstName = $paymentData->getCustomerFullName();
         }
         $this->paymentResult = Braintree_Transaction::sale(["amount" => $paymentData->getAmount(), "creditCard" => ["number" => $paymentData->getCCNumber(), "cvv" => $paymentData->getCCV2(), "expirationMonth" => $paymentData->getCCExpirationMonth(), "expirationYear" => $paymentData->getCCExpirationYear(), "cardholderName" => $paymentData->getCCHolderName()], "customer" => ["firstName" => $customerFirstName, "lastName" => $customerLastName], "options" => ["submitForSettlement" => true]]);
         if ($this->paymentResult->success) {
             $paymentId = $this->paymentResult->transaction->id;
             //if success return transaction id
         } else {
             if ($this->paymentResult->transaction) {
                 $this->errorMessages[] = 'Error: ' . $this->paymentResult->message . '(Code: ' . $this->paymentResult->transaction->processorResponseCode . ')';
             } else {
                 $this->errorMessages[] = "Validation errors:<br/>";
                 foreach ($this->paymentResult->errors->deepAll() as $error) {
                     $this->errorMessages[count($this->errorMessages) - 1] .= "\n- " . $error->message . "<br/>";
                 }
             }
         }
     } else {
         $this->errorMessages = $paymentData->getErrorMessages();
     }
     return $paymentId;
 }