Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mikebobadilla/33af5c72b2e2f5f7667f to your computer and use it in GitHub Desktop.
Save mikebobadilla/33af5c72b2e2f5f7667f to your computer and use it in GitHub Desktop.

Revisions

  1. Julien Le Coupanec revised this gist Oct 24, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions 1-meteor-paypal-api-rest-server-file.coffee
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@

    # The following code must be only run on the server!


    # 1. Create your paypal configuration object.


  2. Julien Le Coupanec revised this gist Jun 6, 2014. 1 changed file with 7 additions and 9 deletions.
    16 changes: 7 additions & 9 deletions 1-meteor-paypal-api-rest-server-file.coffee
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,12 @@
    ###
    This gist illustrates how to create and execute a payment with Paypal using their REST API.
    For additional informations, check the documentation: https://developer.paypal.com/docs/api/
    # This gist illustrates how to create and execute a payment with Paypal using their REST API.
    # For additional informations, check the documentation: https://developer.paypal.com/docs/api/

    Note 1: I assume that you have already created a developer account for Paypal and an application.
    To test that your code is working, use the sandbox accounts.
    https://developer.paypal.com/webapps/developer/applications/accounts
    # Note 1: I assume that you have already created a developer account for Paypal and an application.
    # To test that your code is working, use the sandbox accounts.
    # https://developer.paypal.com/webapps/developer/applications/accounts

    Note 2: we will not use the Paypal REST API SDK package for Node.js
    https://github.com/paypal/rest-api-sdk-nodejs
    ###
    # Note 2: we will not use the Paypal REST API SDK package for Node.js
    # https://github.com/paypal/rest-api-sdk-nodejs

    # The following code must be only run on the server!

  3. Julien Le Coupanec revised this gist May 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1-meteor-paypal-api-rest-server-file.coffee
    Original file line number Diff line number Diff line change
    @@ -125,7 +125,7 @@ Meteor.methods
    payment = res.data
    payment['userId'] = @userId

    if payment.state is 'approved'
    if payment.state in ['approved' , 'pending'] # Be careful, in production the payment state is "pending"
    # we insert the sucessful payment here
    PaypalPayments.insert payment

  4. Julien Le Coupanec renamed this gist May 4, 2014. 1 changed file with 0 additions and 0 deletions.
  5. Julien Le Coupanec revised this gist May 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-meteor-paypal-api-rest-client-file.coffee
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # The following code must be accessible run to the client.
    # The following code must be accessible to the client.


    # 4. We configure Iron Router to handle the different payment steps.
  6. Julien Le Coupanec revised this gist May 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1-meteor-paypal-api-rest-server-file.coffee
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ###
    This gist illustrates how to create and execute a payment with Paypal using their REST API.
    If you need something more, check the documentation: https://developer.paypal.com/docs/api/
    For additional informations, check the documentation: https://developer.paypal.com/docs/api/
    Note 1: I assume that you have already created a developer account for Paypal and an application.
    To test that your code is working, use the sandbox accounts.
  7. Julien Le Coupanec revised this gist May 4, 2014. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions 3--meteor-paypal-api-rest-client-file.html
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,10 @@
    <!-- 6. The Dashboard Template -->


    <template name="DashboardRoot">
    <span class="buy-product">Buy this product</span>
    </template>


    <!-- 7. The Dashboard Payment Paypal Execute Template -->


    <template name="DashboardPaymentPaypalExecute">
    <span class="loading">Wait a few seconds, we are processing your payment.</span>
    </template>
  8. Julien Le Coupanec revised this gist May 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 3--meteor-paypal-api-rest-client-file.html
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,6 @@
    <!-- 7. The Dashboard Payment Paypal Execute Template -->


    <template name="DashboardRoot">
    <template name="DashboardPaymentPaypalExecute">
    <span class="loading">Wait a few seconds, we are processing your payment.</span>
    </template>
  9. Julien Le Coupanec revised this gist May 4, 2014. 3 changed files with 55 additions and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@
    https://github.com/paypal/rest-api-sdk-nodejs
    ###

    # The following code must be only run on the server!

    # 1. Create your paypal configuration object.

    40 changes: 40 additions & 0 deletions 2-meteor-paypal-api-rest-client-file.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    # The following code must be accessible run to the client.


    # 4. We configure Iron Router to handle the different payment steps.
    # For more informations: https://github.com/EventedMind/iron-router


    Router.map ->
    @route 'DashboardRoot',
    path: '/dashboard/'

    @route 'DashboardPaymentPaypalExecute',
    path: '/dashboard/payment/paypal/execute/'


    # 5. Templates Events.


    Template.DashboardRoot.events
    'click .buy-product': ->
    product =
    name: 'product name'
    description: 'production description'
    price: 30.00

    Meteor.call 'createPaypalPayment', product, (err, res) ->
    window.location.replace res.links[1].href


    Template.DashboardPaymentPaypalExecute.created = ->
    payerId = window.location.search.split('PayerID=')[1]

    Meteor.call 'executePaypalPayment', payerId, (err, res) ->
    if res is true
    console.log 'Your payment has been successfully executed.'

    else
    console.log 'Your payment has been refused.'

    Router.go 'DashboardRoot'
    14 changes: 14 additions & 0 deletions 3--meteor-paypal-api-rest-client-file.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <!-- 6. The Dashboard Template -->


    <template name="DashboardRoot">
    <span class="buy-product">Buy this product</span>
    </template>


    <!-- 7. The Dashboard Payment Paypal Execute Template -->


    <template name="DashboardRoot">
    <span class="loading">Wait a few seconds, we are processing your payment.</span>
    </template>
  10. Julien Le Coupanec revised this gist May 4, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions meteor-paypal-api-rest-server-file.coffee
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@

    # 1. Create your paypal configuration object.


    @paypalConf =
    host: "api.sandbox.paypal.com"
    clientId: "pHQcZ1cH0lnX5Mub4MGzo_-FH6witB3_2zuRYgvUFxMHFH6wiAe3zCRDqatu3"
    @@ -21,6 +22,7 @@

    # 2. Create two collections to save our payments and the Paypal tokens.


    @PaypalPayments = new Meteor.Collection 'paypal_payments'
    @PaypalTokens = new Meteor.Collection 'paypal_tokens'

    @@ -30,6 +32,7 @@
    # - Create a payment
    # - Execute a payment.


    Meteor.methods
    'getPaypalToken': ->
    isTokenValid = 0
  11. Julien Le Coupanec created this gist May 4, 2014.
    129 changes: 129 additions & 0 deletions meteor-paypal-api-rest-server-file.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,129 @@
    ###
    This gist illustrates how to create and execute a payment with Paypal using their REST API.
    If you need something more, check the documentation: https://developer.paypal.com/docs/api/
    Note 1: I assume that you have already created a developer account for Paypal and an application.
    To test that your code is working, use the sandbox accounts.
    https://developer.paypal.com/webapps/developer/applications/accounts
    Note 2: we will not use the Paypal REST API SDK package for Node.js
    https://github.com/paypal/rest-api-sdk-nodejs
    ###


    # 1. Create your paypal configuration object.

    @paypalConf =
    host: "api.sandbox.paypal.com"
    clientId: "pHQcZ1cH0lnX5Mub4MGzo_-FH6witB3_2zuRYgvUFxMHFH6wiAe3zCRDqatu3"
    clientSecret:"Xk7-EDJqERAU5up6wjeVoRE6WM2OoIsUT3ouxVRKUmjX38b4k0-q6t_UHei"


    # 2. Create two collections to save our payments and the Paypal tokens.

    @PaypalPayments = new Meteor.Collection 'paypal_payments'
    @PaypalTokens = new Meteor.Collection 'paypal_tokens'


    # 3. Create the three methods to:
    # - Get a valid token to make API calls. (retrieve a new one if invalid)
    # - Create a payment
    # - Execute a payment.

    Meteor.methods
    'getPaypalToken': ->
    isTokenValid = 0
    token = PaypalTokens.findOne({ timestamp: { $exists: true } },
    { sort: { timestamp: -1 } })

    if token?
    isTokenValid = Math.ceil((new Date().getTime() - token.timestamp) / 1000)

    # is the token invalid?
    if isTokenValid is 0 or isTokenValid > token.expires_in
    auth = paypalConf['clientId'] + ':' +
    paypalConf['clientSecret']

    token = EJSON.parse(
    Meteor.http.post('https://api.sandbox.paypal.com/v1/oauth2/token',
    headers:
    'Accept': 'application/json'
    'Accept-Language': 'en_US'
    auth: auth
    params:
    'grant_type': 'client_credentials'
    ).content)

    token['timestamp'] = new Date().getTime()

    # we insert the new valid token to retrieve it later
    PaypalTokens.insert token

    return token

    'createPaypalPayment': (product) ->
    token = Meteor.call 'getPaypalToken'

    payment =
    intent: 'sale'
    payer:
    payment_method: 'paypal'

    redirect_urls:
    return_url: 'http://localhost:3000/dashboard/payment/paypal/execute'
    cancel_url: 'http://localhost:3000/dashboard'

    transactions: [
    item_list:
    'items': [
    'name': product.name,
    'price': product.price,
    'currency': 'USD',
    'quantity': 1
    ]

    amount:
    total: product.price
    currency: 'USD'

    description: product.description
    ]

    res = Meteor.http.post 'https://api.sandbox.paypal.com/v1/payments/payment',
    headers:
    Authorization: 'Bearer ' + token.access_token
    'Content-Type': 'application/json'
    data: payment

    res.data['userId'] = @userId

    # we insert the payment details (for the payment id during execution)
    PaypalPayments.insert res.data

    return res.data

    'executePaypalPayment': (payerId) ->
    payment = PaypalPayments.findOne({ userId: @userId },
    { sort: { 'create_time': -1 } })

    token = Meteor.call 'getPaypalToken'

    url = 'https://api.sandbox.paypal.com/v1/payments/payment/' +
    payment.id + '/execute'

    res = Meteor.http.post url,
    headers:
    Authorization: 'Bearer ' + token.access_token
    'Content-Type': 'application/json'
    data:
    payer_id: payerId

    payment = res.data
    payment['userId'] = @userId

    if payment.state is 'approved'
    # we insert the sucessful payment here
    PaypalPayments.insert payment

    return if payment.state is 'approved' then true else false