Advertisement
Guest User

Untitled

a guest
May 26th, 2019
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. 'use strict'
  2. import request from 'request'
  3. import * as SubscriptionsActions from '../../actions/subscriptions'
  4. import { setup_env, api_url, return_url } from '../../config'
  5. import url from 'url'
  6.  
  7. const paypal = setup_env()
  8.  
  9. export async function createSubscription(req, res) {
  10. try {
  11. let request = req.body
  12. request.user_id = req.user.id;
  13. let subscription = await SubscriptionsActions.createSubscription(request);
  14. return res.json(subscription);
  15. } catch (e) {
  16. console.log(e)
  17. }
  18. }
  19.  
  20. export async function getSubscriptionDetails(req, res) {
  21. try {
  22. paypal.billingAgreement.get(req.billingAgreementId, (error, billingAgreement) => {
  23. if (error) {
  24. console.log(error);
  25. return res.status(500).json({message: "There was an error:" + error.response.message});
  26. } else {
  27. console.log("Get Billing Agreement");
  28. console.log(JSON.stringify(billingAgreement));
  29. return res.status(200).json(billingAgreement);
  30. }
  31. });
  32. } catch (e) {
  33. console.log(e)
  34. }
  35. }
  36.  
  37. export async function searchSubscription(req, res) {
  38. try {
  39. let request = req.body
  40. request.user_id = req.user.id;
  41. let subscription = await SubscriptionsActions.searchSubscription(request);
  42. return res.json(subscription);
  43. } catch (e) {
  44. console.log(e)
  45. }
  46. }
  47.  
  48. export async function updatePlan(req, res) {
  49.  
  50. let { plan_id, op, state } = req.body
  51. let plan_atts = []
  52. if (op === 'remove') {
  53. plan_atts = [{
  54. "op": op,
  55. "path": "/",
  56. "value": {}
  57. }];
  58. } else {
  59. plan_atts = [{
  60. "op": op || "replace",
  61. "path": "/",
  62. "value": {
  63. "state": state || "ACTIVE"
  64. }
  65. }];
  66. }
  67.  
  68. paypal.billingPlan.get(plan_id, (error, billingPlan) => {
  69. if (error) {
  70. return res.status(500).json({message: "Get plan. There was an error:" + JSON.stringify(error.response)});
  71. } else {
  72. paypal.billingPlan.update(plan_id, plan_atts, async (error, response) => {
  73. if (error) {
  74. console.log(error.response)
  75. return res.status(500).json({message: "Update plan. There was an error:" + JSON.stringify(error.response)});
  76. } else {
  77. let data = {
  78. "paypal_plan_id": plan_id,
  79. "state": state,
  80. "deleted": (state === 'DELETED'),
  81. }
  82. let respUpdate = await SubscriptionsActions.updatePlan(data);
  83. return res.status(200).json(billingPlan);
  84. }
  85. });
  86. }
  87. });
  88. }
  89.  
  90. export async function listPlans(req, res) {
  91. let { status, page_size, page, total_required } = req.query
  92.  
  93. let list_billing_plan = {
  94. 'status': status || 'ACTIVE',
  95. 'page_size': parseInt(page_size, 0) || 5,
  96. 'page': parseInt(page, 0) || 0,
  97. 'total_required': total_required || 'yes'
  98. }
  99.  
  100. paypal.billingPlan.list(list_billing_plan, (error, billingPlan) => {
  101. if (error) {
  102. console.log('error.response', error.response)
  103. return res.status(500).json({message: "there was an error:" + JSON.stringify(error.response.message)});
  104. } else {
  105. console.log('plans', billingPlan)
  106. return res.status(200).json(billingPlan);
  107. }
  108. });
  109. }
  110.  
  111. export async function getPlan(req, res) {
  112. let { plan_id } = req.query
  113.  
  114. paypal.billingPlan.get(plan_id, function (error, billingPlan) {
  115. if (error) {
  116. console.log(error);
  117. return res.status(500).json({message: "there was an error:" + JSON.stringify(error.response.message)});
  118. } else {
  119. return res.status(200).json(billingPlan)
  120. }
  121. });
  122. }
  123.  
  124.  
  125. export async function createPlan(req, res) {
  126. let { name,
  127. description,
  128. plan_type,
  129. frequency,
  130. frequency_interval,
  131. amount,
  132. currency,
  133. pay_def_plan_type,
  134. cycles,
  135. cancel_url,
  136. accept_url
  137. } = req.body
  138.  
  139. let plan = {
  140. "description": description,
  141. "merchant_preferences": {
  142. "setup_fee": {
  143. "value": amount,
  144. "currency": currency
  145. },
  146. "auto_bill_amount": "YES",
  147. "cancel_url": cancel_url || return_url + 'cancelPayment',
  148. "return_url": accept_url || return_url + 'acceptPayment',
  149. "initial_fail_amount_action": "CONTINUE",
  150. "max_fail_attempts": "1"
  151. },
  152. "name": name,
  153. "payment_definitions": [
  154. {
  155. "name": name,
  156. "type": pay_def_plan_type,
  157. "frequency": frequency,
  158. "frequency_interval": frequency_interval,
  159. "amount": {
  160. "value": amount,
  161. "currency": currency
  162. },
  163. "cycles": cycles
  164. }
  165. ],
  166. "type": plan_type
  167. };
  168.  
  169. paypal.billingPlan.create(plan, async (error, plan) => {
  170. if (error) {
  171. return res.status(500).send(JSON.stringify(error.response))
  172. } else {
  173. let planToSave = {}
  174. if (frequency === 'YEAR') {
  175. planToSave = {
  176. "paypal_plan_id": plan.id,
  177. "name": plan.name,
  178. "description": plan.description,
  179. "price_per_year": amount,
  180. "status": plan.state
  181. }
  182. } else if (frequency === 'MONTH') {
  183. planToSave = {
  184. "paypal_plan_id": plan.id,
  185. "name": plan.name,
  186. "description": plan.description,
  187. "price_per_month": amount,
  188. "status": plan.state
  189. }
  190. } else {
  191. return res.status(500).json({message: "Frequency is not either YEAR or MONTH. you did something wrong"});
  192. }
  193. await SubscriptionsActions.createPlan(planToSave);
  194. return res.status(200).json(plan);
  195. }
  196. });
  197. }
  198.  
  199. export async function searchSubscriptions(req, res) {
  200.  
  201. }
  202.  
  203. export async function createAgreement(req, res) {
  204. let isoDate = new Date();
  205. let tomorrow = isoDate.setDate(isoDate.getDate() + 1);
  206. isoDate = new Date(tomorrow);
  207. isoDate.toISOString().slice(0, 19) + 'Z';
  208.  
  209. let billingAgreementAttributes = {
  210. "name": req.body.name,
  211. "description": req.body.description,
  212. "start_date": isoDate,
  213. "plan": {
  214. "id": req.body.plan_id
  215. },
  216. "payer": {
  217. "payment_method": "paypal"
  218. }
  219. };
  220.  
  221. paypal.billingAgreement.create(billingAgreementAttributes, (error, billingAgreement) => {
  222. if (error) {
  223. console.log('error.response', error.response)
  224. return res.status(500).json({message: "There was an error:" + error.response.message});
  225. } else {
  226. for (let index = 0; index < billingAgreement.links.length; index++) {
  227. if (billingAgreement.links[index].rel === 'approval_url') {
  228. let approval_url = billingAgreement.links[index].href;
  229. return res.status(200).json({
  230. approval_url: approval_url,
  231. payment_token: url.parse(approval_url, true).query.token
  232. });
  233. }
  234. }
  235. }
  236. });
  237. }
  238.  
  239. export async function executeAgreement(req, res) {
  240. let paymentToken = req.body.paymentToken
  241. let plan_id = req.body.plan_id
  242.  
  243. paypal.billingAgreement.execute(paymentToken, {}, async (error, billingAgreement) => {
  244. if (error) {
  245. console.log(error.response)
  246. } else {
  247. let subscriptionToSave = {
  248. "agreement_Id": billingAgreement.id,
  249. "state": billingAgreement.state,
  250. "created_at": billingAgreement.start_date,
  251. "payer_id": billingAgreement.payer.payer_info.payer_id,
  252. "user_id": req.user.id,
  253. "plan_unq_id": plan_id
  254. }
  255. await SubscriptionsActions.createSubscription(subscriptionToSave);
  256. return res.status(200).json({message: 'Billing Agreement success. ID: ' + billingAgreement.id})
  257. }
  258. });
  259. }
  260.  
  261. export async function cancelAgreement(req, res) {
  262. let billingAgreementId = req.body.billingAgreementId
  263. let cancel_note = {
  264. "note": req.body.cancelAgreementNote
  265. }
  266.  
  267. paypal.billingAgreement.cancel(billingAgreementId, cancel_note, (error, response) => {
  268. if (error) {
  269. console.log(error);
  270. return res.status(500).json({message: "There was an error with cancelation:" + error.response.message});
  271. } else {
  272.  
  273. paypal.billingAgreement.get(billingAgreementId, (error, billingAgreement) => {
  274. if (error) {
  275. console.log(error.response);
  276. return res.status(500).json({message: "There was an error:" + error.response.message});
  277. } else {
  278. let data = {
  279. id: billingAgreementId,
  280. state: billingAgreement.state
  281. }
  282. SubscriptionsActions.cancelSubscription(data);
  283. return res.status(200).json({message: 'Your billing agreement state is: ' + billingAgreement.state});
  284. }
  285. });
  286. }
  287. });
  288. }
  289.  
  290. export async function getAgreement(req, res) {
  291. }
  292.  
  293. export async function updateAgreement(req, res) {
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement