* * *

Author Topic: shopping cart or paypal software ?  (Read 311 times)

jack616

  • Full Member
  • ***
  • Posts: 239
shopping cart or paypal software ?
« on: August 30, 2017, 03:00:55 pm »
Has anyone written either a shopping cart or paypal code with lazarus?


Leledumbo

  • Hero Member
  • *****
  • Posts: 7649
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: shopping cart or paypal software ?
« Reply #1 on: August 30, 2017, 08:25:02 pm »
Here's an excerpt but working, standalone fpweb program that make a payment request via paypal after filling a (static size) cart:
Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   classes,fphttpapp,httpdefs,httpprotocol,httproute;
  5.  
  6. procedure Index(ARequest : TRequest; AResponse : TResponse);
  7. var
  8.   i: Integer;
  9.   s: String;
  10. begin
  11.   with AResponse.Contents do begin
  12.     Add('<html>');
  13.     Add('  <head>');
  14.     Add('    <title>Paypal Test Page</title>');
  15.     Add('  </head>');
  16.     Add('  <body>');
  17.     Add('    <h1>Paypal Test Page</h1>');
  18.     Add('    <form action="/pay" method="post">');
  19.     for i := 1 to 3 do begin
  20.         Str(i,s);
  21.         Add('      <label>Item</label><input type="text" name="item_name_' + s + '"/>');
  22.         Add('      <label>Price</label><input type="text" name="amount_' + s + '"/>');
  23.         Add('      <label>Quantity</label><input type="text" name="qty_' + s + '"/>');
  24.         Add('      <br />');
  25.     end;
  26.     Add('      <input type="submit" value="Checkout" />');
  27.     Add('    </form>');
  28.     Add('  </body>');
  29.     Add('</html>');
  30.   end
  31. end;
  32.  
  33. procedure Pay(ARequest : TRequest; AResponse : TResponse);
  34.  
  35.   function StringListToQueryString(sl: TStrings): String;
  36.   var
  37.     i: Integer;
  38.     OldDelimiter      : Char;
  39.     OldStrictDelimiter: Boolean;
  40.   begin
  41.     for i := 0 to sl.Count - 1 do
  42.       sl.ValueFromIndex[i] := HTTPEncode(sl.ValueFromIndex[i]);
  43.     OldDelimiter       := sl.Delimiter;
  44.     OldStrictDelimiter := sl.StrictDelimiter;
  45.     sl.Delimiter       := '&';
  46.     sl.StrictDelimiter := true;
  47.     Result             := sl.DelimitedText;
  48.     sl.Delimiter       := OldDelimiter;
  49.     sl.StrictDelimiter := OldStrictDelimiter;
  50.   end;
  51.  
  52. var
  53.   PaypalParams: String;
  54. begin
  55.   with ARequest.ContentFields do begin
  56.     Values['business']             := 'put.your@email.here';
  57.     Values['cmd']                  := '_cart';
  58.     Values['upload']               := '1';
  59.     Values['currency_code']        := 'USD';
  60.     Values['charset']              := 'utf-8';
  61.     Values['custom']               := 'any custom data you wish to be sent back from notification, optional of course';
  62.     Values['notify_url']           := 'http://where.paypal.should/fire/back/for/notifying/you';
  63.     Values['return']               := 'http://where.paypal.should/redirect/user/after/payment';
  64.     Values['shipping_1']           := 'the chosen shipping method';
  65.     Values['discount_amount_cart'] := '0';
  66.     Values['tax_cart']             := '0';
  67.     Values['first_name']           := 'receiver first name';
  68.     Values['last_name']            := 'receiver last name';
  69.     Values['address1']             := 'address line 1';
  70.     Values['address2']             := 'address line 2';
  71.     Values['zip']                  := '12345';
  72.     Values['city']                 := 'Depok';
  73.     Values['state']                := 'West Java';
  74.     Values['country']              := 'Indonesia';
  75.     Values['night_phone_b']        := '021-112911388';
  76.     Values['email']                := 'receiver@email.address';
  77.   end;
  78.   PaypalParams       := StringListToQueryString(ARequest.ContentFields);
  79.   AResponse.Code     := 301;
  80.   AResponse.CodeText := 'Redirect';
  81.   AResponse.Location := 'https://www.paypal.com/cgi-bin/webscr?' + PaypalParams;
  82. end;
  83.  
  84. begin
  85.   HTTPRouter.RegisterRoute('/',rmAll,@Index);
  86.   HTTPRouter.RegisterRoute('/pay',rmAll,@Pay);
  87.   Application.Port := 9999;
  88.   Application.Initialize;
  89.   Application.Run;
  90. end.
  91.  
Note that I use the new routing system introduced in FPC trunk, please rewrite in webmodule system if you're using latest stable (3.0.2).
« Last Edit: August 30, 2017, 08:35:56 pm by Leledumbo »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus