MDL-69166 pg_paypal: New helper to get order details from PayPal
[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');
bbb887f6 75 $sandbox = $config->environment == 'sandbox';
4403b8f6
SR
76
77 [
78 'amount' => $amount,
79 'currency' => $currency
80 ] = \core_payment\helper::get_cost($component, $componentid);
81
bbb887f6 82 $paypalhelper = new \pg_paypal\paypal_helper($config->clientid, $config->secret, $sandbox);
4403b8f6
SR
83 $authorization = $paypalhelper->capture_authorization($authorizationid, $amount, $currency);
84
85 $success = false;
86 $message = '';
87
88 if ($authorization) {
89 switch ($authorization['status']) {
90 case 'COMPLETED':
91 $success = true;
92 // Everything is correct. Let's give them what they paid for.
93 try {
94 \core_payment\helper::deliver_order($component, $componentid);
95
96 $paymentid = \core_payment\helper::save_payment($component, $componentid, (int)$USER->id, $amount, $currency,
97 'paypal');
98
99 // Store PayPal extra information.
100 $record = new \stdClass();
101 $record->paymentid = $paymentid;
102 $record->pp_orderid = $orderid;
103 $record->pp_authorizationid = $authorizationid;
104 $record->pp_paymentid = $authorization->id; // The PayPal-generated ID for the captured payment.
105 $record->pp_status = 'COMPLETED';
106
107 $DB->insert_record('pg_paypal', $record);
108 } catch (\Exception $e) {
109 debugging('Exception while trying to process payment: ' . $e->getMessage(), DEBUG_DEVELOPER);
110 $success = false;
111 $message = get_string('internalerror', 'pg_paypal');
112 }
113 break;
114 case 'PENDING':
115 $success = false;
116 $message = get_string('echecknotsupported', 'pg_paypal');
117 break;
118 default:
119 $success = false;
120 $message = get_string('paymentnotcleared', 'pg_paypal');
121 }
122 } else {
123 // Could not capture authorization!
124 $success = false;
125 $message = get_string('captureauthorizationfailed', 'pg_paypal');
126 }
127
128 return [
129 'success' => $success,
130 'message' => $message,
131 ];
132 }
133
134 /**
135 * Returns description of method result value.
136 *
137 * @return external_function_parameters
138 */
139 public static function execute_returns() {
140 return new external_function_parameters([
141 'success' => new external_value(PARAM_BOOL, 'Whether everything was successful or not.'),
142 'message' => new external_value(PARAM_TEXT, 'Message (usually the error message).', VALUE_OPTIONAL),
143 ]);
144 }
145}