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