_formDefaults = array( 'data' => array( 'invoice' => $invoiceId, 'amount' => null, 'subtotal' => null, 'tax' => 0.00, 'promo' => 50.00, 'giftCard' => 0.00, 'shipping' => 20.00, 'handling' => 7.98, 'cancelUrl' => $cancelUrl, 'returnUrl' => $returnUrl, 'itemList' => array( 0 => array( 'L_PAYMENTREQUEST_0_AMT' => 150.00, 'L_PAYMENTREQUEST_0_NAME' => 'Anonymous sleeve', 'L_PAYMENTREQUEST_0_QTY' => 2, ), 1 => array( 'L_PAYMENTREQUEST_0_AMT' => 50.00, 'L_PAYMENTREQUEST_0_NAME' => 'Pryanik', 'L_PAYMENTREQUEST_0_QTY' => 4, ), ), 'custom' => array( 'ADDROVERRIDE' => true, 'ALLOWNOTE' => false, 'PAYMENTREQUEST_0_SHIPTOSTREET' => 'Lake Street', 'PAYMENTREQUEST_0_SHIPTOCITY' => 'Minnetonka', 'PAYMENTREQUEST_0_SHIPTOSTATE' => 'MN', 'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE' => 'US', 'PAYMENTREQUEST_0_SHIPTOZIP' => 55305, ) ) ); parent::__construct($options); } protected function _doInsert() { $form = $this->_getNewForm(); if ($form->validate()) { $options = $form->getValues(); $this->_prepareAmount($options); $payment = App_Service_Payment::factory('PayPalExpressCheckout', array( 'type' => 'SET_EXPRESS_CHECKOUT', 'data' => $options['data'], )); $result = $payment->getResponse()->isSuccess(); $paymentData = $payment->getResponse()->getData(); echo 'Result: ', intval($result), '
', 'Response:
' . print_r($paymentData, true) . '

'; exit(); } $this->_addFormItem($form); return $this; } protected function _prepareAmount(array &$options) { $options['data']['subtotal'] = 0.0; foreach ($options['data']['itemList'] as $i) { $options['data']['subtotal'] = bcadd($options['data']['subtotal'], bcmul($i['L_PAYMENTREQUEST_0_AMT'], $i['L_PAYMENTREQUEST_0_QTY'], 2), 2); } $options['data']['subtotal'] = bcsub($options['data']['subtotal'], $options['data']['promo'], 2); $options['data']['subtotal'] = bcsub($options['data']['subtotal'], $options['data']['giftCard'], 2); $options['data']['amount'] = 0.0; $options['data']['amount'] = bcadd($options['data']['amount'], $options['data']['subtotal'], 2); $options['data']['amount'] = bcadd($options['data']['amount'], $options['data']['tax'], 2); $options['data']['amount'] = bcadd($options['data']['amount'], $options['data']['shipping'], 2); $options['data']['amount'] = bcadd($options['data']['amount'], $options['data']['handling'], 2); return $this; } protected function _getNewForm() { $form = new Qs_Form($this->_getFormOptions()); $form->addElement('hidden', 'action', array('value' => 'insert')); $this->_bindFormFields($form); $form->setDefaults($this->_formDefaults); return $form; } protected function _bindFormFields(Qs_Form $form) { $mainSubForm = new Qs_Form_SubForm(); $mainSubForm->addElement('text', 'invoice', array('label' => 'Invoice #', 'required' => true)); $mainSubForm->addElement('text', 'amount', array('label' => 'Amount', 'disabled' => 'disabled')); $mainSubForm->addElement('text', 'subtotal', array('label' => 'Subtotal', 'disabled' => 'disabled')); $mainSubForm->addElement('text', 'tax', array('label' => 'Tax', 'required' => true)); $mainSubForm->addElement('text', 'promo', array('label' => 'Promo', 'required' => true)); $mainSubForm->addElement('text', 'giftCard', array('label' => 'GiftCard', 'required' => true)); $mainSubForm->addElement('text', 'shipping', array('label' => 'Shipping', 'required' => true)); $mainSubForm->addElement('text', 'handling', array('label' => 'Handling', 'required' => true)); $mainSubForm->addElement('text', 'cancelUrl', array('label' => 'Cancel Url', 'readonly' => 'readonly')); $mainSubForm->addElement('text', 'returnUrl', array('label' => 'Return Url', 'readonly' => 'readonly')); $itemSubForm = new Qs_Form_SubForm(); $itemSubForm->addElement('text', 'L_PAYMENTREQUEST_0_NAME', array('label' => 'Item Name', 'required' => true)); $itemSubForm->addElement('text', 'L_PAYMENTREQUEST_0_AMT', array('label' => 'Amount', 'required' => true)); $itemSubForm->addElement('text', 'L_PAYMENTREQUEST_0_QTY', array('label' => 'Quantity', 'required' => true)); $customSubForm = new Qs_Form_SubForm(); $customSubForm->addElement('hidden', 'ADDROVERRIDE', array('value' => true)); $customSubForm->addElement('hidden', 'ALLOWNOTE', array('value' => false)); $customSubForm->addElement('text', 'PAYMENTREQUEST_0_SHIPTOSTREET', array('label' => 'Street Address', 'required' => true)); $customSubForm->addElement('text', 'PAYMENTREQUEST_0_SHIPTOCITY', array('label' => 'City', 'required' => true)); $customSubForm->addElement('text', 'PAYMENTREQUEST_0_SHIPTOSTATE', array('label' => 'State', 'required' => true)); $customSubForm->addElement('text', 'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE', array('label' => 'Country Code', 'required' => true)); $customSubForm->addElement('text', 'PAYMENTREQUEST_0_SHIPTOZIP', array('label' => 'Zip', 'required' => true)); $mainSubForm->addSubForm($customSubForm, 'custom'); $itemsForm = new Qs_Form_SubForm(); $itemsForm->addSubForm(clone $itemSubForm, '0'); $itemsForm->addSubForm(clone $itemSubForm, '1'); $mainSubForm->addSubForm($itemsForm, 'itemList'); $form->addSubForm($mainSubForm, 'data'); return $this; } protected function _doVerifyOrder() { echo 'ORDER VIRIFIED
Response:
' . print_r($_REQUEST, true) . '

'; exit(); } protected function _doCancelOrder() { echo 'ORDER CANCELED
Response:
' . print_r($_REQUEST, true) . '

'; exit(); } }