<commit_before>/*global
  Members, Payments
*/

/**
 * PaymentsController
 *
 *
 *  Braintree:
 *      - client side (https://developers.braintreepayments.com/javascript+ruby/start/hello-client)
 *      - server side (https://developers.braintreepayments.com/ios+node/start/hello-server)
 *
**/

var braintree = require('braintree')
var Validation = require('../services/validate.js')

// ATTENTION: sandbox credentials: need real credentials and must be kept PRIVATE
var BraintreeGateway = braintree.connect({
  environment: braintree.Environment.Sandbox,
  merchantId: 'rk34hgxrsz8z28y9', // process.env.BRAINTREE_MERCHANT_ID
  publicKey: '<KEY>', // process.env.BRAINTREE_PUBLIC_KEY
  privateKey: 'f96014be14f9b260c692e46a4d8ea1ca' // process.env.BRAINTREE_PRIVATE_KEY
})

module.exports = {
  charge: function (req, res) {
    var payment = req.body
    payment.date = new Date()
    payment.member = req.session.user.id

    Validation('payment', payment, function (errorValidation) {
      if (errorValidation) {
        return res.badRequest({error: errorValidation})
      }

      Payments
        .create(payment)
        .exec(function (error, item) {
          if (error) {
            return res.badRequest({error: error})
          } else {
            return res.send(item)
          }
        })
    })
  },
  clientToken: function (req, res) {
    BraintreeGateway
      .clientToken
      .generate({
        merchantAccountId: 'rk34hgxrsz8z28y9'
      }, function (err, response) {
        if (err) {
          res.badRequest({error: err})
        } else {
          res.send({token: response.clientToken})
        }
      })
  },
  /**
   *  Payment
   *
   *
   *
  **/
  makePaypalPayment: function (req, res) {
    Validation('payment', req.body, function (errorValidation) {
      if (errorValidation) {
        return res.badRequest({error: errorValidation})
      }

      BraintreeGateway
        .transaction
        .sale({
          amount: req.body.amount || '0',
          paymentMethodNonce: req.body.payment_method_nonce
        }, function (err, result) {
          if (err) {
            res.badRequest({error: err})
          } else {
            Members
              .findOne(req.session.user.id)
              .then(function (member) {
                // console.log("RESULT", result)

                var paymentRecord = {
                  category: 'payment',
                  description: 'Payment by ' + result.transaction.paymentInstrumentType,
                  type: 'PAYPAL',
                  amount: result.transaction.amount,
                  member: member.id,
                  date: new Date()
                }

                return Payments.create(paymentRecord)
              }).then(function (payment) {
              // console.log("PAYMENT", payment)

              return res.redirect('/')
            }).catch(function (err) {
              // console.log("ERROR", err)

              return res.badRequest({error: err})
            })
          }
        })
    })
  },

  payingInReport: function (req, res) {
    Payments.query
      ( 'select p.*, m.last_name from payments p, members m' +
        '  where exists (' +
        '    select 1 from payments p2' +
        '      where p2.reference = ?' +
        '        AND p2.member = p.member' +
        '        AND p2.date >= p.date' +
        '  )' +
        '  AND p.member = m.id' +
        '  order by p.date;'
      , [req.params.ref]
      , function (err, results) {
          if (err) res.badRequest({ error: err })
          else res.send(results)
        }
      )
  }
}
<commit_msg>ensures payments come last in a day<commit_after>/*global
  Members, Payments
*/

/**
 * PaymentsController
 *
 *
 *  Braintree:
 *      - client side (https://developers.braintreepayments.com/javascript+ruby/start/hello-client)
 *      - server side (https://developers.braintreepayments.com/ios+node/start/hello-server)
 *
**/

var braintree = require('braintree')
var Validation = require('../services/validate.js')

// ATTENTION: sandbox credentials: need real credentials and must be kept PRIVATE
var BraintreeGateway = braintree.connect({
  environment: braintree.Environment.Sandbox,
  merchantId: 'rk34hgxrsz8z28y9', // process.env.BRAINTREE_MERCHANT_ID
  publicKey: '<KEY>', // process.env.BRAINTREE_PUBLIC_KEY
  privateKey: 'f96014be14f9b260c692e46a4d8ea1ca' // process.env.BRAINTREE_PRIVATE_KEY
})

module.exports = {
  charge: function (req, res) {
    var payment = req.body
    payment.date = new Date()
    payment.member = req.session.user.id

    Validation('payment', payment, function (errorValidation) {
      if (errorValidation) {
        return res.badRequest({error: errorValidation})
      }

      Payments
        .create(payment)
        .exec(function (error, item) {
          if (error) {
            return res.badRequest({error: error})
          } else {
            return res.send(item)
          }
        })
    })
  },
  clientToken: function (req, res) {
    BraintreeGateway
      .clientToken
      .generate({
        merchantAccountId: 'rk34hgxrsz8z28y9'
      }, function (err, response) {
        if (err) {
          res.badRequest({error: err})
        } else {
          res.send({token: response.clientToken})
        }
      })
  },
  /**
   *  Payment
   *
   *
   *
  **/
  makePaypalPayment: function (req, res) {
    Validation('payment', req.body, function (errorValidation) {
      if (errorValidation) {
        return res.badRequest({error: errorValidation})
      }

      BraintreeGateway
        .transaction
        .sale({
          amount: req.body.amount || '0',
          paymentMethodNonce: req.body.payment_method_nonce
        }, function (err, result) {
          if (err) {
            res.badRequest({error: err})
          } else {
            Members
              .findOne(req.session.user.id)
              .then(function (member) {
                // console.log("RESULT", result)

                var paymentRecord = {
                  category: 'payment',
                  description: 'Payment by ' + result.transaction.paymentInstrumentType,
                  type: 'PAYPAL',
                  amount: result.transaction.amount,
                  member: member.id,
                  date: new Date()
                }

                return Payments.create(paymentRecord)
              }).then(function (payment) {
              // console.log("PAYMENT", payment)

              return res.redirect('/')
            }).catch(function (err) {
              // console.log("ERROR", err)

              return res.badRequest({error: err})
            })
          }
        })
    })
  },

  payingInReport: function (req, res) {
    Payments.query
      ( 'select p.*, m.last_name from payments p, members m' +
        '  where exists (' +
        '    select 1 from payments p2' +
        '      where p2.reference = ?' +
        '        AND p2.member = p.member' +
        '        AND p2.date >= p.date' +
        '  )' +
        '  AND p.member = m.id' +
        '  order by p.date' +
        "  , field(p.category, 'donation', 'event', 'subscription', 'payment');"
      , [req.params.ref]
      , function (err, results) {
          if (err) res.badRequest({ error: err })
          else res.send(results)
        }
      )
  }
}

{'fraction_non_alphanumeric': 0.12714636140637775, 'fraction_numerical': 0.010084491687108205, 'mean_word_length': 1.819439108720707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1671834', 'n_tokens_mistral': 2263, 'n_tokens_neox': 2183, 'n_words': 429}