DraftCheckoutsController.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Rest; |
| 4 | |
| 5 | use SureCart\Models\Checkout; |
| 6 | use SureCart\WordPress\Users\CustomerLinkService; |
| 7 | |
| 8 | /** |
| 9 | * Handle price requests through the REST API |
| 10 | */ |
| 11 | class DraftCheckoutsController extends RestController { |
| 12 | /** |
| 13 | * Class to make the requests. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $class = Checkout::class; |
| 18 | |
| 19 | /** |
| 20 | * Manually pay an order. |
| 21 | * |
| 22 | * @param \WP_REST_Request $request Rest Request. |
| 23 | * |
| 24 | * @return \SureCart\Models\Checkout|\WP_Error |
| 25 | */ |
| 26 | public function manuallyPay( \WP_REST_Request $request ) { |
| 27 | $checkout = $this->middleware( new $this->class( $request['id'] ), $request ); |
| 28 | if ( is_wp_error( $checkout ) ) { |
| 29 | return $checkout; |
| 30 | } |
| 31 | |
| 32 | $paid = $checkout->where( $request->get_query_params() )->with( |
| 33 | [ |
| 34 | 'purchases', // Important: we need to make sure we expand the purchase to provide access. |
| 35 | ] |
| 36 | )->manuallyPay(); |
| 37 | |
| 38 | // purchase created. |
| 39 | if ( ! empty( $paid->purchases->data ) ) { |
| 40 | foreach ( $paid->purchases->data as $purchase ) { |
| 41 | if ( empty( $purchase->revoked ) ) { |
| 42 | // broadcast the webhook. |
| 43 | do_action( 'surecart/purchase_created', $purchase ); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return $paid; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Finalize an order. |
| 53 | * |
| 54 | * @param \WP_REST_Request $request Rest Request. |
| 55 | * |
| 56 | * @return \SureCart\Models\Checkout|\WP_Error |
| 57 | */ |
| 58 | public function finalize( \WP_REST_Request $request ) { |
| 59 | $checkout = new $this->class( [ 'id' => $request['id'] ] ); |
| 60 | $finalized = $checkout->where( $request->get_query_params() )->finalize( $request->get_body_params() ); |
| 61 | |
| 62 | // handle error. |
| 63 | if ( is_wp_error($finalized)) { |
| 64 | return $finalized; |
| 65 | } |
| 66 | |
| 67 | // not paid, don't continue. |
| 68 | if ( !in_array( $finalized->status, ['paid'] )) { |
| 69 | return $finalized; |
| 70 | } |
| 71 | |
| 72 | // fetch the checkout with purchases. |
| 73 | $checkout = $checkout->where(array_merge( |
| 74 | $request->get_query_params(), |
| 75 | [ 'refresh_status' => true ] // Important: Do not remove. This will force syncing with the processor. |
| 76 | ))->with( |
| 77 | [ |
| 78 | 'purchases', // Important: we need to make sure we expand the purchase to provide access. |
| 79 | 'customer', // Important: we need to use this to create the WP User with the same info. |
| 80 | ] |
| 81 | )->find( $request['id'] ); |
| 82 | |
| 83 | // bail if error. |
| 84 | if ( is_wp_error( $checkout ) ) { |
| 85 | return $checkout; |
| 86 | } |
| 87 | |
| 88 | // purchase created. |
| 89 | if ( ! empty( $checkout->purchases->data ) ) { |
| 90 | foreach ( $checkout->purchases->data as $purchase ) { |
| 91 | if ( empty( $purchase->revoked ) ) { |
| 92 | try { |
| 93 | // broadcast the webhook. |
| 94 | do_action( 'surecart/purchase_created', $purchase ); |
| 95 | } catch( \Exception $e) { |
| 96 | error_log( $e->getMessage() ); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // the order is confirmed. |
| 103 | do_action( 'surecart/checkout_confirmed', $checkout, $request ); |
| 104 | |
| 105 | // return the order. |
| 106 | return $checkout; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Link the customer id to the order. |
| 111 | * |
| 112 | * @param \SureCart\Models\Checkout $checkout Checkout model. |
| 113 | * @return \WP_User|\WP_Error |
| 114 | */ |
| 115 | public function linkCustomerId( $checkout ) { |
| 116 | // get transient. |
| 117 | $password_hash = get_transient( 'sc_checkout_password_hash_' . $checkout->id ); |
| 118 | // delete transient. |
| 119 | delete_transient( 'sc_checkout_password_hash_' . $checkout->id ); |
| 120 | // link customer. |
| 121 | $service = new CustomerLinkService( $checkout, $password_hash ); |
| 122 | return $service->link(); |
| 123 | } |
| 124 | } |
| 125 |