表单提交JSON / Rest / API(Bloomerang)

时间:2015-10-28 18:41:59

标签: javascript json forms api

在过去的一天半里,我一直在旋转,试图找出这种形式的挂断方式。我继承了我们最初为他们构建的客户端的一些自定义代码。我发现了许多我已经修复过的洞和东西,他们以前没有注意到。关于如何提交数据以及如何在捐赠平台Bloomerang(api docs for reference)的后端显示数据仍存在一个问题

现在看来,如果表单填写两者已选中感恩篮+采用选项,则采用信息将在Bloomerang后端显示为“实物”并带有相应的#收养家庭出现在票据领域,但感恩节选项不会。如果我只填写其中一个选项,则转移到Bloomerang后端的信息不会将该项目标记为“实物”捐赠,也不会将#text字段内容转移到bloomerang中后端。

完整代码包含在下面。我没有包含演示网址,因为这是一个“实时”表单。同样,我继承了这段代码并且没有大量的JSON经验,并且在我的生活中无法弄清楚为什么 all 信息将无法在所有实例中成功完成。< / p>

<div class="custom-donation-widget" id="custom-donation-widget">
  <script src="https://crm.bloomerang.co/Content/Scripts/Api/Bloomerang.js" type="text/javascript"></script>
  <script type="text/javascript">
     var firstSubmit = "true";
     var submitCount;
     var tbasketSubmit = "false";
     var familySubmit = "false";
     var donationSubmit = "false";

     (function() {
         // Connect to correct database
         Bloomerang.useKey("pub_cf958da2-0fee-11e3-a756-02a718d18e56"); 


         Bloomerang.Util.requireJQueryValidation(function () {
             // Once the form validates, submit the data through the API (will cause OnSubmit() to be called)
             jQuery("#donation-form").validate({ submitHandler: function() {
                 if(jQuery("input[name='ThanksGiving']:checked").val() && jQuery("input[name='AdoptAFamily']:checked").val() && jQuery("input[name='GivingLevel']:checked").val() && firstSubmit == "true"){
                     submitCount = 2;
                 }else if(jQuery("input[name='ThanksGiving']:checked").val() && jQuery("input[name='AdoptAFamily']:checked").val() && firstSubmit == "true"){
                     submitCount = 1;
                 }else if(jQuery("input[name='ThanksGiving']:checked").val() && jQuery("input[name='GivingLevel']:checked").val() && firstSubmit == "true"){
                     submitCount = 1;
                 }else if(jQuery("input[name='AdoptAFamily']:checked").val() && jQuery("input[name='GivingLevel']:checked").val() && firstSubmit == "true"){
                     submitCount = 1;
                 }else if(jQuery("input[name='ThanksGiving']:checked").val() && firstSubmit == "true"){
                     submitCount = 0;
                 }else if(jQuery("input[name='AdoptAFamily']:checked").val() && firstSubmit == "true"){
                     submitCount = 0;
                 }else if (jQuery("input[name='GivingLevel']:checked").val() && firstSubmit == "true"){
                     submitCount = 0;
                 };

                 if(jQuery("input[name='GivingLevel']:checked").val() && jQuery("input[name='Rcurring']:checked").val() && submitCount == 0 && donationSubmit == "false") {
                    return Bloomerang.Api.recurringDonate();
                 }
                 else {
                    return Bloomerang.Api.donate();
                 }

             }});

             // Callback for submission of API call to build the data to send to Bloomerang (auto-called via DoSubmit())
             // Note: You can cancel submission by returning false from this function
             Bloomerang.Api.OnSubmit = function (args) {
                 // Change the "Donate" button text and disable it so you don't accidentally submit multiple times
                 jQuery("#submit").prop("disabled", true).val("(Processing, One Moment...)");
                 // Set the name and contact/billing info on the Constituent record
                 if(jQuery("#donation-type").val() == "organization"){
                    Bloomerang.Account
                        .organization()
                        .organizationName(jQuery("#company").val())
                        .firstName(jQuery("#first-name").val())
                        .middleName(jQuery("#middle-name").val())
                        .lastName(jQuery("#last-name").val())
                        .homeAddress(
                           [jQuery("#address1").val(), jQuery("#address2").val()].join("\n"),
                           jQuery("#city").val(),
                           jQuery("#state").val(),
                           jQuery("#zip").val())
                        .homeEmail(jQuery("#email").val())
                        .homePhone(jQuery("#phone").val());    
                 }else {
                    Bloomerang.Account
                        .individual()
                        .firstName(jQuery("#first-name").val())
                        .middleName(jQuery("#middle-name").val())
                        .lastName(jQuery("#last-name").val())
                        .homeAddress(
                           [jQuery("#address1").val(), jQuery("#address2").val()].join("\n"),
                           jQuery("#city").val(),
                           jQuery("#state").val(),
                           jQuery("#zip").val())
                        .homeEmail(jQuery("#email").val())
                        .homePhone(jQuery("#phone").val());   
                 };

                 if(jQuery("input[name='ThanksGiving']:checked").val() && tbasketSubmit == "false")
                 {
                     tbasketSubmit = "true";
                     if(jQuery("input[id='ThanksBasket']:checked").val()){
                        // Fill in the In-Kind info 
                        Bloomerang.InKind
                             .goods()
                             .description(jQuery("#ThanksBasket-amount").val() + " " + jQuery("#ThanksBasket").val());  
                        Bloomerang.Donation
                             .amount(0)
                             .fund("Thanksgiving Basket")
                             .appeal("C&F Website");
                    }else if(jQuery("input[id='ThanksGiftCard']:checked").val()){
                        // Fill in the In-Kind info
                        Bloomerang.InKind
                             .goods()
                             .description(jQuery("#ThanksGiftCard").val());
                        Bloomerang.Donation
                             .amount(0)
                             .fund("Thanksgiving Basket")
                             .appeal("C&F Website");
                    }else if(jQuery("input[id='ThanksCheck']:checked").val()){
                        Bloomerang.Check
                             .number("0000");
                        Bloomerang.Donation
                             .amount(0)
                             .fund("Thanksgiving Basket")
                             .appeal("C&F Website");
                    };
                 }
                 else if(jQuery("input[name='AdoptAFamily']:checked").val() && familySubmit == "false")
                 {
                    familySubmit = "true";
                    if(jQuery("input[id='AdoptAFamily']:checked").val()){;
                        Bloomerang.InKind
                            .goods()
                            .description(jQuery("#AdoptChildren-amount").val() + " " + jQuery("#AdoptAFamily").val());
                        Bloomerang.Donation
                            .amount(0)
                            .fund("Adopt a Family")
                            .appeal("C&F Website")
                            .note(jQuery("#special-consideration").val());
                    }else if(jQuery("input[id='AdoptGiftCard']:checked").val()){
                        Bloomerang.InKind
                            .goods()
                            .description(jQuery("#AdoptGiftCard").val());
                        Bloomerang.Donation
                            .amount(0)
                            .fund("Adopt a Family")
                            .appeal("C&F Website")
                            .note(jQuery("#special-consideration").val());
                    }else if(jQuery("input[id='AdoptCheck']:checked").val()){
                        Bloomerang.Check
                             .number("0000");
                        Bloomerang.Donation
                            .amount(0)
                            .fund("Adopt a Family")
                            .appeal("C&F Website")
                            .note(jQuery("#special-consideration").val());
                    };
                 }
                 else if (jQuery("input[name='GivingLevel']:checked").val() && donationSubmit == "false")
                 {
                    donationSubmit = "true";
                     // Fill in the credit card info
                     Bloomerang.CreditCard
                         .number(jQuery("#card-number").val())
                         .securityCode(jQuery("#cvv2").val())
                         .expirationMonth(jQuery("#expires-month").val())
                         .expirationYear(jQuery("#expires-year").val());
                     // Build a single or recurring donation based on the checkbox
                     if(jQuery("input[name='Rcurring']:checked").val()) {
                         Bloomerang.RecurringDonation
                            .monthly()
                            .amount(jQuery("#custom-amount").val())
                            .fund("Philanthropy Fund")
                            .appeal("C&F Website");
                     } 
                     else {
                         Bloomerang.Donation
                            .amount(jQuery("#custom-amount").val())
                            .fund("Philanthropy Fund")
                            .appeal("C&F Website");
                     };
                 };
             };

             // When the charge goes through successfully
             Bloomerang.Api.OnSuccess = function (response) {
                 if (submitCount > 0) {
                     submitCount--;
                     firstSubmit = "false";
                     if(jQuery("input[name='Rcurring']:checked").val() && jQuery("input[name='GivingLevel']:checked").val() && submitCount == 0 && donationSubmit == "false") {
                         setTimeout(function() { Bloomerang.Api.recurringDonate(); }, 500); 
                     }
                     else {
                         setTimeout(function() { Bloomerang.Api.donate(); }, 500);
                     }
                 }
                 else {
                    //may need to add submitStage == 3 if form does not submit all entries
                     jQuery("#donation-form").hide();
                     jQuery("#success-message").show();
                 };
             };
             // When the server responds with an error
             Bloomerang.Api.OnError = function (response) {
                 jQuery("#submit").prop("disabled", false).val("Donate");
                 jQuery("#donation-errors").show().find(".message").html(response.Message);
                 jQuery("#donation-form :input:visible:first").focus();
             };

         });   // end requireJQueryValidation()

     })();     // end (function() {
  </script>
<form name="event-form" class="event-form" id="donation-form" method="post" action="javascript:Bloomerang.Api.DoSubmit()">
     <div class="errors" id="donation-errors"style="display:none;">
       <div class="message">The card number is invalid</div>
     </div>

     <!-- ===== HOLIDAY GIVING ======= -->
     <div class="section donation-program thanksgiving-basket">
        <h2>Thanksgiving Basket Program</h2>
        <p><input class="form-radio" type="radio" name="ThanksGiving" id="ThanksBasket" value="Thanksgiving Baseket" /><span class="select-label"> I/we would like to participate in the Thanksgiving Basket Program</span></p>
        <p class="basket-number"><input type="text" name="ThanksBaskets" id="ThanksBasket-amount" class="basket" /><span class="input-label"># of Baskets you are able to provide</span></p>
        <p><input class="form-radio" type="radio" name="ThanksGiving" id="ThanksGiftCard" value="Thanksgiving Gift Card" /><span class="select-label">I/we would like to provide a donation of Gift cards to Stop & Shop or Shaw's</span></p>
        <p><em>Please drop off at Child & Family to the attention of our Volunteer Program</em></p>
        <p><input class="form-radio" type="radio" name="ThanksGiving" id="ThanksCheck" value="Thanksgiving Check" /><span class="select-label">I/we would like to provide a donation of a Check</span></p>
        <p><em>Please make out to Child & Family with a notation of, "Thanksgiving Program" and mail to Child & Family.</em></p>
     </div>
     <div class="section donation-program adopt-family">
        <h2>Adopt-A-Family Program</h2>
        <p><input class="form-radio"  type="radio" name="AdoptAFamily" id="AdoptAFamily" value="Adopt-A-Family" /><span class="select-label">I/we would like to participate in the Adopt-A-Family Program</span></p>
        <p class="basket-number"><input type="text" name="AdoptChildren" id="AdoptChildren-amount" class="basket" /><span class="input-label"># of children that you are able to provide for.</span></p>
        <p><input  class="form-radio" type="radio" name="AdoptAFamily" id="AdoptGiftCard" value="Holiday Program Gift Card" /><span class="select-label">I/we would like to provide a donation of Gift cards to Stop & Shop, Shaw's, Walmart or other local, accessible stores.</span></p>
        <p><em>Please drop off at Child & Family to the attention of our Volunteer Program</em></p>
        <p><input class="form-radio"  type="radio" name="AdoptAFamily" id="AdoptCheck" value="Holiday Program Check" /><span class="select-label">I/we would like to provide a donation of a Check</span></p>
        <p><em>Please make out to Child & Family with a notation of, "Holiday Program" and mail to Child & Family.</em></p>
        <div class="field special-consideration">
            <label for="special-consideration">Adopt-A-Family Special Considerations</label>
            <textarea id="special-consideration" name="special-consideration" type="text" value="" /> </textarea>
            <span class="info">Please tell us of any restrictions or considerations you may have.</span>
        </div>
     </div>

     <!-- ===== NAME INFO ======= -->
     <div class="section address-info">
     <h2>Contact Information</h2>
        <div class="field donation-type required">
         <label for="donation-type">Donation Type <span class="required-star">*</span></label>
         <select id="donation-type" name="DonationType" class="required">        
           <option value="">Select One</option>        
           <option value="individual">Individual</option>
           <option value="organization">Organization</option>
          </select>
       </div>
       <div class="field first-name required">
         <label for="first-name">First Name <span class="required-star">*</span></label>
         <input id="first-name" name="FirstName" type="text" value="" class="required" />
       </div>

       <div class="field middle-name">
         <label for="middle-name">Middle Name</label>
         <input id="middle-name" name="MiddleName" type="text" value="" />
       </div>

       <div class="field last-name required">
         <label for="last-name">Last Name <span class="required-star">*</span></label>
         <input id="last-name" name="LastName" type="text" value="" class="required" />
       </div>
       <div class="field company">
         <label for="company">Company / Organization</label>
         <input id="company" name="Company" type="text" value="" />
       </div>
     <!-- ===== CONTACT INFO ======= -->
       <div class="field address1 required">
         <label for="address1">Address Line 1 <span class="required-star">*</span></label>
         <input id="address1" name="Address1" type="text" value="" class="required" />
       </div>

       <div class="field address2">
         <label for="address2">Address Line 2</label>
         <input id="address2" name="Address2" type="text" value="" />
       </div>

       <div class="field city required">
         <label for="city">City <span class="required-star">*</span></label>
         <input id="city" name="City" type="text" value="" class="required" />
       </div>

       <div class="field state required">
         <label for="state">State/Province <span class="required-star">*</span></label>
         <select id="state" name="State" class="required">        
           <option value=""></option>        
           <option value="AL">Alabama</option>        
           <option value="AK">Alaska</option>        
           <option value="AZ">Arizona</option>        
           <option value="AR">Arkansas</option>        
           <option value="CA">California</option>        
           <option value="CO">Colorado</option>        
           <option value="CT">Connecticut</option>        
           <option value="DC">District of Columbia</option>        
           <option value="DE">Delaware</option>        
           <option value="FL">Florida</option>        
           <option value="GA">Georgia</option>        
           <option value="HI">Hawaii</option>        
           <option value="ID">Idaho</option>        
           <option value="IL">Illinois</option>        
           <option value="IN">Indiana</option>        
           <option value="IA">Iowa</option>        
           <option value="KS">Kansas</option>        
           <option value="KY">Kentucky</option>        
           <option value="LA">Louisiana</option>        
           <option value="ME">Maine</option>        
           <option value="MD">Maryland</option>        
           <option value="MA">Massachusetts</option>        
           <option value="MI">Michigan</option>        
           <option value="MN">Minnesota</option>        
           <option value="MS">Mississippi</option>        
           <option value="MO">Missouri</option>        
           <option value="MT">Montana</option>        
           <option value="NE">Nebraska</option>        
           <option value="NV">Nevada</option>        
           <option value="NH">New Hampshire</option>        
           <option value="NJ">New Jersey</option>        
           <option value="NM">New Mexico</option>        
           <option value="NY">New York</option>        
           <option value="NC">North Carolina</option>        
           <option value="ND">North Dakota</option>        
           <option value="OH">Ohio</option>        
           <option value="OK">Oklahoma</option>        
           <option value="OR">Oregon</option>        
           <option value="PA">Pennsylvania</option>        
           <option value="RI">Rhode Island</option>        
           <option value="SC">South Carolina</option>        
           <option value="SD">South Dakota</option>        
           <option value="TN">Tennessee</option>        
           <option value="TX">Texas</option>        
           <option value="UT">Utah</option>        
           <option value="VT">Vermont</option>        
           <option value="VA">Virginia</option>        
           <option value="WA">Washington</option>        
           <option value="WV">West Virginia</option>        
           <option value="WI">Wisconsin</option>        
           <option value="WY">Wyoming</option>
           <optgroup label="US Territories">          
             <option value="AS">American Samoa</option>          
             <option value="GU">Guam</option>          
             <option value="PR">Puerto Rico</option>          
             <option value="VI">US Virgin Islands</option>
           </optgroup>
           <optgroup label="Canada">          
             <option value="AB">Alberta</option>          
             <option value="BC">British Columbia</option>          
             <option value="MB">Manitoba</option>          
             <option value="NL">Newfoundland and Labrador</option>          
             <option value="NB">New Brunswick</option>          
             <option value="NS">Nova Scotia</option>          
             <option value="ON">Ontario</option>          
             <option value="PE">Prince Edward Island</option>          
             <option value="QC">Quebec</option>          
             <option value="SK">Saskatchewan</option>
           </optgroup>

           <optgroup label="M&#233;xico">
             <option value="AGU">Aguascalientes</option>
             <option value="BCN">Baja California</option>
             <option value="BCS">Baja California Sur</option>
             <option value="CAM">Campeche</option>
             <option value="CHP">Chiapas</option>
             <option value="CHH">Chihuahua</option>
             <option value="COA">Coahuila</option>
             <option value="COL">Colima</option>
             <option value="DIF">Distrito Federal</option>
             <option value="DUR">Durango</option>
             <option value="GUA">Guanajuato</option>
             <option value="GRO">Guerrero</option> 
             <option value="HID">Hidalgo</option> 
             <option value="JAL">Jalisco</option> 
             <option value="MEX">Estado de M&#233;xico</option> 
             <option value="MIC">Michoac&#225;n</option> 
             <option value="MOR">Morelos</option> 
             <option value="NAY">Nayarit</option> 
             <option value="NLE">Nuevo Le&#243;n</option> 
             <option value="OAX">Oaxaca</option> 
             <option value="PUE">Puebla</option> 
             <option value="QUE">Quer&#233;taro</option> 
             <option value="ROO">Quintana Roo</option> 
             <option value="SLP">San Luis Potos&#237;</option> 
             <option value="SIN">Sinaloa</option> 
             <option value="SON">Sonora</option> 
             <option value="TAB">Tabasco</option> 
             <option value="TAM">Tamaulipas</option> 
             <option value="TLA">Tlaxcala</option> 
             <option value="VER">Veracruz</option> 
             <option value="YUC">Yucat&#225;n</option> 
             <option value="ZAC">Zacatecas</option> 
           </optgroup>
         </select>
       </div>

       <div class="field zip required">
         <label for="zip">Zip Code <span class="required-star">*</span></label>
         <input id="zip" name="Zip" type="text" value="" class="required" />
       </div>

       <div class="field email required">
         <label for="email">Email <span class="required-star">*</span></label>
         <input id="email" name="Email" type="text" value="" class="required" />
       </div>

       <div class="field phone required">
         <label for="phone">Phone <span class="required-star">*</span></label>
         <input id="phone" name="Phone" type="text" value="" class="required" />
       </div>
     </div><!-----------END OF ADDRESS INFO-------------------->
     <div class="make-difference">
          <div class="section pledge">       
<figure class="pledge-image"><img src="/sites/default/themes/ChildFamily/assets/images/holiday-giving-image.png" alt="make a pledge" title="make a pledge" /></figure>
<div class="pledge-font">Before you go</div>
 <h2>Here's another opportunity to make a difference.</h2>
            <p>We're so grateful for your participation in this year's Holiday Giving Program. If you'd like to continue making a difference throughout the year, please consider our one year pledge program.  Your pledge or a one-time donation will help people build better lives for themselves and their families. It's a gift that continues to give long after the holidays are over!</p>
          </div>

<!-- ===== DONATION AMOUNT ======= -->
          <div class="section amounts">
             <div class="field giving-level"><br>
               <p>
                 <input class="form-checkbox" type="checkbox" name="GivingLevel" id="level-none" value="0.0" /><span class="select-label">I would also like to donate the following amount</span>
                 <input type="text" name="CustomAmount" id="custom-amount" placeholder="$0.00" />
               </p>

               <p><input class="form-checkbox" type="checkbox" name="Rcurring" id="recurring" value="true" /><span class="select-label">Show even more support by making this a monthly gift</span></p>
             </div>

           <!-- ===== CREDIT CARD INFO ======= -->
             <div class="card-info">


             <div class="field tel cardNumber">
<label for="cardNumber">Card Number</label>
<input id="cardNumber" name="cardNumber" class="creditcard" type="tel"></input>
</div>
<div class="field  cardExpirationMonth">
<label for="cardExpirationMonth">Expiration Month</label>
<select id="cardExpirationMonth" name="cardExpirationMonth"><option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
</div>
<div class="field text cardExpirationYear">
<label for="cardExpirationYear">Expiration Year</label>
<select id="cardExpirationYear" name="cardExpirationYear"><option value=""></option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
<option value="2025">2025</option>
<option value="2026">2026</option>
<option value="2027">2027</option>
<option value="2028">2028</option>
<option value="2029">2029</option>
<option value="2030">2030</option>
<option value="2031">2031</option>
<option value="2032">2032</option>
<option value="2033">2033</option>
<option value="2034">2034</option>
<option value="2035">2035</option>
<option value="2036">2036</option>
<option value="2037">2037</option>
<option value="2038">2038</option>
<option value="2039">2039</option>
<option value="2040">2040</option>
<option value="2041">2041</option>
<option value="2042">2042</option>
<option value="2043">2043</option>
</select>
</div>
<div class="field tel security-code">
<label for="security-code">Security Code</label>
<input id="security-code" name="security-code" class="number" type="tel"></input>
</div>




             </div>
         </div> <!----END OF AMOUNTS------>
     </div>
<input type="submit" id="submit" class="submit" value="Donate" />
  </form>

  <div class="success-message" id="success-message" style="display:none;">
    <h2>Thanks for Your Donation!</h2>
    <p>If you have signed up for the Thanksgiving Basket Program, please follow the instructions on the website for delivery. See you here! Thanks.</p>
    <p>If you have signed up for the Adopt A Family Program, we will be in touch by e-mail when matches are made and further instructions will follow then. Please bear with us as this is a complex process! Thanks.</p>
    <p>Thanks for your Donation! Your generous gift has been processed. We truly appreciate your support.</p>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

您似乎正在为几个Bloomerang.Donation对象提交零美元金额。公共api端点似乎不允许这样做(在文档中没有出现但是在尝试提交$ 0事务时我遇到了错误,这是我唯一改变的事情。)