MDL-69166 pg_paypal: Process after payment
[moodle.git] / payment / gateway / paypal / classes / external / transaction_complete.php
CommitLineData
4403b8f6
SR
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This class contains a list of webservice functions related to the PayPal payment gateway.
19 *
20 * @package pg_paypal
21 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25declare(strict_types=1);
26
27namespace pg_paypal\external;
28
29use external_api;
30use external_function_parameters;
31use external_value;
32
33defined('MOODLE_INTERNAL') || die();
34
35require_once($CFG->libdir . '/externallib.php');
36
37class transaction_complete extends external_api {
38
39 /**
40 * Returns description of method parameters.
41 *
42 * @return external_function_parameters
43 */
44 public static function execute_parameters() {
45 return new external_function_parameters([
46 'component' => new external_value(PARAM_COMPONENT, 'The component name'),
47 'componentid' => new external_value(PARAM_INT, 'The item id in the context of the component'),
48 'orderid' => new external_value(PARAM_TEXT, 'The order id coming back from PayPal'),
49 'authorizationid' => new external_value(PARAM_TEXT, 'The authorization id coming back from PayPal'),
50 ]);
51 }
52
53 /**
54 * Perform what needs to be done when a transaction is reported to be complete.
55 * This function does not take cost as a parameter as we cannot rely on any provided value.
56 *
57 * @param string $component Name of the component that the componentid belongs to
58 * @param int $componentid An internal identifier that is used by the component
59 * @param string $orderid PayPal order ID
60 * @param string $authorizationid The PayPal-generated ID for the authorized payment
61 * @return array
62 */
63 public static function execute(string $component, int $componentid, string $orderid,
64 string $authorizationid): array {
65 global $USER, $DB;
66
67 self::validate_parameters(self::execute_parameters(), [
68 'component' => $component,
69 'componentid' => $componentid,
70 'orderid' => $orderid,
71 'authorizationid' => $authorizationid,
72 ]);
73
74 $config = get_config('pg_paypal');
75
76 [
77 'amount' => $amount,
78 'currency' => $currency
79 ] = \core_payment\helper::get_cost($component, $componentid);
80
81 $paypalhelper = new paypal_helper($config->clientid, $config->secret, false);
82 $authorization = $paypalhelper->capture_authorization($authorizationid, $amount, $currency);
83
84 $success = false;
85 $message = '';
86
87 if ($authorization) {
88 switch ($authorization['status']) {
89 case 'COMPLETED':
90 $success = true;
91 // Everything is correct. Let's give them what they paid for.
92 try {
93 \core_payment\helper::deliver_order($component, $componentid);
94
95 $paymentid = \core_payment\helper::save_payment($component, $componentid, (int)$USER->id, $amount, $currency,
96 'paypal');
97
98 // Store PayPal extra information.
99 $record = new \stdClass();
100 $record->paymentid = $paymentid;
101 $record->pp_orderid = $orderid;
102 $record->pp_authorizationid = $authorizationid;
103 $record->pp_paymentid = $authorization->id; // The PayPal-generated ID for the captured payment.
104 $record->pp_status = 'COMPLETED';
105
106 $DB->insert_record('pg_paypal', $record);
107 } catch (\Exception $e) {
108 debugging('Exception while trying to process payment: ' . $e->getMessage(), DEBUG_DEVELOPER);
109 $success = false;
110 $message = get_string('internalerror', 'pg_paypal');
111 }
112 break;
113 case 'PENDING':
114 $success = false;
115 $message = get_string('echecknotsupported', 'pg_paypal');
116 break;
117 default:
118 $success = false;
119 $message = get_string('paymentnotcleared', 'pg_paypal');
120 }
121 } else {
122 // Could not capture authorization!
123 $success = false;
124 $message = get_string('captureauthorizationfailed', 'pg_paypal');
125 }
126
127 return [
128 'success' => $success,
129 'message' => $message,
130 ];
131 }
132
133 /**
134 * Returns description of method result value.
135 *
136 * @return external_function_parameters
137 */
138 public static function execute_returns() {
139 return new external_function_parameters([
140 'success' => new external_value(PARAM_BOOL, 'Whether everything was successful or not.'),
141 'message' => new external_value(PARAM_TEXT, 'Message (usually the error message).', VALUE_OPTIONAL),
142 ]);
143 }
144}