Example #1
0
  def start_purchase(self, item):
    purchase = model.Purchase( item=item, owner=item.owner, purchaser=users.get_current_user(), status='NEW', secret=util.random_alnum(16) )
    purchase.put()
    if settings.USE_IPN:
      ipn_url = "%s/ipn/%s/%s/" % ( self.request.host_url, purchase.key(), purchase.secret )
    else:
      ipn_url = None
    if settings.USE_CHAIN:
      seller_paypal_email = util.paypal_email(item.owner)
    else:
      seller_paypal_email = None
    pay = paypal.Pay( 
      item.price_dollars(), 
      "%sreturn/%s/%s/" % (self.request.uri, purchase.key(), purchase.secret), 
      "%scancel/%s/" % (self.request.uri, purchase.key()), 
      self.request.remote_addr,
      seller_paypal_email,
      ipn_url,
      shipping=settings.SHIPPING)

    purchase.debug_request = pay.raw_request
    purchase.debug_response = pay.raw_response
    purchase.paykey = pay.paykey()
    purchase.put()
    
    if pay.status() == 'CREATED':
      purchase.status = 'CREATED'
      purchase.put()
      return (True, pay)
    else:
      purchase.status = 'ERROR'
      purchase.put()
      return (False, pay)
Example #2
0
def add_product(name, email, product, brand, price, unity):
    purchase = model.Purchase(product=product,
                              brand=brand,
                              price=price,
                              unity=unity)
    purchase.user = model.User(name=name, email=email)
    purchase.put()
Example #3
0
    def set_ExpressPurchase(self, item):
        purchase = model.Purchase(item=item,
                                  owner=item.owner,
                                  purchaser=users.get_current_user(),
                                  status='NEW',
                                  secret=util.random_alnum(16))
        purchase.put()

        ipn_url = None

        seller_paypal_email = None

        returnurl = "%sreturn/%s/%s/" % (self.request.uri, purchase.key(),
                                         purchase.secret)
        cancelurl = "%scancel/%s/" % (self.request.uri, purchase.key())

        #        pay = paypal.ExpressPay(
        #            item.price_dollars(),
        #            returnurl,
        #            cancelurl,
        #            self.request.remote_addr,
        #            seller_paypal_email,
        #            ipn_url,
        #            shipping=settings.SHIPPING)

        self.signature_values = {
            'USER': settings.PAYPAL_USERID,
            'PWD': settings.PAYPAL_PASSWORD,
            'SIGNATURE': settings.PAYPAL_SIGNATURE,
            'VERSION': '65.0',
        }
        self.API_ENDPOINT = 'https://api-3t.sandbox.paypal.com/nvp'
        self.PAYPAL_URL = 'https://www.sandbox.paypal.com/webscr&cmd=_express-checkout'
        self.signature = urllib.urlencode(self.signature_values) + "&"

        params = {'METHOD': 'SetExpressCheckout'}
        expressParams = {
            'PAYMENTREQUEST_0_AMT': '%0.2f' % item.price_dollars(),
            'PAYMENTREQUEST_0_CURRENCYCODE': 'USD',
            'PAYMENTREQUEST_0_PAYMENTACTION': 'Sale',
            'RETURNURL': returnurl,
            'CANCELURL': cancelurl
        }
        params_string = self.signature + urllib.urlencode(
            params) + "&" + urllib.urlencode(expressParams)

        self.raw_response = urllib.urlopen(self.API_ENDPOINT, params_string)
        self.response_content = self.raw_response.read()

        logging.debug(self.request.uri)
        logging.debug(self.request.remote_addr)

        data = self.API_ENDPOINT + params_string
        purchase.debug_request = json.dumps(data)
        purchase.debug_response = self.response_content

        res = urllib.unquote(self.response_content)
        datares = dict(
            (n, v) for n, v in (a.split('=') for a in res.split('&')))
        logging.debug(datares)
        ret_url = ""
        if datares['ACK'] == 'Success':
            purchase.paykey = datares['TOKEN']
            purchase.status = 'CREATED'
            purchase.put()
            url_red = self.PAYPAL_URL + '&token=' + purchase.paykey
            logging.info('redirect to:%s' % url_red)
            ret_url = url_red
            #            secret = util.random_alnum(16)
            #            method_params = {
            #                'METHOD' : 'DoExpressCheckoutPayment'
            #                    }
            #            detail_params = {
            #                'TOKEN' : purchase.paykey,
            #                'PAYERID' : secret,
            #                'PAYMENTREQUEST_0_AMT' : '%0.2f' % item.price_dollars(),
            #                'PAYMENTREQUEST_0_CURRENCYCODE' : 'USD',
            #                'PAYMENTREQUEST_0_PAYMENTACTION' : 'Sale'
            #            }
            #            do_exp_params_string = self.signature + urllib.urlencode(method_params) + "&" + urllib.urlencode(detail_params)
            #            ret_url = self.PAYPAL_URL + "&" + do_exp_params_string
            #            logging.info('redirect to:  %s' % ret_url)
            return (True, ret_url)
        else:
            return (False, ret_url)