Advertisement
Guest User

Code

a guest
Jul 23rd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. /*
  2. * Calls the LINE Pay API and "reserves" a payment transaction
  3. * It wil also redirect to the LINE Pay screen
  4. * */
  5. async Reserve(userProfile)
  6. {
  7. console.log("");
  8. console.log("linepaymodule.Reserve");
  9.  
  10. /*
  11. * These are the URLs that need to be put into a webview
  12. * These will be the URLs the app will redirect to when the user
  13. * either confirms or cancels the payment at the LINE Pay service
  14. * */
  15.  
  16. let queryString = this.PrepareQueryString(userProfile);
  17.  
  18. //Change to LIFF URL here
  19. //confirmURL = 'line://app/1595088973-yBZmjZ6M' + "?" + queryString;
  20. let confirmURL = process.env.BASE_URL + "/linepay_service/confirm";
  21. let cancelURL = process.env.BASE_URL + "/linepay_service/cancel";
  22. confirmURL = confirmURL + "?" + queryString;
  23. cancelURL = cancelURL + "?" + queryString;
  24.  
  25. let sqlvariables = [
  26. userProfile.botID,
  27. userProfile.userID,
  28. userProfile.language
  29. ];
  30.  
  31. try
  32. {
  33. /*
  34. * Get the cart contents from the database
  35. * It should be a specialized JSON, as LINE Pay has a specific JSON style to follow
  36. * */
  37. let returnedValue = await dbModule.useDBFunction(dbModule.SQLStrings.view_cart, sqlvariables);
  38. console.log(JSON.stringify(returnedValue));
  39.  
  40. //For debugging purposes, this will be the JSON returned for now
  41. let jsonReturned = {
  42. "amount" : 100,
  43. "currency" : "USD",
  44. "orderId" : "MKSI_S_20180904_1000001",
  45. "packages" : [
  46. {
  47. "id" : "1",
  48. "amount": 100,
  49. "products" : [
  50. {
  51. "name" : "Pen Brown",
  52. "quantity" : 2,
  53. "price" : 50
  54. }
  55. ]
  56. }
  57. ],
  58. "redirectUrls" : {
  59. "confirmUrl" : confirmURL,
  60. "cancelUrl" : cancelURL
  61. }
  62. };
  63. jsonReturned.redirectUrls.confirmUrl = confirmURL;
  64. jsonReturned.redirectUrls.confirmUrl = cancelURL;
  65.  
  66. //To skip CaptureAPI
  67. jsonReturned.options.payment.capture = true;
  68.  
  69. let apiResponse = await sendClassObj.LinePay_Reserve(jsonReturned);
  70. console.log("API Response: ", apiResponse);
  71.  
  72. //Save Transaction ID into database
  73. let jsonObject = await dbModule.useDBFunction(dbModule.SQLStrings.view_cart, sqlvariables);
  74.  
  75. //Display the cards with the API Response content
  76.  
  77. // let apiResponse = {
  78. // webURL: body.info.paymentUrl.web,
  79. // appURL: body.info.paymentUrl.app,
  80. // transactionID: body.info.transactionId,
  81. // paymentAccessToken: body.info.paymentAccessToken
  82. // };
  83.  
  84. let urlObject = {
  85. buildurl: apiResponse.webURL
  86. };
  87.  
  88. jsonObject.urlObject = urlObject;
  89.  
  90. //let messageType = this._FixMessageType(jsonObject);
  91. //displayModule.displayBuilder(userProfile, jsonObject, messageType);
  92.  
  93. //FOR DEBUGGING
  94. //Maybe get this JSON from the database? For better customization?
  95. let json = {
  96. "type": "bubble",
  97. "styles": {
  98. "body": {
  99. "backgroundColor": "#FFFFFF"
  100. },
  101. "footer": {
  102. "backgroundColor": "#FFFFFF",
  103. "separator": true,
  104. "separatorColor": "#000000"
  105. }
  106. },
  107. "body": {
  108. "type": "box",
  109. "layout": "vertical",
  110. "contents": [
  111. {
  112. "type": "text",
  113. "text": "Payment",
  114. "weight": "bold",
  115. "color": "#000000",
  116. "wrap": true,
  117. "size": "md"
  118. },
  119. {
  120. "type": "text",
  121. "text": "Please press the button to complete proceed.",
  122. "color": "#000000",
  123. "wrap": true,
  124. "size": "sm"
  125. },
  126. {
  127. "type": "spacer",
  128. "size": "xl"
  129. }
  130. ]
  131. },
  132. "footer": {
  133. "type": "box",
  134. "layout": "vertical",
  135. "spacing": "sm",
  136. "contents": [
  137. {
  138. "type": "button",
  139. "style": "primary",
  140. "action": {
  141. "type": "uri",
  142. "label": "LINE Pay",
  143. "uri": apiResponse.webURL
  144. }
  145. }
  146. ]
  147. }
  148. };
  149.  
  150. displayModule.sendRawJSON(userProfile, json);
  151. }
  152. catch (err)
  153. {
  154. console.log("");
  155. console.log("linepaymodule.Reserve ERROR!");
  156. console.log(err);
  157. this.DisplayErrorMessage(userProfile, "linepaymodule.Reserve", err);
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement