1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
<?php
/** * Class for the Stripe API. * * @link https://stripe.com/docs/api */ class WPCF7_Stripe_API {
const api_version = '2020-08-27'; const partner_id = 'pp_partner_HHbvqLh1AaO7Am'; const app_name = 'WordPress Contact Form 7'; const app_url = 'https://contactform7.com/stripe-integration/';
private $secret;
/** * Constructor. * * @param string $secret Secret key. */ public function __construct( $secret ) { $this->secret = $secret; }
/** * Sends a debug information for a remote request to the PHP error log. * * @param string $url URL to retrieve. * @param array $request Request arguments. * @param array|WP_Error $response The response or WP_Error on failure. */ private function log( $url, $request, $response ) { wpcf7_log_remote_request( $url, $request, $response ); }
/** * Returns default set of HTTP request headers used for Stripe API. * * @link https://stripe.com/docs/building-plugins#setappinfo * * @return array An associative array of headers. */ private function default_headers() { $app_info = array( 'name' => self::app_name, 'partner_id' => self::partner_id, 'url' => self::app_url, 'version' => WPCF7_VERSION, );
$ua = array( 'lang' => 'php', 'lang_version' => PHP_VERSION, 'application' => $app_info, );
$headers = array( 'Authorization' => sprintf( 'Bearer %s', $this->secret ), 'Stripe-Version' => self::api_version, 'X-Stripe-Client-User-Agent' => json_encode( $ua ), 'User-Agent' => sprintf( '%1$s/%2$s (%3$s)', self::app_name, WPCF7_VERSION, self::app_url ), );
return $headers; }
/** * Creates a Payment Intent. * * @link https://stripe.com/docs/api/payment_intents/create * * @param string|array $args Optional. Arguments to control behavior. * @return array|bool An associative array if 200 OK, false otherwise. */ public function create_payment_intent( $args = '' ) { $args = wp_parse_args( $args, array( 'amount' => 0, 'currency' => '', 'receipt_email' => '', ) );
if ( ! is_email( $args['receipt_email'] ) ) { unset( $args['receipt_email'] ); }
$endpoint = 'https://api.stripe.com/v1/payment_intents';
$request = array( 'headers' => $this->default_headers(), 'body' => $args, );
$response = wp_remote_post( esc_url_raw( $endpoint ), $request );
if ( 200 != wp_remote_retrieve_response_code( $response ) ) { if ( WP_DEBUG ) { $this->log( $endpoint, $request, $response ); }
return false; }
$response_body = wp_remote_retrieve_body( $response ); $response_body = json_decode( $response_body, true );
return $response_body; }
/** * Retrieve a Payment Intent. * * @link https://stripe.com/docs/api/payment_intents/retrieve * * @param string $id Payment Intent identifier. * @return array|bool An associative array if 200 OK, false otherwise. */ public function retrieve_payment_intent( $id ) { $endpoint = sprintf( 'https://api.stripe.com/v1/payment_intents/%s', urlencode( $id ) );
$request = array( 'headers' => $this->default_headers(), );
$response = wp_remote_get( esc_url_raw( $endpoint ), $request );
if ( 200 != wp_remote_retrieve_response_code( $response ) ) { if ( WP_DEBUG ) { $this->log( $endpoint, $request, $response ); }
return false; }
$response_body = wp_remote_retrieve_body( $response ); $response_body = json_decode( $response_body, true );
return $response_body; }
}
|